-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into FOLIO-3627-sw
- Loading branch information
Showing
5 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import ky from 'ky'; | ||
Check warning on line 1 in src/useOkapiKy.test.js GitHub Actions / build-npm
|
||
import { renderHook, waitFor } from '@folio/jest-config-stripes/testing-library/react'; | ||
Check warning on line 2 in src/useOkapiKy.test.js GitHub Actions / build-npm
|
||
|
||
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({ tenant: 'monkey' })); | ||
result.current.hooks.beforeRequest[0](r); | ||
|
||
expect(r.headers.set).toHaveBeenCalledWith('X-Okapi-Tenant', 'monkey'); | ||
}); | ||
}); | ||
|
||
|