From e511b25c8d69f29eb9e30bb6c6a2a28110879bb4 Mon Sep 17 00:00:00 2001 From: Oleksandr Hladchenko <85172747+OleksandrHladchenko1@users.noreply.github.com> Date: Wed, 10 Jan 2024 15:19:01 +0200 Subject: [PATCH] UIPFIMP-64: Jest/RTL: Cover utils with unit tests (#151) * UIPFIMP-64: Cover fetchAssociations.js with unit tests * UIPFIMP-64: Cover fetchAssociations.js with unit tests * UIPFIMP-64: Remove faker from the code --- CHANGELOG.md | 2 + .../utils/fetchAssociations.test.js | 66 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/FindImportProfile/utils/fetchAssociations.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 2836ff8..83bf080 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,9 @@ ## **7.1.0** (in progress) ### Features added: + * Jest/RTL: Increase test coverage for FindImportProfile component (UIPFIMP-65) +* Jest/RTL: Cover utils with unit tests (UIPFIMP-64) ## [7.0.0](https://github.com/folio-org/ui-plugin-find-import-profile/tree/v7.0.0) (2023-10-13) diff --git a/src/FindImportProfile/utils/fetchAssociations.test.js b/src/FindImportProfile/utils/fetchAssociations.test.js new file mode 100644 index 0000000..9eadf30 --- /dev/null +++ b/src/FindImportProfile/utils/fetchAssociations.test.js @@ -0,0 +1,66 @@ +import '../../../test/jest/__mock__'; + +import { fetchAssociations } from './fetchAssociations'; + +global.fetch = jest.fn(); + +const mockData = { name: 'test name' }; +const profileId = 'testProfileId'; +const okapi = { + url: 'https://test.com', + tenant: 'tenant', + token: 'token', +}; +const path = `${okapi.url}/data-import-profiles/profileAssociations/${profileId}`; + +describe('fetchAssociations function', () => { + beforeEach(() => { + global.fetch.mockClear(); + }); + + describe('when masterType is equal to parentType', () => { + it('should fetch details for the entity', async () => { + global.fetch.mockResolvedValueOnce({ + ok: true, + status: 200, + json: async () => mockData, + }); + + const expectedUrl = `${path}/masters?detailType=JOB_PROFILE`; + const data = await fetchAssociations(okapi, profileId, 'ACTION_PROFILE', 'ACTION_PROFILE', 'jobProfiles'); + + expect(global.fetch.mock.calls[0][0]).toBe(expectedUrl); + expect(data).toEqual(mockData); + }); + }); + + describe('when masterType is not equal to parentType', () => { + it('should fetch details for the master type', async () => { + global.fetch.mockResolvedValueOnce({ + ok: true, + status: 200, + json: async () => mockData, + }); + + const expectedUrl = `${path}/details?masterType=ACTION_PROFILE`; + const data = await fetchAssociations(okapi, profileId, 'JOB_PROFILE', 'ACTION_PROFILE', 'actionProfiles'); + + expect(global.fetch.mock.calls[0][0]).toBe(expectedUrl); + expect(data).toEqual(mockData); + }); + }); + + describe('when there is no data', () => { + it('should return an empty object', async () => { + global.fetch.mockResolvedValueOnce({ + ok: false, + status: 400, + json: async () => null, + }); + + const data = await fetchAssociations(okapi, profileId, 'test', 'test', 'jobProfiles'); + + expect(data).toEqual({}); + }); + }); +});