From db0b67396508c36fcf4d1620b5a23196ddb50fd4 Mon Sep 17 00:00:00 2001 From: Matti Nannt Date: Mon, 3 Jul 2023 11:37:08 +0200 Subject: [PATCH] Fix formatting issues (#471) --- .github/ISSUE_TEMPLATE/feature_request.md | 1 + .../settings/members/EditMemberships.tsx | 6 +- .../teams/[teamId]/invite/[inviteId]/index.ts | 1 - packages/js/src/lib/noCodeEvents.ts | 4 +- packages/js/tests/__mocks__/apiMock.ts | 490 ++++++++++-------- packages/js/tests/constants.ts | 92 ++-- packages/js/tests/index.test.ts | 274 +++++----- packages/js/tests/types.ts | 10 +- 8 files changed, 465 insertions(+), 413 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index 4070d3724a..402b8d37de 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -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` diff --git a/apps/web/app/environments/[environmentId]/settings/members/EditMemberships.tsx b/apps/web/app/environments/[environmentId]/settings/members/EditMemberships.tsx index 1430718c49..db68fc2ca0 100644 --- a/apps/web/app/environments/[environmentId]/settings/members/EditMemberships.tsx +++ b/apps/web/app/environments/[environmentId]/settings/members/EditMemberships.tsx @@ -202,7 +202,7 @@ export function EditMemberships({ environmentId }: EditMembershipsProps) { )}
-
+
Fullname
Email
@@ -212,9 +212,9 @@ export function EditMemberships({ environmentId }: EditMembershipsProps) {
{[...team.members, ...team.invitees].map((member) => (
-
+
diff --git a/apps/web/pages/api/v1/teams/[teamId]/invite/[inviteId]/index.ts b/apps/web/pages/api/v1/teams/[teamId]/invite/[inviteId]/index.ts index e2180e4e32..1b417daeba 100644 --- a/apps/web/pages/api/v1/teams/[teamId]/invite/[inviteId]/index.ts +++ b/apps/web/pages/api/v1/teams/[teamId]/invite/[inviteId]/index.ts @@ -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, diff --git a/packages/js/src/lib/noCodeEvents.ts b/packages/js/src/lib/noCodeEvents.ts index a11757e348..06eea331b3 100644 --- a/packages/js/src/lib/noCodeEvents.ts +++ b/packages/js/src/lib/noCodeEvents.ts @@ -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); } diff --git a/packages/js/tests/__mocks__/apiMock.ts b/packages/js/tests/__mocks__/apiMock.ts index 4f6e24b4c0..193724d4d2 100644 --- a/packages/js/tests/__mocks__/apiMock.ts +++ b/packages/js/tests/__mocks__/apiMock.ts @@ -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'); -} \ No newline at end of file + 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"); +}; diff --git a/packages/js/tests/constants.ts b/packages/js/tests/constants.ts index ad83adad3f..83380b38c1 100644 --- a/packages/js/tests/constants.ts +++ b/packages/js/tests/constants.ts @@ -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; diff --git a/packages/js/tests/index.test.ts b/packages/js/tests/index.test.ts index d7018edf5e..789fb969bd 100644 --- a/packages/js/tests/index.test.ts +++ b/packages/js/tests/index.test.ts @@ -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 = currentState.attributes as Array; - expect(currentStateAttributes).toHaveLength(0) -}) + const currentStateAttributes: Array = currentState.attributes as Array; + 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 = currentState.attributes as Array; - const numberOfUserAttributes = currentStateAttributes.length - expect(numberOfUserAttributes).toStrictEqual(1) + const currentStateAttributes: Array = currentState.attributes as Array; + 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 = currentState.attributes as Array; - const numberOfUserAttributes = currentStateAttributes.length - expect(numberOfUserAttributes).toStrictEqual(2) + const currentStateAttributes: Array = currentState.attributes as Array; + 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 = currentState.attributes as Array; - const numberOfUserAttributes = currentStateAttributes.length - expect(numberOfUserAttributes).toStrictEqual(3) + const currentStateAttributes: Array = currentState.attributes as Array; + 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 = currentState.attributes as Array; + const currentStateAttributes: Array = currentState.attributes as Array; - 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 = currentState.attributes as Array; + mockLogoutResponse(); + await formbricks.logout(); + const currentState = formbricks.getPerson(); + const currentStateAttributes: Array = currentState.attributes as Array; - expect(currentState.environmentId).toStrictEqual(environmentId) - expect(currentStateAttributes.length).toBe(0) -}) + expect(currentState.environmentId).toStrictEqual(environmentId); + expect(currentStateAttributes.length).toBe(0); +}); diff --git a/packages/js/tests/types.ts b/packages/js/tests/types.ts index 933d50388d..f434d0d369 100644 --- a/packages/js/tests/types.ts +++ b/packages/js/tests/types.ts @@ -1,8 +1,8 @@ export interface Attribute { + id: string; + value: string; + attributeClass: { id: string; - value: string; - attributeClass: { - id: string; - name: string; - }; + name: string; + }; }