Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
usavkov-epam committed Nov 26, 2024
1 parent e14ad1b commit ebc77ca
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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')),
]);

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);

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),
});
});
});

0 comments on commit ebc77ca

Please sign in to comment.