Skip to content

Commit

Permalink
fixup! ✨(frontend) add crisp chatbot
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoLC committed Sep 23, 2024
1 parent 87e82a5 commit 574c167
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { waitFor } from '@testing-library/react';
import { Crisp } from 'crisp-sdk-web';
import fetchMock from 'fetch-mock';

import { useAuthStore } from '../useAuthStore';

jest.mock('crisp-sdk-web', () => ({
...jest.requireActual('crisp-sdk-web'),
Crisp: {
setTokenId: jest.fn(),
user: {
setEmail: jest.fn(),
},
session: {
reset: jest.fn(),
},
},
}));

describe('useAuthStore', () => {
afterEach(() => {
jest.clearAllMocks();
fetchMock.restore();
});

it('checks initialize support session when initAuth', async () => {
window.$crisp = true;
fetchMock.mock('end:users/me/', {
id: '123456',
email: '[email protected]',
});

useAuthStore.getState().initAuth();

await waitFor(() => {
expect(Crisp.setTokenId).toHaveBeenCalledWith('123456');
});

expect(Crisp.user.setEmail).toHaveBeenCalledWith('[email protected]');
});

it('checks support session is terminated when logout', () => {
window.$crisp = true;
Object.defineProperty(window, 'location', {
value: {
...window.location,
replace: jest.fn(),
},
writable: true,
});

useAuthStore.getState().logout();

expect(Crisp.session.reset).toHaveBeenCalled();
});
});
30 changes: 30 additions & 0 deletions src/frontend/apps/impress/src/hook/__tests__/useSupport.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { renderHook } from '@testing-library/react';
import { Crisp } from 'crisp-sdk-web';

import { useSupport } from '../useSupport';

jest.mock('crisp-sdk-web', () => ({
...jest.requireActual('crisp-sdk-web'),
Crisp: {
configure: jest.fn(),
},
}));

describe('useSupport', () => {
afterEach(() => jest.clearAllMocks());

it('checks that env NEXT_PUBLIC_CRISP_WEBSITE_ID not set give a warning', () => {
process.env.NEXT_PUBLIC_CRISP_WEBSITE_ID = '';
jest.spyOn(console, 'warn').mockImplementation(() => {});

renderHook(() => useSupport());
expect(console.warn).toHaveBeenCalledWith('Crisp Website ID is not set');
});

it('checks Crisp is configured', () => {
process.env.NEXT_PUBLIC_CRISP_WEBSITE_ID = '123456';
renderHook(() => useSupport());

expect(Crisp.configure).toHaveBeenCalledWith('123456');
});
});

0 comments on commit 574c167

Please sign in to comment.