Skip to content

Commit

Permalink
STCOR-747: Provide optional tenant argument to useOkapiKy hook (#1348)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariia-aloshyna authored and Dmitriy-Litvinenko committed Nov 23, 2023
1 parent 793e2c7 commit 02f52fb
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

* Export `validateUser`. Refs STCOR-749.

* 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)

Expand Down
6 changes: 3 additions & 3 deletions src/useOkapiKy.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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,
hooks: {
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);
}
]
Expand Down
86 changes: 86 additions & 0 deletions src/useOkapiKy.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});


0 comments on commit 02f52fb

Please sign in to comment.