mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-04 11:30:38 -05:00
Fix formatting issues (#471)
This commit is contained in:
@@ -19,6 +19,7 @@ A clear and concise description of any alternative solutions or features you've
|
||||
Add any other context or screenshots about the feature request here.
|
||||
|
||||
### How we code at Formbricks 🤓
|
||||
|
||||
- Everything is type-safe
|
||||
- All UI components are in the package `formbricks/ui`
|
||||
- Run `pnpm dev` to find a demo app to test in-app surveys at `localhost:3002`
|
||||
|
||||
@@ -202,7 +202,7 @@ export function EditMemberships({ environmentId }: EditMembershipsProps) {
|
||||
)}
|
||||
</div>
|
||||
<div className="rounded-lg border border-slate-200">
|
||||
<div className="grid h-12 grid-cols-20 content-center rounded-t-lg bg-slate-100 text-left text-sm font-semibold text-slate-900">
|
||||
<div className="grid-cols-20 grid h-12 content-center rounded-t-lg bg-slate-100 text-left text-sm font-semibold text-slate-900">
|
||||
<div className="col-span-2"></div>
|
||||
<div className="col-span-5">Fullname</div>
|
||||
<div className="col-span-5">Email</div>
|
||||
@@ -212,9 +212,9 @@ export function EditMemberships({ environmentId }: EditMembershipsProps) {
|
||||
<div className="grid-cols-20">
|
||||
{[...team.members, ...team.invitees].map((member) => (
|
||||
<div
|
||||
className="grid h-auto w-full grid-cols-20 content-center rounded-lg p-0.5 py-2 text-left text-sm text-slate-900"
|
||||
className="grid-cols-20 grid h-auto w-full content-center rounded-lg p-0.5 py-2 text-left text-sm text-slate-900"
|
||||
key={member.email}>
|
||||
<div className="h-58 pl-4 col-span-2">
|
||||
<div className="h-58 col-span-2 pl-4">
|
||||
<ProfileAvatar userId={member.userId || member.email} />
|
||||
</div>
|
||||
<div className="ph-no-capture col-span-5 flex flex-col justify-center break-all">
|
||||
|
||||
@@ -116,7 +116,6 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse)
|
||||
// GET /api/v1/teams/[teamId]/invite/[inviteId]
|
||||
// Retrieve an invite token
|
||||
else if (req.method === "GET") {
|
||||
|
||||
const invite = await prisma.invite.findUnique({
|
||||
where: {
|
||||
id: inviteId,
|
||||
|
||||
@@ -109,7 +109,7 @@ export const checkClickMatch = (event: MouseEvent) => {
|
||||
trackEvent(e.name).then((res) => {
|
||||
match(
|
||||
res,
|
||||
(_value) => { },
|
||||
(_value) => {},
|
||||
(err) => {
|
||||
errorHandler.handle(err);
|
||||
}
|
||||
@@ -124,7 +124,7 @@ export const checkClickMatch = (event: MouseEvent) => {
|
||||
trackEvent(e.name).then((res) => {
|
||||
match(
|
||||
res,
|
||||
(_value) => { },
|
||||
(_value) => {},
|
||||
(err) => {
|
||||
errorHandler.handle(err);
|
||||
}
|
||||
|
||||
@@ -1,254 +1,286 @@
|
||||
import fetchMock from "jest-fetch-mock";
|
||||
import { constants } from "../constants"
|
||||
import { constants } from "../constants";
|
||||
|
||||
const { environmentId, apiHost, sessionId, expiryTime,
|
||||
surveyId, questionOneId, questionTwoId,
|
||||
choiceOneId, choiceTwoId, choiceThreeId,
|
||||
initialPersonUid, initialUserId, initialUserEmail, newPersonUid,
|
||||
eventIdForRouteChange, updatedUserEmail, customAttributeKey,
|
||||
customAttributeValue, eventIdForEventTracking, userIdAttributeId,
|
||||
userInitialEmailAttributeId, userCustomAttrAttributeId,
|
||||
userUpdatedEmailAttributeId } = constants
|
||||
const {
|
||||
environmentId,
|
||||
apiHost,
|
||||
sessionId,
|
||||
expiryTime,
|
||||
surveyId,
|
||||
questionOneId,
|
||||
questionTwoId,
|
||||
choiceOneId,
|
||||
choiceTwoId,
|
||||
choiceThreeId,
|
||||
initialPersonUid,
|
||||
initialUserId,
|
||||
initialUserEmail,
|
||||
newPersonUid,
|
||||
eventIdForRouteChange,
|
||||
updatedUserEmail,
|
||||
customAttributeKey,
|
||||
customAttributeValue,
|
||||
eventIdForEventTracking,
|
||||
userIdAttributeId,
|
||||
userInitialEmailAttributeId,
|
||||
userCustomAttrAttributeId,
|
||||
userUpdatedEmailAttributeId,
|
||||
} = constants;
|
||||
|
||||
export const mockInitResponse = () => {
|
||||
fetchMock.mockResponseOnce(JSON.stringify({
|
||||
apiHost,
|
||||
fetchMock.mockResponseOnce(
|
||||
JSON.stringify({
|
||||
apiHost,
|
||||
environmentId,
|
||||
person: {
|
||||
id: initialPersonUid,
|
||||
environmentId,
|
||||
person: {
|
||||
id: initialPersonUid,
|
||||
environmentId,
|
||||
attributes: []
|
||||
},
|
||||
session: {
|
||||
"id": sessionId,
|
||||
expiresAt: expiryTime
|
||||
},
|
||||
settings: {
|
||||
surveys: [
|
||||
{
|
||||
id: surveyId,
|
||||
questions: [
|
||||
{
|
||||
id: questionOneId,
|
||||
type: "multipleChoiceSingle",
|
||||
choices: [
|
||||
{
|
||||
id: choiceOneId,
|
||||
label: "Not at all disappointed"
|
||||
},
|
||||
{
|
||||
id: choiceTwoId,
|
||||
label: "Somewhat disappointed"
|
||||
},
|
||||
{
|
||||
id: choiceThreeId,
|
||||
label: "Very disappointed"
|
||||
}
|
||||
],
|
||||
headline: "How disappointed would you be if you could no longer use Test-Formbricks?",
|
||||
required: true,
|
||||
subheader: "Please select one of the following options:"
|
||||
},
|
||||
{
|
||||
id: questionTwoId,
|
||||
type: "openText",
|
||||
headline: "How can we improve Test-Formbricks for you?",
|
||||
required: true,
|
||||
subheader: "Please be as specific as possible."
|
||||
}
|
||||
],
|
||||
triggers: [],
|
||||
thankYouCard: {
|
||||
enabled: true,
|
||||
headline: "Thank you!",
|
||||
subheader: "We appreciate your feedback."
|
||||
},
|
||||
autoClose: null,
|
||||
delay: 0
|
||||
}],
|
||||
noCodeEvents: [],
|
||||
brandColor: "#20b398",
|
||||
formbricksSignature: true,
|
||||
placement: "bottomRight",
|
||||
darkOverlay: false,
|
||||
clickOutsideClose: true
|
||||
}
|
||||
}));
|
||||
}
|
||||
attributes: [],
|
||||
},
|
||||
session: {
|
||||
id: sessionId,
|
||||
expiresAt: expiryTime,
|
||||
},
|
||||
settings: {
|
||||
surveys: [
|
||||
{
|
||||
id: surveyId,
|
||||
questions: [
|
||||
{
|
||||
id: questionOneId,
|
||||
type: "multipleChoiceSingle",
|
||||
choices: [
|
||||
{
|
||||
id: choiceOneId,
|
||||
label: "Not at all disappointed",
|
||||
},
|
||||
{
|
||||
id: choiceTwoId,
|
||||
label: "Somewhat disappointed",
|
||||
},
|
||||
{
|
||||
id: choiceThreeId,
|
||||
label: "Very disappointed",
|
||||
},
|
||||
],
|
||||
headline: "How disappointed would you be if you could no longer use Test-Formbricks?",
|
||||
required: true,
|
||||
subheader: "Please select one of the following options:",
|
||||
},
|
||||
{
|
||||
id: questionTwoId,
|
||||
type: "openText",
|
||||
headline: "How can we improve Test-Formbricks for you?",
|
||||
required: true,
|
||||
subheader: "Please be as specific as possible.",
|
||||
},
|
||||
],
|
||||
triggers: [],
|
||||
thankYouCard: {
|
||||
enabled: true,
|
||||
headline: "Thank you!",
|
||||
subheader: "We appreciate your feedback.",
|
||||
},
|
||||
autoClose: null,
|
||||
delay: 0,
|
||||
},
|
||||
],
|
||||
noCodeEvents: [],
|
||||
brandColor: "#20b398",
|
||||
formbricksSignature: true,
|
||||
placement: "bottomRight",
|
||||
darkOverlay: false,
|
||||
clickOutsideClose: true,
|
||||
},
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
export const mockSetUserIdResponse = () => {
|
||||
fetchMock.mockResponseOnce(JSON.stringify({
|
||||
apiHost,
|
||||
fetchMock.mockResponseOnce(
|
||||
JSON.stringify({
|
||||
apiHost,
|
||||
environmentId,
|
||||
settings: {
|
||||
surveys: [],
|
||||
noCodeEvents: [],
|
||||
},
|
||||
person: {
|
||||
id: initialPersonUid,
|
||||
environmentId,
|
||||
settings: {
|
||||
surveys: [],
|
||||
noCodeEvents: [],
|
||||
},
|
||||
person: {
|
||||
id: initialPersonUid,
|
||||
environmentId,
|
||||
attributes: [
|
||||
{
|
||||
id: userIdAttributeId,
|
||||
value: initialUserId,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "userId"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}));
|
||||
}
|
||||
attributes: [
|
||||
{
|
||||
id: userIdAttributeId,
|
||||
value: initialUserId,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "userId",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
export const mockSetEmailIdResponse = () => {
|
||||
fetchMock.mockResponseOnce(JSON.stringify({
|
||||
apiHost,
|
||||
fetchMock.mockResponseOnce(
|
||||
JSON.stringify({
|
||||
apiHost,
|
||||
environmentId,
|
||||
settings: {
|
||||
surveys: [],
|
||||
noCodeEvents: [],
|
||||
},
|
||||
person: {
|
||||
id: initialPersonUid,
|
||||
environmentId,
|
||||
settings: {
|
||||
surveys: [],
|
||||
noCodeEvents: [],
|
||||
},
|
||||
person: {
|
||||
id: initialPersonUid,
|
||||
environmentId,
|
||||
attributes: [
|
||||
{
|
||||
id: userIdAttributeId,
|
||||
value: initialUserId,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "userId"
|
||||
}
|
||||
},
|
||||
{
|
||||
id: userInitialEmailAttributeId,
|
||||
value: initialUserEmail,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "email"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
}));
|
||||
}
|
||||
attributes: [
|
||||
{
|
||||
id: userIdAttributeId,
|
||||
value: initialUserId,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "userId",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: userInitialEmailAttributeId,
|
||||
value: initialUserEmail,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "email",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
export const mockSetCustomAttributeResponse = () => {
|
||||
fetchMock.mockResponseOnce(JSON.stringify({
|
||||
apiHost,
|
||||
fetchMock.mockResponseOnce(
|
||||
JSON.stringify({
|
||||
apiHost,
|
||||
environmentId,
|
||||
settings: {
|
||||
surveys: [],
|
||||
noCodeEvents: [],
|
||||
},
|
||||
person: {
|
||||
id: initialPersonUid,
|
||||
environmentId,
|
||||
settings: {
|
||||
surveys: [],
|
||||
noCodeEvents: [],
|
||||
},
|
||||
person: {
|
||||
id: initialPersonUid,
|
||||
environmentId,
|
||||
attributes: [
|
||||
{
|
||||
id: userIdAttributeId,
|
||||
value: initialUserId,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "userId"
|
||||
}
|
||||
},
|
||||
{
|
||||
id: userInitialEmailAttributeId,
|
||||
value: initialUserEmail,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "email"
|
||||
}
|
||||
},
|
||||
{
|
||||
id: userCustomAttrAttributeId,
|
||||
value: customAttributeValue,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: customAttributeKey
|
||||
}
|
||||
}
|
||||
|
||||
]
|
||||
},
|
||||
}));
|
||||
}
|
||||
attributes: [
|
||||
{
|
||||
id: userIdAttributeId,
|
||||
value: initialUserId,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "userId",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: userInitialEmailAttributeId,
|
||||
value: initialUserEmail,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "email",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: userCustomAttrAttributeId,
|
||||
value: customAttributeValue,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: customAttributeKey,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
export const mockUpdateEmailResponse = () => {
|
||||
fetchMock.mockResponseOnce(JSON.stringify({
|
||||
apiHost,
|
||||
fetchMock.mockResponseOnce(
|
||||
JSON.stringify({
|
||||
apiHost,
|
||||
environmentId,
|
||||
settings: {
|
||||
surveys: [],
|
||||
noCodeEvents: [],
|
||||
},
|
||||
person: {
|
||||
id: initialPersonUid,
|
||||
environmentId,
|
||||
settings: {
|
||||
surveys: [],
|
||||
noCodeEvents: [],
|
||||
},
|
||||
person: {
|
||||
id: initialPersonUid,
|
||||
environmentId,
|
||||
attributes: [
|
||||
{
|
||||
id: userIdAttributeId,
|
||||
value: initialUserId,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "userId"
|
||||
}
|
||||
},
|
||||
{
|
||||
id: userUpdatedEmailAttributeId,
|
||||
value: updatedUserEmail,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "email"
|
||||
}
|
||||
},
|
||||
{
|
||||
id: userCustomAttrAttributeId,
|
||||
value: customAttributeValue,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: customAttributeKey
|
||||
}
|
||||
}
|
||||
|
||||
]
|
||||
},
|
||||
}));
|
||||
}
|
||||
attributes: [
|
||||
{
|
||||
id: userIdAttributeId,
|
||||
value: initialUserId,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "userId",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: userUpdatedEmailAttributeId,
|
||||
value: updatedUserEmail,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: "email",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: userCustomAttrAttributeId,
|
||||
value: customAttributeValue,
|
||||
attributeClass: {
|
||||
id: environmentId,
|
||||
name: customAttributeKey,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
export const mockEventTrackResponse = () => {
|
||||
fetchMock.mockResponseOnce(JSON.stringify({
|
||||
id: eventIdForEventTracking,
|
||||
}));
|
||||
console.log('Formbricks: Event "Button Clicked" tracked')
|
||||
}
|
||||
fetchMock.mockResponseOnce(
|
||||
JSON.stringify({
|
||||
id: eventIdForEventTracking,
|
||||
})
|
||||
);
|
||||
console.log('Formbricks: Event "Button Clicked" tracked');
|
||||
};
|
||||
|
||||
export const mockRefreshResponse = () => {
|
||||
fetchMock.mockResponseOnce(JSON.stringify({}));
|
||||
console.log('Settings refreshed')
|
||||
}
|
||||
fetchMock.mockResponseOnce(JSON.stringify({}));
|
||||
console.log("Settings refreshed");
|
||||
};
|
||||
|
||||
export const mockRegisterRouteChangeResponse = () => {
|
||||
fetchMock.mockResponseOnce(JSON.stringify({
|
||||
id: eventIdForRouteChange,
|
||||
}));
|
||||
console.log('Checking page url: http://localhost/');
|
||||
}
|
||||
fetchMock.mockResponseOnce(
|
||||
JSON.stringify({
|
||||
id: eventIdForRouteChange,
|
||||
})
|
||||
);
|
||||
console.log("Checking page url: http://localhost/");
|
||||
};
|
||||
|
||||
export const mockLogoutResponse = () => {
|
||||
fetchMock.mockResponseOnce(JSON.stringify({
|
||||
settings: {
|
||||
surveys: [],
|
||||
noCodeEvents: [],
|
||||
},
|
||||
person: {
|
||||
id: newPersonUid,
|
||||
environmentId,
|
||||
attributes: []
|
||||
},
|
||||
session: {},
|
||||
}));
|
||||
console.log('Resetting person. Getting new person, session and settings from backend');
|
||||
}
|
||||
fetchMock.mockResponseOnce(
|
||||
JSON.stringify({
|
||||
settings: {
|
||||
surveys: [],
|
||||
noCodeEvents: [],
|
||||
},
|
||||
person: {
|
||||
id: newPersonUid,
|
||||
environmentId,
|
||||
attributes: [],
|
||||
},
|
||||
session: {},
|
||||
})
|
||||
);
|
||||
console.log("Resetting person. Getting new person, session and settings from backend");
|
||||
};
|
||||
|
||||
@@ -1,59 +1,59 @@
|
||||
const generateUserId = () => {
|
||||
const min = 1000;
|
||||
const max = 9999;
|
||||
const randomNum = Math.floor(Math.random() * (max - min + 1) + min);
|
||||
return randomNum.toString();
|
||||
}
|
||||
const min = 1000;
|
||||
const max = 9999;
|
||||
const randomNum = Math.floor(Math.random() * (max - min + 1) + min);
|
||||
return randomNum.toString();
|
||||
};
|
||||
|
||||
const generateEmailId = () => {
|
||||
const domain = "formbricks.test";
|
||||
const randomString = Math.random().toString(36).substring(2);
|
||||
const emailId = `${randomString}@${domain}`;
|
||||
return emailId;
|
||||
const domain = "formbricks.test";
|
||||
const randomString = Math.random().toString(36).substring(2);
|
||||
const emailId = `${randomString}@${domain}`;
|
||||
return emailId;
|
||||
};
|
||||
|
||||
const generateRandomString = () => {
|
||||
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
const maxLength = 8;
|
||||
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
const maxLength = 8;
|
||||
|
||||
let randomString = "";
|
||||
for (let i = 0; i < maxLength; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * characters.length);
|
||||
randomString += characters.charAt(randomIndex);
|
||||
}
|
||||
return randomString;
|
||||
let randomString = "";
|
||||
for (let i = 0; i < maxLength; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * characters.length);
|
||||
randomString += characters.charAt(randomIndex);
|
||||
}
|
||||
return randomString;
|
||||
};
|
||||
|
||||
const getOneDayExpiryTime = () => {
|
||||
var ms = new Date().getTime();
|
||||
var oneDayMs = 24 * 60 * 60 * 1000; // Number of milliseconds in one day
|
||||
var expiryOfOneDay = ms + oneDayMs
|
||||
return expiryOfOneDay;
|
||||
}
|
||||
var ms = new Date().getTime();
|
||||
var oneDayMs = 24 * 60 * 60 * 1000; // Number of milliseconds in one day
|
||||
var expiryOfOneDay = ms + oneDayMs;
|
||||
return expiryOfOneDay;
|
||||
};
|
||||
|
||||
export const constants = {
|
||||
environmentId: "mockedEnvironmentId",
|
||||
apiHost: "mockedApiHost",
|
||||
sessionId: generateRandomString(),
|
||||
expiryTime: getOneDayExpiryTime(),
|
||||
surveyId: generateRandomString(),
|
||||
questionOneId: generateRandomString(),
|
||||
questionTwoId: generateRandomString(),
|
||||
choiceOneId: generateRandomString(),
|
||||
choiceTwoId: generateRandomString(),
|
||||
choiceThreeId: generateRandomString(),
|
||||
choiceFourId: generateRandomString(),
|
||||
initialPersonUid: generateRandomString(),
|
||||
newPersonUid: generateRandomString(),
|
||||
initialUserId: generateUserId(),
|
||||
initialUserEmail: generateEmailId(),
|
||||
updatedUserEmail: generateEmailId(),
|
||||
customAttributeKey: generateRandomString(),
|
||||
customAttributeValue: generateRandomString(),
|
||||
userIdAttributeId: generateRandomString(),
|
||||
userInitialEmailAttributeId: generateRandomString(),
|
||||
userCustomAttrAttributeId: generateRandomString(),
|
||||
userUpdatedEmailAttributeId: generateRandomString(),
|
||||
eventIdForEventTracking: generateRandomString(),
|
||||
eventIdForRouteChange: generateRandomString()
|
||||
environmentId: "mockedEnvironmentId",
|
||||
apiHost: "mockedApiHost",
|
||||
sessionId: generateRandomString(),
|
||||
expiryTime: getOneDayExpiryTime(),
|
||||
surveyId: generateRandomString(),
|
||||
questionOneId: generateRandomString(),
|
||||
questionTwoId: generateRandomString(),
|
||||
choiceOneId: generateRandomString(),
|
||||
choiceTwoId: generateRandomString(),
|
||||
choiceThreeId: generateRandomString(),
|
||||
choiceFourId: generateRandomString(),
|
||||
initialPersonUid: generateRandomString(),
|
||||
newPersonUid: generateRandomString(),
|
||||
initialUserId: generateUserId(),
|
||||
initialUserEmail: generateEmailId(),
|
||||
updatedUserEmail: generateEmailId(),
|
||||
customAttributeKey: generateRandomString(),
|
||||
customAttributeValue: generateRandomString(),
|
||||
userIdAttributeId: generateRandomString(),
|
||||
userInitialEmailAttributeId: generateRandomString(),
|
||||
userCustomAttrAttributeId: generateRandomString(),
|
||||
userUpdatedEmailAttributeId: generateRandomString(),
|
||||
eventIdForEventTracking: generateRandomString(),
|
||||
eventIdForRouteChange: generateRandomString(),
|
||||
} as const;
|
||||
|
||||
+147
-127
@@ -2,179 +2,199 @@
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
import formbricks from "../src/index";
|
||||
import { constants } from "./constants"
|
||||
import { constants } from "./constants";
|
||||
import { Attribute } from "./types";
|
||||
import { mockEventTrackResponse, mockInitResponse, mockLogoutResponse, mockRefreshResponse, mockRegisterRouteChangeResponse, mockSetCustomAttributeResponse, mockSetEmailIdResponse, mockSetUserIdResponse, mockUpdateEmailResponse } from "./__mocks__/apiMock";
|
||||
import {
|
||||
mockEventTrackResponse,
|
||||
mockInitResponse,
|
||||
mockLogoutResponse,
|
||||
mockRefreshResponse,
|
||||
mockRegisterRouteChangeResponse,
|
||||
mockSetCustomAttributeResponse,
|
||||
mockSetEmailIdResponse,
|
||||
mockSetUserIdResponse,
|
||||
mockUpdateEmailResponse,
|
||||
} from "./__mocks__/apiMock";
|
||||
|
||||
const consoleLogMock = jest.spyOn(console, 'log').mockImplementation();
|
||||
const consoleLogMock = jest.spyOn(console, "log").mockImplementation();
|
||||
|
||||
test("Test Jest", () => {
|
||||
expect(1 + 9).toBe(10);
|
||||
expect(1 + 9).toBe(10);
|
||||
});
|
||||
|
||||
const { environmentId, apiHost, initialUserId, initialUserEmail, updatedUserEmail, customAttributeKey, customAttributeValue } = constants
|
||||
const {
|
||||
environmentId,
|
||||
apiHost,
|
||||
initialUserId,
|
||||
initialUserEmail,
|
||||
updatedUserEmail,
|
||||
customAttributeKey,
|
||||
customAttributeValue,
|
||||
} = constants;
|
||||
|
||||
beforeEach(() => {
|
||||
fetchMock.resetMocks();
|
||||
fetchMock.resetMocks();
|
||||
});
|
||||
|
||||
test("Formbricks should Initialise", async () => {
|
||||
mockInitResponse()
|
||||
mockInitResponse();
|
||||
|
||||
await formbricks.init({
|
||||
environmentId,
|
||||
apiHost,
|
||||
});
|
||||
await formbricks.init({
|
||||
environmentId,
|
||||
apiHost,
|
||||
});
|
||||
|
||||
const configFromBrowser = localStorage.getItem("formbricksConfig");
|
||||
expect(configFromBrowser).toBeTruthy();
|
||||
const configFromBrowser = localStorage.getItem("formbricksConfig");
|
||||
expect(configFromBrowser).toBeTruthy();
|
||||
|
||||
if (configFromBrowser) {
|
||||
const jsonSavedConfig = JSON.parse(configFromBrowser);
|
||||
expect(jsonSavedConfig.environmentId).toStrictEqual(environmentId);
|
||||
expect(jsonSavedConfig.apiHost).toStrictEqual(apiHost);
|
||||
}
|
||||
if (configFromBrowser) {
|
||||
const jsonSavedConfig = JSON.parse(configFromBrowser);
|
||||
expect(jsonSavedConfig.environmentId).toStrictEqual(environmentId);
|
||||
expect(jsonSavedConfig.apiHost).toStrictEqual(apiHost);
|
||||
}
|
||||
});
|
||||
|
||||
test("Formbricks should get the current person with no attributes", () => {
|
||||
const currentState = formbricks.getPerson()
|
||||
const currentState = formbricks.getPerson();
|
||||
|
||||
const currentStateAttributes: Array<Attribute> = currentState.attributes as Array<Attribute>;
|
||||
expect(currentStateAttributes).toHaveLength(0)
|
||||
})
|
||||
const currentStateAttributes: Array<Attribute> = currentState.attributes as Array<Attribute>;
|
||||
expect(currentStateAttributes).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("Formbricks should set userId", async () => {
|
||||
mockSetUserIdResponse()
|
||||
await formbricks.setUserId(initialUserId)
|
||||
mockSetUserIdResponse();
|
||||
await formbricks.setUserId(initialUserId);
|
||||
|
||||
const currentState = formbricks.getPerson()
|
||||
expect(currentState.environmentId).toStrictEqual(environmentId)
|
||||
const currentState = formbricks.getPerson();
|
||||
expect(currentState.environmentId).toStrictEqual(environmentId);
|
||||
|
||||
const currentStateAttributes: Array<Attribute> = currentState.attributes as Array<Attribute>;
|
||||
const numberOfUserAttributes = currentStateAttributes.length
|
||||
expect(numberOfUserAttributes).toStrictEqual(1)
|
||||
const currentStateAttributes: Array<Attribute> = currentState.attributes as Array<Attribute>;
|
||||
const numberOfUserAttributes = currentStateAttributes.length;
|
||||
expect(numberOfUserAttributes).toStrictEqual(1);
|
||||
|
||||
currentStateAttributes.forEach((attribute) => {
|
||||
switch (attribute.attributeClass.name) {
|
||||
case "userId":
|
||||
expect(attribute.value).toStrictEqual(initialUserId)
|
||||
break;
|
||||
default:
|
||||
expect(0).toStrictEqual(1)
|
||||
}
|
||||
})
|
||||
})
|
||||
currentStateAttributes.forEach((attribute) => {
|
||||
switch (attribute.attributeClass.name) {
|
||||
case "userId":
|
||||
expect(attribute.value).toStrictEqual(initialUserId);
|
||||
break;
|
||||
default:
|
||||
expect(0).toStrictEqual(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test("Formbricks should set email", async () => {
|
||||
mockSetEmailIdResponse()
|
||||
await formbricks.setEmail(initialUserEmail)
|
||||
mockSetEmailIdResponse();
|
||||
await formbricks.setEmail(initialUserEmail);
|
||||
|
||||
const currentState = formbricks.getPerson()
|
||||
expect(currentState.environmentId).toStrictEqual(environmentId)
|
||||
const currentState = formbricks.getPerson();
|
||||
expect(currentState.environmentId).toStrictEqual(environmentId);
|
||||
|
||||
const currentStateAttributes: Array<Attribute> = currentState.attributes as Array<Attribute>;
|
||||
const numberOfUserAttributes = currentStateAttributes.length
|
||||
expect(numberOfUserAttributes).toStrictEqual(2)
|
||||
const currentStateAttributes: Array<Attribute> = currentState.attributes as Array<Attribute>;
|
||||
const numberOfUserAttributes = currentStateAttributes.length;
|
||||
expect(numberOfUserAttributes).toStrictEqual(2);
|
||||
|
||||
currentStateAttributes.forEach((attribute) => {
|
||||
switch (attribute.attributeClass.name) {
|
||||
case "userId":
|
||||
expect(attribute.value).toStrictEqual(initialUserId)
|
||||
break;
|
||||
case "email":
|
||||
expect(attribute.value).toStrictEqual(initialUserEmail)
|
||||
break;
|
||||
default:
|
||||
expect(0).toStrictEqual(1)
|
||||
}
|
||||
})
|
||||
})
|
||||
currentStateAttributes.forEach((attribute) => {
|
||||
switch (attribute.attributeClass.name) {
|
||||
case "userId":
|
||||
expect(attribute.value).toStrictEqual(initialUserId);
|
||||
break;
|
||||
case "email":
|
||||
expect(attribute.value).toStrictEqual(initialUserEmail);
|
||||
break;
|
||||
default:
|
||||
expect(0).toStrictEqual(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test("Formbricks should set custom attribute", async () => {
|
||||
mockSetCustomAttributeResponse()
|
||||
await formbricks.setAttribute(customAttributeKey, customAttributeValue)
|
||||
mockSetCustomAttributeResponse();
|
||||
await formbricks.setAttribute(customAttributeKey, customAttributeValue);
|
||||
|
||||
const currentState = formbricks.getPerson()
|
||||
expect(currentState.environmentId).toStrictEqual(environmentId)
|
||||
const currentState = formbricks.getPerson();
|
||||
expect(currentState.environmentId).toStrictEqual(environmentId);
|
||||
|
||||
const currentStateAttributes: Array<Attribute> = currentState.attributes as Array<Attribute>;
|
||||
const numberOfUserAttributes = currentStateAttributes.length
|
||||
expect(numberOfUserAttributes).toStrictEqual(3)
|
||||
const currentStateAttributes: Array<Attribute> = currentState.attributes as Array<Attribute>;
|
||||
const numberOfUserAttributes = currentStateAttributes.length;
|
||||
expect(numberOfUserAttributes).toStrictEqual(3);
|
||||
|
||||
currentStateAttributes.forEach((attribute) => {
|
||||
switch (attribute.attributeClass.name) {
|
||||
case "userId":
|
||||
expect(attribute.value).toStrictEqual(initialUserId)
|
||||
break;
|
||||
case "email":
|
||||
expect(attribute.value).toStrictEqual(initialUserEmail)
|
||||
break;
|
||||
case customAttributeKey:
|
||||
expect(attribute.value).toStrictEqual(customAttributeValue)
|
||||
break;
|
||||
default:
|
||||
expect(0).toStrictEqual(1)
|
||||
}
|
||||
})
|
||||
})
|
||||
currentStateAttributes.forEach((attribute) => {
|
||||
switch (attribute.attributeClass.name) {
|
||||
case "userId":
|
||||
expect(attribute.value).toStrictEqual(initialUserId);
|
||||
break;
|
||||
case "email":
|
||||
expect(attribute.value).toStrictEqual(initialUserEmail);
|
||||
break;
|
||||
case customAttributeKey:
|
||||
expect(attribute.value).toStrictEqual(customAttributeValue);
|
||||
break;
|
||||
default:
|
||||
expect(0).toStrictEqual(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test("Formbricks should update attribute", async () => {
|
||||
mockUpdateEmailResponse()
|
||||
await formbricks.setEmail(updatedUserEmail)
|
||||
mockUpdateEmailResponse();
|
||||
await formbricks.setEmail(updatedUserEmail);
|
||||
|
||||
const currentState = formbricks.getPerson()
|
||||
expect(currentState.environmentId).toStrictEqual(environmentId)
|
||||
const currentState = formbricks.getPerson();
|
||||
expect(currentState.environmentId).toStrictEqual(environmentId);
|
||||
|
||||
const currentStateAttributes: Array<Attribute> = currentState.attributes as Array<Attribute>;
|
||||
const currentStateAttributes: Array<Attribute> = currentState.attributes as Array<Attribute>;
|
||||
|
||||
const numberOfUserAttributes = currentStateAttributes.length
|
||||
expect(numberOfUserAttributes).toStrictEqual(3)
|
||||
const numberOfUserAttributes = currentStateAttributes.length;
|
||||
expect(numberOfUserAttributes).toStrictEqual(3);
|
||||
|
||||
currentStateAttributes.forEach((attribute) => {
|
||||
switch (attribute.attributeClass.name) {
|
||||
case "email":
|
||||
expect(attribute.value).toStrictEqual(updatedUserEmail)
|
||||
break;
|
||||
case "userId":
|
||||
expect(attribute.value).toStrictEqual(initialUserId)
|
||||
break;
|
||||
case customAttributeKey:
|
||||
expect(attribute.value).toStrictEqual(customAttributeValue)
|
||||
break;
|
||||
default:
|
||||
expect(0).toStrictEqual(1)
|
||||
}
|
||||
})
|
||||
})
|
||||
currentStateAttributes.forEach((attribute) => {
|
||||
switch (attribute.attributeClass.name) {
|
||||
case "email":
|
||||
expect(attribute.value).toStrictEqual(updatedUserEmail);
|
||||
break;
|
||||
case "userId":
|
||||
expect(attribute.value).toStrictEqual(initialUserId);
|
||||
break;
|
||||
case customAttributeKey:
|
||||
expect(attribute.value).toStrictEqual(customAttributeValue);
|
||||
break;
|
||||
default:
|
||||
expect(0).toStrictEqual(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test("Formbricks should track event", async () => {
|
||||
mockEventTrackResponse()
|
||||
const mockButton = document.createElement("button");
|
||||
mockButton.addEventListener("click", async () => {
|
||||
await formbricks.track("Button Clicked");
|
||||
});
|
||||
await mockButton.click();
|
||||
expect(consoleLogMock).toHaveBeenCalledWith(expect.stringMatching(/Formbricks: Event "Button Clicked" tracked/));
|
||||
mockEventTrackResponse();
|
||||
const mockButton = document.createElement("button");
|
||||
mockButton.addEventListener("click", async () => {
|
||||
await formbricks.track("Button Clicked");
|
||||
});
|
||||
await mockButton.click();
|
||||
expect(consoleLogMock).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/Formbricks: Event "Button Clicked" tracked/)
|
||||
);
|
||||
});
|
||||
|
||||
test("Formbricks should refresh", async () => {
|
||||
mockRefreshResponse()
|
||||
await formbricks.refresh()
|
||||
expect(consoleLogMock).toHaveBeenCalledWith(expect.stringMatching(/Settings refreshed/));
|
||||
})
|
||||
mockRefreshResponse();
|
||||
await formbricks.refresh();
|
||||
expect(consoleLogMock).toHaveBeenCalledWith(expect.stringMatching(/Settings refreshed/));
|
||||
});
|
||||
|
||||
test("Formbricks should register for route change", async () => {
|
||||
mockRegisterRouteChangeResponse()
|
||||
await formbricks.registerRouteChange()
|
||||
expect(consoleLogMock).toHaveBeenCalledWith(expect.stringMatching(/Checking page url/));
|
||||
})
|
||||
mockRegisterRouteChangeResponse();
|
||||
await formbricks.registerRouteChange();
|
||||
expect(consoleLogMock).toHaveBeenCalledWith(expect.stringMatching(/Checking page url/));
|
||||
});
|
||||
|
||||
test("Formbricks should logout", async () => {
|
||||
mockLogoutResponse()
|
||||
await formbricks.logout()
|
||||
const currentState = formbricks.getPerson()
|
||||
const currentStateAttributes: Array<Attribute> = currentState.attributes as Array<Attribute>;
|
||||
mockLogoutResponse();
|
||||
await formbricks.logout();
|
||||
const currentState = formbricks.getPerson();
|
||||
const currentStateAttributes: Array<Attribute> = currentState.attributes as Array<Attribute>;
|
||||
|
||||
expect(currentState.environmentId).toStrictEqual(environmentId)
|
||||
expect(currentStateAttributes.length).toBe(0)
|
||||
})
|
||||
expect(currentState.environmentId).toStrictEqual(environmentId);
|
||||
expect(currentStateAttributes.length).toBe(0);
|
||||
});
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
export interface Attribute {
|
||||
id: string;
|
||||
value: string;
|
||||
attributeClass: {
|
||||
id: string;
|
||||
value: string;
|
||||
attributeClass: {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
name: string;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user