-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from InseeFr/feat/add-unit-test-for-service-w…
…orkers Feat/add unit test for service workers
- Loading branch information
Showing
12 changed files
with
982 additions
and
573 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { SynchronizeButton } from './SynchronizeButton'; | ||
import { SyncContext } from '../Sync/SyncContextProvider'; | ||
import { useNetworkOnline } from '../../utils/hooks/useOnline'; | ||
import D from '../../i18n/build-dictionary'; | ||
|
||
vi.mock('../../utils/hooks/useOnline', () => ({ | ||
useNetworkOnline: vi.fn(), | ||
})); | ||
|
||
vi.mock('@mui/material/Button', () => { | ||
return { | ||
default: ({ children, disabled, onClick }) => ( | ||
<button disabled={disabled} onClick={onClick}> | ||
{children} | ||
</button> | ||
), | ||
}; | ||
}); | ||
|
||
describe('SynchronizeButton', () => { | ||
const mockSyncFunction = vi.fn(); | ||
|
||
beforeEach(() => { | ||
mockSyncFunction.mockClear(); | ||
}); | ||
|
||
it('renders the button with correct text', () => { | ||
useNetworkOnline.mockReturnValue(true); | ||
render( | ||
<SyncContext.Provider value={{ syncFunction: mockSyncFunction }}> | ||
<SynchronizeButton /> | ||
</SyncContext.Provider> | ||
); | ||
|
||
screen.getByRole('button', { name: D.synchronizeButton }); | ||
}); | ||
|
||
it('disables the button when offline', () => { | ||
useNetworkOnline.mockReturnValue(false); | ||
render( | ||
<SyncContext.Provider value={{ syncFunction: mockSyncFunction }}> | ||
<SynchronizeButton /> | ||
</SyncContext.Provider> | ||
); | ||
|
||
screen.getByRole('button', { name: D.synchronizeButton, disabled: true }); | ||
}); | ||
|
||
it('calls syncFunction when clicked', () => { | ||
useNetworkOnline.mockReturnValue(true); | ||
render( | ||
<SyncContext.Provider value={{ syncFunction: mockSyncFunction }}> | ||
<SynchronizeButton /> | ||
</SyncContext.Provider> | ||
); | ||
|
||
fireEvent.click(screen.getByRole('button')); | ||
expect(mockSyncFunction).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { describe, it, beforeEach, expect, vi } from 'vitest'; | ||
import { ServiceWorkerStatus } from './ServiceWorkerStatus'; | ||
import { useServiceWorker } from '../utils/hooks/useServiceWorker'; | ||
import D from '../i18n/build-dictionary'; | ||
|
||
vi.mock('../utils/hooks/useServiceWorker'); | ||
|
||
describe('ServiceWorkerStatus', () => { | ||
beforeEach(() => { | ||
useServiceWorker.mockReturnValue({ | ||
isUpdating: false, | ||
isUpdateInstalled: false, | ||
isUpdateAvailable: false, | ||
isServiceWorkerInstalled: false, | ||
isInstallingServiceWorker: false, | ||
isInstallationFailed: false, | ||
clearUpdating: vi.fn(), | ||
updateApp: vi.fn(), | ||
}); | ||
}); | ||
|
||
it(`Display ${D.updating}`, () => { | ||
useServiceWorker.mockReturnValueOnce({ | ||
isServiceWorkerInstalled: false, | ||
isUpdateInstalled: false, | ||
isUpdateAvailable: false, | ||
isUpdating: true, | ||
isInstallingServiceWorker: false, | ||
isInstallationFailed: false, | ||
clearUpdating: vi.fn(), | ||
updateApp: vi.fn(), | ||
}); | ||
|
||
render(<ServiceWorkerStatus authenticated={true} />); | ||
screen.getByText(D.updating); | ||
expect(screen.queryByText(D.updateNow)).toBeNull(); | ||
}); | ||
|
||
it(`Display ${D.updateInstalled}`, () => { | ||
useServiceWorker.mockReturnValueOnce({ | ||
isServiceWorkerInstalled: false, | ||
isUpdateInstalled: true, | ||
isUpdateAvailable: false, | ||
isUpdating: false, | ||
isInstallingServiceWorker: false, | ||
isInstallationFailed: false, | ||
clearUpdating: vi.fn(), | ||
updateApp: vi.fn(), | ||
}); | ||
|
||
render(<ServiceWorkerStatus authenticated={true} />); | ||
screen.getByText(D.updateInstalled); | ||
expect(screen.queryByText(D.updateNow)).toBeNull(); | ||
}); | ||
|
||
it(`Display ${D.updateAvailable}`, () => { | ||
const updateApp = vi.fn(); | ||
useServiceWorker.mockReturnValueOnce({ | ||
isServiceWorkerInstalled: false, | ||
isUpdateInstalled: false, | ||
isUpdateAvailable: true, | ||
isUpdating: false, | ||
isInstallingServiceWorker: false, | ||
isInstallationFailed: false, | ||
clearUpdating: vi.fn(), | ||
updateApp, | ||
}); | ||
|
||
render(<ServiceWorkerStatus authenticated={true} />); | ||
screen.getByText(D.updateAvailable); | ||
const button = screen.getByText(D.updateNow); | ||
fireEvent.click(button); | ||
expect(updateApp).toHaveBeenCalled(); | ||
}); | ||
|
||
it(`Display ${D.appReadyOffline}`, () => { | ||
useServiceWorker.mockReturnValueOnce({ | ||
isServiceWorkerInstalled: true, | ||
isUpdateInstalled: false, | ||
isUpdateAvailable: false, | ||
isUpdating: false, | ||
isInstallingServiceWorker: false, | ||
isInstallationFailed: false, | ||
clearUpdating: vi.fn(), | ||
updateApp: vi.fn(), | ||
}); | ||
|
||
render(<ServiceWorkerStatus authenticated={true} />); | ||
screen.getByText(D.appReadyOffline); | ||
expect(screen.queryByText(D.updateNow)).toBeNull(); | ||
}); | ||
|
||
it(`Display ${D.appInstalling}`, () => { | ||
useServiceWorker.mockReturnValueOnce({ | ||
isServiceWorkerInstalled: false, | ||
isUpdateInstalled: false, | ||
isUpdateAvailable: false, | ||
isUpdating: false, | ||
isInstallingServiceWorker: true, | ||
isInstallationFailed: false, | ||
clearUpdating: vi.fn(), | ||
updateApp: vi.fn(), | ||
}); | ||
|
||
render(<ServiceWorkerStatus authenticated={true} />); | ||
screen.getByText(D.appInstalling); | ||
expect(screen.queryByText(D.updateNow)).toBeNull(); | ||
}); | ||
|
||
it(`Display ${D.installError}`, () => { | ||
useServiceWorker.mockReturnValueOnce({ | ||
isServiceWorkerInstalled: false, | ||
isUpdateInstalled: false, | ||
isUpdateAvailable: false, | ||
isUpdating: false, | ||
isInstallingServiceWorker: false, | ||
isInstallationFailed: true, | ||
clearUpdating: vi.fn(), | ||
updateApp: vi.fn(), | ||
}); | ||
|
||
render(<ServiceWorkerStatus authenticated={true} />); | ||
screen.getByText(D.installError); | ||
expect(screen.queryByText(D.updateNow)).toBeNull(); | ||
}); | ||
|
||
it('should close the Snackbar', () => { | ||
useServiceWorker.mockReturnValueOnce({ | ||
isServiceWorkerInstalled: false, | ||
isUpdateInstalled: false, | ||
isUpdateAvailable: false, | ||
isUpdating: false, | ||
isInstallingServiceWorker: false, | ||
isInstallationFailed: true, | ||
clearUpdating: vi.fn(), | ||
updateApp: vi.fn(), | ||
}); | ||
|
||
render(<ServiceWorkerStatus authenticated={true} />); | ||
screen.getByText(D.installError); | ||
|
||
const closeButton = screen.getByRole('button', { name: /close/i }); | ||
fireEvent.click(closeButton); | ||
expect(screen.queryByText(D.installError)).toBeNull(); | ||
}); | ||
|
||
it('should close the Snackbar and call clearUpdating', () => { | ||
const clearUpdating = vi.fn(); | ||
useServiceWorker.mockReturnValueOnce({ | ||
isServiceWorkerInstalled: false, | ||
isUpdateInstalled: true, | ||
isUpdateAvailable: false, | ||
isUpdating: false, | ||
isInstallingServiceWorker: false, | ||
isInstallationFailed: false, | ||
clearUpdating, | ||
updateApp: vi.fn(), | ||
}); | ||
|
||
render(<ServiceWorkerStatus authenticated={true} />); | ||
screen.getByText(D.updateInstalled); | ||
|
||
const closeButton = screen.getByRole('button', { name: /close/i }); | ||
fireEvent.click(closeButton); | ||
expect(screen.queryByText(D.updateInstalled)).toBeNull(); | ||
expect(clearUpdating).toHaveBeenCalled(); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import { addListener } from './dom'; | ||
|
||
describe('addListener', () => { | ||
it('should add an event listener to the target', () => { | ||
const target = document.createElement('div'); | ||
const listener = vi.fn(); | ||
|
||
const cleanup = addListener(target, 'click', listener); | ||
|
||
target.dispatchEvent(new Event('click')); | ||
|
||
expect(listener).toHaveBeenCalledTimes(1); | ||
|
||
cleanup(); | ||
|
||
target.dispatchEvent(new Event('click')); | ||
|
||
expect(listener).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should work with window as target', () => { | ||
const listener = vi.fn(); | ||
|
||
const cleanup = addListener(window, 'resize', listener); | ||
|
||
window.dispatchEvent(new Event('resize')); | ||
|
||
expect(listener).toHaveBeenCalledTimes(1); | ||
|
||
cleanup(); | ||
|
||
window.dispatchEvent(new Event('resize')); | ||
|
||
expect(listener).toHaveBeenCalledTimes(1); // L'écouteur ne doit pas être appelé à nouveau | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Add a listener to the target while returning a cleaning function | ||
*/ | ||
export function addListener(target: HTMLElement | Window, event: string, listener: () => void) { | ||
target.addEventListener(event, listener); | ||
return () => { | ||
target.removeEventListener(event, listener); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.