-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(storage-browser): Add unit tests
- Loading branch information
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
...es/react-storage/src/components/StorageBrowser/providers/store/__tests__/useStore.spec.ts
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,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<HandleStoreAction>[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); | ||
}); | ||
}); |