From 4fa2f2af74e349473b9291990332370a08343a6d Mon Sep 17 00:00:00 2001 From: Oleksandr Hladchenko1 Date: Fri, 5 Jan 2024 15:15:14 +0200 Subject: [PATCH 1/3] UIPFIMP-64: Cover fetchAssociations.js with unit tests --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03f12eb..6e49978 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change history for ui-plugin-find-import-profile +## **7.1.0** (in progress) + +### Features added: +* 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) ### Features added: From 95d15dd4385fdc8a6d5cb4f8ecab73ca9bc87253 Mon Sep 17 00:00:00 2001 From: Oleksandr Hladchenko1 Date: Fri, 5 Jan 2024 15:15:46 +0200 Subject: [PATCH 2/3] UIPFIMP-64: Cover fetchAssociations.js with unit tests --- .../utils/fetchAssociations.test.js | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/FindImportProfile/utils/fetchAssociations.test.js diff --git a/src/FindImportProfile/utils/fetchAssociations.test.js b/src/FindImportProfile/utils/fetchAssociations.test.js new file mode 100644 index 0000000..293c038 --- /dev/null +++ b/src/FindImportProfile/utils/fetchAssociations.test.js @@ -0,0 +1,68 @@ +import faker from 'faker'; + +import '../../../test/jest/__mock__'; + +import { fetchAssociations } from './fetchAssociations'; + +global.fetch = jest.fn(); + +const mockData = { name: 'test name' }; +const profileId = faker.random.uuid(); +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({}); + }); + }); +}); From 8e30ff75e95937670010756efae2aa1a191bbb27 Mon Sep 17 00:00:00 2001 From: Oleksandr Hladchenko1 Date: Tue, 9 Jan 2024 14:28:14 +0200 Subject: [PATCH 3/3] UIPFIMP-64: Remove faker from the code --- src/FindImportProfile/utils/fetchAssociations.test.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/FindImportProfile/utils/fetchAssociations.test.js b/src/FindImportProfile/utils/fetchAssociations.test.js index 293c038..9eadf30 100644 --- a/src/FindImportProfile/utils/fetchAssociations.test.js +++ b/src/FindImportProfile/utils/fetchAssociations.test.js @@ -1,5 +1,3 @@ -import faker from 'faker'; - import '../../../test/jest/__mock__'; import { fetchAssociations } from './fetchAssociations'; @@ -7,7 +5,7 @@ import { fetchAssociations } from './fetchAssociations'; global.fetch = jest.fn(); const mockData = { name: 'test name' }; -const profileId = faker.random.uuid(); +const profileId = 'testProfileId'; const okapi = { url: 'https://test.com', tenant: 'tenant',