Skip to content

Commit

Permalink
Add tests for refreshStats
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemd24 committed Mar 19, 2024
1 parent c213955 commit 065ff6d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion js/src/hooks/useMCProductStatistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const useMCProductStatistics = () => {

const refreshStats = async () => {
await refreshProductStats();
invalidateResolution( 'getMCProductStatistics', [] );
invalidateResolution();
};

useEffect( () => {
Expand Down
41 changes: 41 additions & 0 deletions js/src/hooks/useMCProductStatistics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 );
} );
} );
} );

0 comments on commit 065ff6d

Please sign in to comment.