From 6330c4972196ddce63b74049bd7b33478c77179c Mon Sep 17 00:00:00 2001 From: Mariia_Aloshyna Date: Wed, 11 Oct 2023 18:37:09 +0300 Subject: [PATCH 1/3] STCOR-747: Provide optional tenant argument to `useOkapiKy` hook --- CHANGELOG.md | 1 + src/useOkapiKy.js | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fac75302..259285466 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * *BREAKING* bump `react-intl` to `v6.4.4`. Refs STCOR-744. * Bump `stylelint` to `v15` and `stylelint-config-standard` to `v34`. Refs STCOR-745. * Read ky timeout from stripes-config value. Refs STCOR-594. +* Provide optional tenant argument to `useOkapiKy` hook. Refs STCOR-747. ## [9.0.0](https://github.com/folio-org/stripes-core/tree/v9.0.0) (2023-01-30) [Full Changelog](https://github.com/folio-org/stripes-core/compare/v8.3.0...v9.0.0) diff --git a/src/useOkapiKy.js b/src/useOkapiKy.js index c0bcda1c8..669ba7d6f 100644 --- a/src/useOkapiKy.js +++ b/src/useOkapiKy.js @@ -1,8 +1,8 @@ import ky from 'ky'; import { useStripes } from './StripesContext'; -export default () => { - const { locale = 'en', timeout = 30000, tenant, token, url } = useStripes().okapi; +export default (tenant) => { + const { locale = 'en', timeout = 30000, tenant: currentTenant, token, url } = useStripes().okapi; return ky.create({ prefixUrl: url, @@ -10,7 +10,7 @@ export default () => { beforeRequest: [ request => { request.headers.set('Accept-Language', locale); - request.headers.set('X-Okapi-Tenant', tenant); + request.headers.set('X-Okapi-Tenant', tenant ?? currentTenant); request.headers.set('X-Okapi-Token', token); } ] From a117fe4399e0b34688fc04a202425075a6298d7c Mon Sep 17 00:00:00 2001 From: Zak Burke Date: Thu, 12 Oct 2023 15:34:32 -0400 Subject: [PATCH 2/3] tests are nice --- src/useOkapiKy.test.js | 86 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/useOkapiKy.test.js diff --git a/src/useOkapiKy.test.js b/src/useOkapiKy.test.js new file mode 100644 index 000000000..41fbc71a2 --- /dev/null +++ b/src/useOkapiKy.test.js @@ -0,0 +1,86 @@ +import ky from 'ky'; +import { renderHook, waitFor } from '@folio/jest-config-stripes/testing-library/react'; + +import { useStripes } from './StripesContext'; +import useOkapiKy from './useOkapiKy'; + +jest.mock('./StripesContext'); +jest.mock('ky', () => ({ + create: ({ ...av }) => av, +})); + +describe('useOkapiKy', () => { + it('pulls values from stripes object', async () => { + const okapi = { + locale: 'klingon', + tenant: 'tenant', + timeout: 271828, + token: 'token', + url: 'https://whatever.com' + }; + + const mockUseStripes = useStripes; + mockUseStripes.mockReturnValue({ okapi }); + + const r = { + headers: { + set: jest.fn(), + } + }; + + const { result } = renderHook(() => useOkapiKy()); + result.current.hooks.beforeRequest[0](r); + + expect(result.current.prefixUrl).toBe(okapi.url); + expect(result.current.timeout).toBe(okapi.timeout); + + expect(r.headers.set).toHaveBeenCalledWith('Accept-Language', okapi.locale); + expect(r.headers.set).toHaveBeenCalledWith('X-Okapi-Tenant', okapi.tenant); + expect(r.headers.set).toHaveBeenCalledWith('X-Okapi-Token', okapi.token); + }); + + it('provides default values if stripes lacks them', async () => { + const okapi = {}; + + const mockUseStripes = useStripes; + mockUseStripes.mockReturnValue({ okapi }); + + const r = { + headers: { + set: jest.fn(), + } + }; + + const { result } = renderHook(() => useOkapiKy()); + result.current.hooks.beforeRequest[0](r); + + expect(result.current.timeout).toBe(30000); + + expect(r.headers.set).toHaveBeenCalledWith('Accept-Language', 'en'); + }); + + it('overrides tenant', async () => { + const okapi = { + tenant: 'tenant', + timeout: 271828, + token: 'token', + url: 'https://whatever.com' + }; + + const mockUseStripes = useStripes; + mockUseStripes.mockReturnValue({ okapi }); + + const r = { + headers: { + set: jest.fn(), + } + }; + + const { result } = renderHook(() => useOkapiKy('monkey')); + result.current.hooks.beforeRequest[0](r); + + expect(r.headers.set).toHaveBeenCalledWith('X-Okapi-Tenant', 'monkey'); + }); +}); + + From 42b59c721495dd6f9dc0ea4ae57b99cd0df3d559 Mon Sep 17 00:00:00 2001 From: Mariia_Aloshyna Date: Fri, 13 Oct 2023 13:48:12 +0300 Subject: [PATCH 3/3] Move changes to v10.1.0 --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ff830b18..99d023b3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 10.1.0 IN PROGRESS +* Provide optional tenant argument to `useOkapiKy` hook. Refs STCOR-747. + ## [10.0.0](https://github.com/folio-org/stripes-core/tree/v10.0.0) (2023-10-11) [Full Changelog](https://github.com/folio-org/stripes-core/compare/v9.0.0...v10.0.0) @@ -26,7 +28,6 @@ * *BREAKING* bump `react-intl` to `v6.4.4`. Refs STCOR-744. * Bump `stylelint` to `v15` and `stylelint-config-standard` to `v34`. Refs STCOR-745. * Read ky timeout from stripes-config value. Refs STCOR-594. -* Provide optional tenant argument to `useOkapiKy` hook. Refs STCOR-747. ## [9.0.0](https://github.com/folio-org/stripes-core/tree/v9.0.0) (2023-01-30) [Full Changelog](https://github.com/folio-org/stripes-core/compare/v8.3.0...v9.0.0)