|
| 1 | +import { createBackendFetch } from './BackendFetch'; |
| 2 | +import { createFetchingUtility } from './__test__/fetchingUtillity'; |
| 3 | + |
| 4 | +describe('backend fetch', () => { |
| 5 | + let f: ReturnType<typeof createFetchingUtility>; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + f = createFetchingUtility(); |
| 9 | + }); |
| 10 | + |
| 11 | + it('calls fetch with correct params', () => { |
| 12 | + const plugin = createBackendFetch(); |
| 13 | + plugin.getRecord({ fetch: f.fetchMock, language: 'de' }); |
| 14 | + expect(f.fetchMock).toHaveBeenCalledWith( |
| 15 | + '/i18n/de.json', |
| 16 | + expect.objectContaining({ |
| 17 | + headers: { Accept: 'application/json' }, |
| 18 | + }) |
| 19 | + ); |
| 20 | + }); |
| 21 | + |
| 22 | + it('calls fetch with custom prefix', () => { |
| 23 | + const plugin = createBackendFetch({ prefix: 'http://test.com/test' }); |
| 24 | + plugin.getRecord({ fetch: f.fetchMock, language: 'de', namespace: 'ns' }); |
| 25 | + expect(f.fetchMock).toHaveBeenCalledWith( |
| 26 | + 'http://test.com/test/ns/de.json', |
| 27 | + expect.objectContaining({ |
| 28 | + headers: { Accept: 'application/json' }, |
| 29 | + }) |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + it('handles extra slash', () => { |
| 34 | + const plugin = createBackendFetch({ prefix: 'http://test.com/test/' }); |
| 35 | + plugin.getRecord({ fetch: f.fetchMock, language: 'de' }); |
| 36 | + expect(f.fetchMock).toHaveBeenCalledWith( |
| 37 | + 'http://test.com/test/de.json', |
| 38 | + expect.objectContaining({ |
| 39 | + headers: { Accept: 'application/json' }, |
| 40 | + }) |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it('adds headers', () => { |
| 45 | + const plugin = createBackendFetch({ |
| 46 | + prefix: 'http://test.com/test/', |
| 47 | + headers: { Authorization: 'test' }, |
| 48 | + }); |
| 49 | + plugin.getRecord({ fetch: f.fetchMock, language: 'de' }); |
| 50 | + expect(f.fetchMock).toHaveBeenCalledWith( |
| 51 | + 'http://test.com/test/de.json', |
| 52 | + expect.objectContaining({ |
| 53 | + headers: { Accept: 'application/json', Authorization: 'test' }, |
| 54 | + }) |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('passes additional properties', () => { |
| 59 | + const plugin = createBackendFetch({ |
| 60 | + prefix: 'http://test.com/test/', |
| 61 | + cache: 'no-cache', |
| 62 | + }); |
| 63 | + plugin.getRecord({ fetch: f.fetchMock, language: 'de' }); |
| 64 | + expect(f.fetchMock).toHaveBeenCalledWith( |
| 65 | + 'http://test.com/test/de.json', |
| 66 | + expect.objectContaining({ |
| 67 | + headers: { Accept: 'application/json' }, |
| 68 | + cache: 'no-cache', |
| 69 | + }) |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it('fails with a timeout', async () => { |
| 74 | + const plugin = createBackendFetch({ |
| 75 | + prefix: 'http://test.com/test/', |
| 76 | + timeout: 5, |
| 77 | + }); |
| 78 | + await expect( |
| 79 | + plugin.getRecord({ fetch: f.infiniteFetch, language: 'de' }) |
| 80 | + ).rejects.toHaveProperty( |
| 81 | + 'message', |
| 82 | + 'TIMEOUT: http://test.com/test/de.json' |
| 83 | + ); |
| 84 | + |
| 85 | + expect(f.infiniteFetch).toHaveBeenCalledWith( |
| 86 | + 'http://test.com/test/de.json', |
| 87 | + expect.objectContaining({ |
| 88 | + headers: { Accept: 'application/json' }, |
| 89 | + }) |
| 90 | + ); |
| 91 | + expect(f.signalHandler).toHaveBeenCalledWith('Aborted with signal'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('throws the original error', async () => { |
| 95 | + const plugin = createBackendFetch({ |
| 96 | + prefix: 'http://test.com/test/', |
| 97 | + }); |
| 98 | + expect( |
| 99 | + plugin.getRecord({ fetch: f.failingFetch, language: 'de' }) |
| 100 | + ).rejects.toHaveProperty('message', 'Fetch failed'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('returns undefined when `fallbackOnFail`', async () => { |
| 104 | + const plugin = createBackendFetch({ |
| 105 | + prefix: 'http://test.com/test/', |
| 106 | + fallbackOnFail: true, |
| 107 | + }); |
| 108 | + expect( |
| 109 | + await plugin.getRecord({ fetch: f.failingFetch, language: 'de' }) |
| 110 | + ).toEqual(undefined); |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns undefined when `fallbackOnFail` and timeout', async () => { |
| 114 | + const plugin = createBackendFetch({ |
| 115 | + prefix: 'http://test.com/test/', |
| 116 | + fallbackOnFail: true, |
| 117 | + timeout: 5, |
| 118 | + }); |
| 119 | + expect( |
| 120 | + await plugin.getRecord({ fetch: f.infiniteFetch, language: 'de' }) |
| 121 | + ).toEqual(undefined); |
| 122 | + expect(f.signalHandler).toHaveBeenCalledWith('Aborted with signal'); |
| 123 | + }); |
| 124 | +}); |
0 commit comments