From ebc77caa175dcac86bbee77478c5bd171de5c141 Mon Sep 17 00:00:00 2001 From: Yury Saukou Date: Tue, 26 Nov 2024 18:04:38 +0400 Subject: [PATCH] add tests --- .../useSelectedOrganizationVersion.js | 18 ++-- .../useSelectedOrganizationVersion.test.js | 89 +++++++++++++++++++ 2 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 src/Organizations/OrganizationVersion/hooks/useSelectedOrganizationVersion/useSelectedOrganizationVersion.test.js diff --git a/src/Organizations/OrganizationVersion/hooks/useSelectedOrganizationVersion/useSelectedOrganizationVersion.js b/src/Organizations/OrganizationVersion/hooks/useSelectedOrganizationVersion/useSelectedOrganizationVersion.js index e9ec136d..74647f5d 100644 --- a/src/Organizations/OrganizationVersion/hooks/useSelectedOrganizationVersion/useSelectedOrganizationVersion.js +++ b/src/Organizations/OrganizationVersion/hooks/useSelectedOrganizationVersion/useSelectedOrganizationVersion.js @@ -75,9 +75,9 @@ export const useSelectedOrganizationVersion = ({ versionId, versions, snapshotPa const { isLoading: isVersionDataLoading, data = {}, - } = useQuery( - [namespace, versionId, versionSnapshot?.id], - async () => { + } = useQuery({ + queryKey: [namespace, versionId, versionSnapshot?.id], + queryFn: async () => { const acqUnitsIds = [ ...get('acqUnitIds', versionSnapshot, []), ...flow( @@ -87,9 +87,7 @@ export const useSelectedOrganizationVersion = ({ versionId, versions, snapshotPa )(versionSnapshot), ]; - const [ - acqUnitsMap, - ] = await Promise.all([ + const [acqUnitsMap] = await Promise.all([ fetchAcqUnitsByIds(ky)(acqUnitsIds).then(keyBy('id')), ]); @@ -111,11 +109,9 @@ export const useSelectedOrganizationVersion = ({ versionId, versions, snapshotPa metadata, }; }, - { - enabled: Boolean(versionId && organization?.id), - ...options, - }, - ); + enabled: Boolean(versionId && organization?.id), + ...options, + }); const selectedVersion = useMemo(() => { const versionUsersMap = keyBy('id', users); diff --git a/src/Organizations/OrganizationVersion/hooks/useSelectedOrganizationVersion/useSelectedOrganizationVersion.test.js b/src/Organizations/OrganizationVersion/hooks/useSelectedOrganizationVersion/useSelectedOrganizationVersion.test.js new file mode 100644 index 00000000..752aff62 --- /dev/null +++ b/src/Organizations/OrganizationVersion/hooks/useSelectedOrganizationVersion/useSelectedOrganizationVersion.test.js @@ -0,0 +1,89 @@ +import get from 'lodash/get'; +import { + QueryClient, + QueryClientProvider, +} from 'react-query'; + +import { + renderHook, + waitFor, +} from '@folio/jest-config-stripes/testing-library/react'; +import { + fetchAcqUnitsByIds, + getVersionMetadata, + useOrganization, + useUsersBatch, +} from '@folio/stripes-acq-components'; + +import { organizationAuditEvent } from 'fixtures'; +import { + useContactsByIds, + useInterfacesByIds, +} from '../../../../common/hooks'; +import { useSelectedOrganizationVersion } from './useSelectedOrganizationVersion'; + +jest.mock('@folio/stripes-acq-components', () => ({ + ...jest.requireActual('@folio/stripes-acq-components'), + fetchAcqUnitsByIds: jest.fn(), + getVersionMetadata: jest.fn(), + useOrganization: jest.fn(), + useUsersBatch: jest.fn(), +})); +jest.mock('../../../../common/hooks', () => ({ + ...jest.requireActual('../../../../common/hooks'), + useContactsByIds: jest.fn(), + useInterfacesByIds: jest.fn(), +})); + +const versionId = organizationAuditEvent.id; +const versions = [organizationAuditEvent]; +const snapshotPath = 'organizationSnapshot.map'; + +const contacts = [{ id: 'contact1' }]; +const interfaces = [{ id: 'interface1' }]; +const organization = { id: 'org1' }; +const users = [{ id: 'user1', personal: { firstName: 'John', lastName: 'Doe' } }]; + +const queryClient = new QueryClient(); +const wrapper = ({ children }) => ( + + {children} + +); + +describe('useSelectedOrganizationVersion', () => { + beforeEach(() => { + fetchAcqUnitsByIds.mockReturnValue(() => Promise.resolve([{ id: 'acq-unit-id' }])); + useOrganization.mockReturnValue({ organization, isLoading: false }); + useUsersBatch.mockReturnValue({ users, isLoading: false }); + useContactsByIds.mockReturnValue({ contacts, isLoading: false }); + useInterfacesByIds.mockReturnValue({ interfaces, isLoading: false }); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should return selected version data', async () => { + const { result } = renderHook( + () => useSelectedOrganizationVersion({ versionId, versions, snapshotPath }), + { wrapper }, + ); + + await waitFor(() => expect(result.current.isLoading).toBeFalsy()); + + expect(result.current.selectedVersion).toEqual({ + ...get(organizationAuditEvent, snapshotPath), + accounts: get(organizationAuditEvent, `${snapshotPath}.accounts`).map(acc => ({ + ...acc, + acqUnits: [], + })), + acqUnits: '', + alternativeNames: 'Amazon', + contactsList: contacts, + interfacesList: interfaces, + vendorCurrenciesValue: 'US Dollar (USD)', + metadata: getVersionMetadata(organizationAuditEvent, organization), + }); + }); +});