From ba0d37c2435130121225b85c1b1f6d1ef3633c97 Mon Sep 17 00:00:00 2001 From: Chris Fang Date: Wed, 20 Nov 2024 16:00:33 -0800 Subject: [PATCH] test(storage-browser): Add unit tests --- .../store/__tests__/useStore.spec.ts | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 packages/react-storage/src/components/StorageBrowser/providers/store/__tests__/useStore.spec.ts diff --git a/packages/react-storage/src/components/StorageBrowser/providers/store/__tests__/useStore.spec.ts b/packages/react-storage/src/components/StorageBrowser/providers/store/__tests__/useStore.spec.ts new file mode 100644 index 00000000000..f76d07891b3 --- /dev/null +++ b/packages/react-storage/src/components/StorageBrowser/providers/store/__tests__/useStore.spec.ts @@ -0,0 +1,119 @@ +import { renderHook } from '@testing-library/react'; +import { useActionType } from '../actionType'; +import { useFiles } from '../files'; +import { useLocation } from '../location'; +import { useLocationItems } from '../locationItems'; +import { HandleStoreAction, useStore } from '../useStore'; +import { FileItem } from '../../../actions'; + +jest.mock('../../../controls/context'); +jest.mock('../actionType'); +jest.mock('../files'); +jest.mock('../location'); +jest.mock('../locationItems'); + +describe('useStore', () => { + const locationData = { + bucket: 'bucket', + id: 'id', + permissions: ['write' as const], + prefix: '', + type: 'PREFIX' as const, + }; + const actionTypeState = 'action-type'; + const filesState: FileItem[] = []; + const locationState = { current: locationData, path: '', key: '' }; + const locationItemsState = { fileDataItems: [] }; + + const mockUseActionType = jest.mocked(useActionType); + const mocktUseFiles = jest.mocked(useFiles); + const mocktUseLocation = jest.mocked(useLocation); + const mocktUseLocationItems = jest.mocked(useLocationItems); + const mockDispatchActionType = jest.fn(); + const mockDispatchFilesAction = jest.fn(); + const mockDispatchLocationAction = jest.fn(); + const mockDispatchLocationItemsAction = jest.fn(); + + const expectActions = (actions: Parameters[number][]) => { + const { result } = renderHook(() => useStore()); + const [, dispatchStoreAction] = result.current; + return { + toHaveBeenDispatchedBy: (dispatcher: HandleStoreAction) => { + actions.forEach((action, index) => { + // dispatch each action as a store action + dispatchStoreAction(action); + // assert that that the correct sub-dispatcher was invoked + expect(dispatcher).toHaveBeenNthCalledWith(index + 1, action); + }); + }, + }; + }; + + beforeEach(() => { + mockUseActionType.mockReturnValue([ + actionTypeState, + mockDispatchActionType, + ]); + mocktUseFiles.mockReturnValue([filesState, mockDispatchFilesAction]); + mocktUseLocation.mockReturnValue([ + locationState, + mockDispatchLocationAction, + ]); + mocktUseLocationItems.mockReturnValue([ + locationItemsState, + mockDispatchLocationItemsAction, + ]); + }); + + afterEach(() => { + mockUseActionType.mockClear(); + mocktUseFiles.mockClear(); + mocktUseLocation.mockClear(); + mocktUseLocationItems.mockClear(); + }); + + it('returns store state', () => { + const { result } = renderHook(() => useStore()); + + expect(result.current).toStrictEqual([ + { + actionType: actionTypeState, + files: filesState, + location: locationState, + locationItems: locationItemsState, + }, + expect.any(Function), + ]); + }); + + it('dispatches actionType action', () => { + expectActions([ + { type: 'SET_ACTION_TYPE', actionType: 'new-action-type' }, + { type: 'RESET_ACTION_TYPE' }, + ]).toHaveBeenDispatchedBy(mockDispatchActionType); + }); + + it('dispatches files action', () => { + expectActions([ + { type: 'ADD_FILE_ITEMS' }, + { type: 'REMOVE_FILE_ITEM', id: 'file-id' }, + { type: 'SELECT_FILES' }, + { type: 'RESET_FILE_ITEMS' }, + ]).toHaveBeenDispatchedBy(mockDispatchFilesAction); + }); + + it('dispatches location action', () => { + expectActions([ + { type: 'NAVIGATE', location: locationData }, + { type: 'RESET_LOCATION' }, + ]).toHaveBeenDispatchedBy(mockDispatchLocationAction); + }); + + it('dispatches locationItems action', () => { + expectActions([ + { type: 'SET_LOCATION_ITEMS' }, + { type: 'REMOVE_LOCATION_ITEM', id: 'file-id' }, + { type: 'RESET_LOCATION_ITEMS' }, + ]).toHaveBeenDispatchedBy(mockDispatchLocationItemsAction); + }); +});