From 065ff6dd3ae43beb4eb5118a1d2236c230f66124 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Tue, 19 Mar 2024 22:18:56 +0100 Subject: [PATCH] Add tests for refreshStats --- js/src/hooks/useMCProductStatistics.js | 2 +- js/src/hooks/useMCProductStatistics.test.js | 41 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/js/src/hooks/useMCProductStatistics.js b/js/src/hooks/useMCProductStatistics.js index 65dfab34a4..418e1f29b0 100644 --- a/js/src/hooks/useMCProductStatistics.js +++ b/js/src/hooks/useMCProductStatistics.js @@ -35,7 +35,7 @@ const useMCProductStatistics = () => { const refreshStats = async () => { await refreshProductStats(); - invalidateResolution( 'getMCProductStatistics', [] ); + invalidateResolution(); }; useEffect( () => { diff --git a/js/src/hooks/useMCProductStatistics.test.js b/js/src/hooks/useMCProductStatistics.test.js index 32448c2ef5..073f308612 100644 --- a/js/src/hooks/useMCProductStatistics.test.js +++ b/js/src/hooks/useMCProductStatistics.test.js @@ -10,10 +10,17 @@ import useMCProductStatistics from '.~/hooks/useMCProductStatistics'; import useCountdown from '.~/hooks/useCountdown'; import useAppSelectDispatch from '.~/hooks/useAppSelectDispatch'; import { useAppDispatch } from '.~/data'; +import useApiFetchCallback from '.~/hooks/useApiFetchCallback'; jest.mock( '.~/hooks/useAppSelectDispatch' ); jest.mock( '.~/hooks/useCountdown' ); jest.mock( '.~/data' ); +jest.mock( '.~/hooks/useApiFetchCallback', () => ( { + __esModule: true, + default: jest.fn().mockImplementation( () => { + return [ jest.fn(), null ]; + } ), +} ) ); describe( 'useMCProductStatistics', () => { const startCountdown = jest.fn(); @@ -139,4 +146,38 @@ describe( 'useMCProductStatistics', () => { ); } ); } ); + describe( 'Refresh the product statistics', () => { + beforeAll( () => { + jest.clearAllMocks(); + } ); + it( 'The user clicks the refresh button', async () => { + const refreshProductStats = jest.fn(); + useCountdown.mockImplementation( () => { + return { + second: 0, + callCount: 0, + startCountdown, + }; + } ); + + useAppSelectDispatch.mockImplementation( () => { + return { + hasFinishedResolution: true, + invalidateResolution, + data: statsData, + }; + } ); + + useApiFetchCallback.mockImplementation( () => { + return [ refreshProductStats, null ]; + } ); + + const { result } = renderHook( () => useMCProductStatistics() ); + + await result.current.refreshStats(); + + expect( refreshProductStats ).toHaveBeenCalledTimes( 1 ); + expect( invalidateResolution ).toHaveBeenCalledTimes( 1 ); + } ); + } ); } );