|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import { useSearchParams } from 'react-router-dom'; |
| 3 | +import { useSaveRecord } from '@common/hooks/useSaveRecord'; |
| 4 | +import { useStatusStore } from '@src/store'; |
| 5 | +import { setInitialGlobalState } from '@src/test/__mocks__/store'; |
| 6 | + |
| 7 | +const mockSaveRecord = jest.fn(); |
| 8 | +const mockOpenModal = jest.fn(); |
| 9 | +const mockCloseModal = jest.fn(); |
| 10 | + |
| 11 | +jest.mock('react-router-dom', () => ({ |
| 12 | + useSearchParams: jest.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('@common/hooks/useRecordStatus', () => ({ |
| 16 | + useRecordStatus: () => ({ |
| 17 | + hasBeenSaved: false, |
| 18 | + }), |
| 19 | +})); |
| 20 | + |
| 21 | +jest.mock('@common/hooks/useRecordControls', () => ({ |
| 22 | + useRecordControls: () => ({ |
| 23 | + saveRecord: mockSaveRecord, |
| 24 | + }), |
| 25 | +})); |
| 26 | + |
| 27 | +jest.mock('@common/hooks/useModalControls', () => ({ |
| 28 | + useModalControls: () => ({ |
| 29 | + isModalOpen: false, |
| 30 | + openModal: mockOpenModal, |
| 31 | + closeModal: mockCloseModal, |
| 32 | + }), |
| 33 | +})); |
| 34 | + |
| 35 | +describe('useSaveRecord', () => { |
| 36 | + const renderUseSaveRecordHook = (isRecordEdited = false, searchParamsCloneOf?: string) => { |
| 37 | + (useSearchParams as jest.Mock).mockReturnValue([{ get: () => searchParamsCloneOf }]); |
| 38 | + |
| 39 | + setInitialGlobalState([ |
| 40 | + { |
| 41 | + store: useStatusStore, |
| 42 | + state: { isRecordEdited }, |
| 43 | + }, |
| 44 | + ]); |
| 45 | + |
| 46 | + return renderHook(() => useSaveRecord(true)); |
| 47 | + }; |
| 48 | + |
| 49 | + test('isButtonDisabled is false when record is edited', () => { |
| 50 | + const { result } = renderUseSaveRecordHook(true); |
| 51 | + |
| 52 | + expect(result.current.isButtonDisabled).toBeFalsy(); |
| 53 | + }); |
| 54 | + |
| 55 | + test('isButtonDisabled is false when cloning record', () => { |
| 56 | + const { result } = renderUseSaveRecordHook(false, 'cloneOfId'); |
| 57 | + |
| 58 | + expect(result.current.isButtonDisabled).toBeFalsy(); |
| 59 | + }); |
| 60 | + |
| 61 | + test('saveRecord calls useRecordControls.saveRecord with correct params', () => { |
| 62 | + const { result } = renderUseSaveRecordHook(true); |
| 63 | + |
| 64 | + result.current.saveRecord(); |
| 65 | + |
| 66 | + expect(mockSaveRecord).toHaveBeenCalledWith({ isNavigatingBack: true }); |
| 67 | + }); |
| 68 | + |
| 69 | + test('modal controls are exposed correctly', () => { |
| 70 | + const { result } = renderUseSaveRecordHook(); |
| 71 | + |
| 72 | + result.current.openModal(); |
| 73 | + expect(mockOpenModal).toHaveBeenCalled(); |
| 74 | + |
| 75 | + result.current.closeModal(); |
| 76 | + expect(mockCloseModal).toHaveBeenCalled(); |
| 77 | + }); |
| 78 | +}); |
0 commit comments