From 8bb2d1d54ef619ce8a8b6cf72a395bac2de52564 Mon Sep 17 00:00:00 2001 From: Roy Scheeren Date: Wed, 31 Jan 2024 14:21:09 +0100 Subject: [PATCH] test(envited.ascs.digital): refactor useNotification test Signed-off-by: Roy Scheeren --- .../useNotification.hook.test.ts | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/apps/envited.ascs.digital/common/notifications/useNotification.hook.test.ts b/apps/envited.ascs.digital/common/notifications/useNotification.hook.test.ts index 62e750d8..e966c8ab 100644 --- a/apps/envited.ascs.digital/common/notifications/useNotification.hook.test.ts +++ b/apps/envited.ascs.digital/common/notifications/useNotification.hook.test.ts @@ -1,61 +1,63 @@ -/** - * @jest-environment node - */ -import { toast } from 'react-toastify' - import { notification } from './useNotification.hook' -jest.mock('react-toastify', () => ({ - toast: { - info: jest.fn().mockImplementation(x => x), - success: jest.fn().mockImplementation(x => x), - error: jest.fn().mockImplementation(x => x), - warning: jest.fn().mockImplementation(x => x), - }, -})) - describe('common/notification', () => { describe('info', () => { - it('should return as expected', async () => { + it('should return an info toast expected', async () => { + // when ... we want to show a info notification + // then ... it should show the info notification as expected + const toast = { + info: jest.fn().mockReturnValue('INFO_TOAST'), + } as any + const { info } = notification(toast)() const result = info('INFO_TOAST') - - // when ... we want to get the notification - // then ... it returns the content as expected expect(result).toEqual('INFO_TOAST') + expect(toast.info).toHaveBeenCalledWith('INFO_TOAST') }) }) describe('success', () => { - it('should return as expected', async () => { + it('should return a success toast as expected', async () => { + // when ... we want to show a success notification + // then ... it should show the success notification as expected + const toast = { + success: jest.fn().mockReturnValue('SUCCESS_TOAST'), + } as any + const { success } = notification(toast)() const result = success('SUCCESS_TOAST') - - // when ... we want to get the notification - // then ... it returns the content as expected expect(result).toEqual('SUCCESS_TOAST') + expect(toast.success).toHaveBeenCalledWith('SUCCESS_TOAST') }) }) describe('warning', () => { - it('should return as expected', async () => { + it('should return a warning toast as expected', async () => { + // when ... we want to show a warning notification + // then ... it should show the warning notification as expected + const toast = { + warning: jest.fn().mockReturnValue('WARNING_TOAST'), + } as any + const { warning } = notification(toast)() const result = warning('WARNING_TOAST') - - // when ... we want to get the notification - // then ... it returns the content as expected expect(result).toEqual('WARNING_TOAST') + expect(toast.warning).toHaveBeenCalledWith('WARNING_TOAST') }) }) describe('error', () => { - it('should return as expected', async () => { + it('should return a error toast', async () => { + // when ... we want to show an error notification + // then ... it should show the error notification as expected + const toast = { + error: jest.fn().mockReturnValue('ERROR_TOAST'), + } as any + const { error } = notification(toast)() const result = error('ERROR_TOAST') - - // when ... we want to get the notification - // then ... it returns the content as expected expect(result).toEqual('ERROR_TOAST') + expect(toast.error).toHaveBeenCalledWith('ERROR_TOAST') }) }) })