|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import nock from "nock" |
| 6 | +import fetchData from "../fetch" |
| 7 | +import { createPluginConfig } from "../plugin-options" |
| 8 | + |
| 9 | +const host = `localhost` |
| 10 | +const options = { |
| 11 | + spaceId: `12345`, |
| 12 | + accessToken: `67890`, |
| 13 | + host, |
| 14 | + contentfulClientConfig: { |
| 15 | + retryLimit: 2, |
| 16 | + }, |
| 17 | +} |
| 18 | + |
| 19 | +const baseURI = `https://${host}` |
| 20 | + |
| 21 | +const start = jest.fn() |
| 22 | +const end = jest.fn() |
| 23 | +const mockActivity = { |
| 24 | + start, |
| 25 | + end, |
| 26 | + tick: jest.fn(), |
| 27 | + done: end, |
| 28 | +} |
| 29 | + |
| 30 | +const reporter = { |
| 31 | + info: jest.fn(), |
| 32 | + verbose: jest.fn(), |
| 33 | + warn: jest.fn(), |
| 34 | + panic: jest.fn(e => { |
| 35 | + throw e |
| 36 | + }), |
| 37 | + activityTimer: jest.fn(() => mockActivity), |
| 38 | + createProgress: jest.fn(() => mockActivity), |
| 39 | +} |
| 40 | + |
| 41 | +const pluginConfig = createPluginConfig(options) |
| 42 | + |
| 43 | +describe(`fetch-backoff`, () => { |
| 44 | + afterEach(() => { |
| 45 | + nock.cleanAll() |
| 46 | + reporter.verbose.mockClear() |
| 47 | + reporter.panic.mockClear() |
| 48 | + reporter.warn.mockClear() |
| 49 | + }) |
| 50 | + |
| 51 | + test(`backoffs page limit when limit is reached`, async () => { |
| 52 | + jest.setTimeout(30000) |
| 53 | + const scope = nock(baseURI) |
| 54 | + // Space |
| 55 | + .get(`/spaces/${options.spaceId}/`) |
| 56 | + .reply(200, { items: [] }) |
| 57 | + // Locales |
| 58 | + .get(`/spaces/${options.spaceId}/environments/master/locales`) |
| 59 | + .reply(200, { items: [{ code: `en`, default: true }] }) |
| 60 | + // Sync with 1000 (to much) |
| 61 | + .get( |
| 62 | + `/spaces/${options.spaceId}/environments/master/sync?initial=true&limit=1000` |
| 63 | + ) |
| 64 | + .times(1) |
| 65 | + .reply(400, { |
| 66 | + sys: { type: `Error`, id: `BadRequest` }, |
| 67 | + message: `Response size too big. Maximum allowed response size: 512000B.`, |
| 68 | + requestId: `12345`, |
| 69 | + }) |
| 70 | + // Sync with 666 (still to much) |
| 71 | + .get( |
| 72 | + `/spaces/${options.spaceId}/environments/master/sync?initial=true&limit=666` |
| 73 | + ) |
| 74 | + .times(1) |
| 75 | + .reply(400, { |
| 76 | + sys: { type: `Error`, id: `BadRequest` }, |
| 77 | + message: `Response size too big. Maximum allowed response size: 512000B.`, |
| 78 | + requestId: `12345`, |
| 79 | + }) |
| 80 | + // Sync with 444 |
| 81 | + .get( |
| 82 | + `/spaces/${options.spaceId}/environments/master/sync?initial=true&limit=444` |
| 83 | + ) |
| 84 | + .reply(200, { items: [] }) |
| 85 | + // Content types |
| 86 | + .get( |
| 87 | + `/spaces/${options.spaceId}/environments/master/content_types?skip=0&limit=1000&order=sys.createdAt` |
| 88 | + ) |
| 89 | + .reply(200, { items: [] }) |
| 90 | + |
| 91 | + await fetchData({ pluginConfig, reporter }) |
| 92 | + |
| 93 | + expect(reporter.panic).not.toBeCalled() |
| 94 | + expect(reporter.warn.mock.calls).toMatchInlineSnapshot(` |
| 95 | + Array [ |
| 96 | + Array [ |
| 97 | + "The sync with Contentful failed using pageLimit 1000 as the reponse size limit of the API is exceeded. |
| 98 | +
|
| 99 | + Retrying sync with pageLimit of 666", |
| 100 | + ], |
| 101 | + Array [ |
| 102 | + "The sync with Contentful failed using pageLimit 666 as the reponse size limit of the API is exceeded. |
| 103 | +
|
| 104 | + Retrying sync with pageLimit of 444", |
| 105 | + ], |
| 106 | + Array [ |
| 107 | + "We recommend you to set your pageLimit in gatsby-config.js to 444 to avoid failed synchronizations.", |
| 108 | + ], |
| 109 | + ] |
| 110 | + `) |
| 111 | + expect(scope.isDone()).toBeTruthy() |
| 112 | + }) |
| 113 | + |
| 114 | + test(`does not backoff page limit when limit is not reached`, async () => { |
| 115 | + jest.setTimeout(30000) |
| 116 | + const scope = nock(baseURI) |
| 117 | + // Space |
| 118 | + .get(`/spaces/${options.spaceId}/`) |
| 119 | + .reply(200, { items: [] }) |
| 120 | + // Locales |
| 121 | + .get(`/spaces/${options.spaceId}/environments/master/locales`) |
| 122 | + .reply(200, { items: [{ code: `en`, default: true }] }) |
| 123 | + // Sync with 1000 (no limit exceeded) |
| 124 | + .get( |
| 125 | + `/spaces/${options.spaceId}/environments/master/sync?initial=true&limit=1000` |
| 126 | + ) |
| 127 | + .reply(200, { items: [] }) |
| 128 | + // Content types |
| 129 | + .get( |
| 130 | + `/spaces/${options.spaceId}/environments/master/content_types?skip=0&limit=1000&order=sys.createdAt` |
| 131 | + ) |
| 132 | + .reply(200, { items: [] }) |
| 133 | + |
| 134 | + await fetchData({ pluginConfig, reporter }) |
| 135 | + |
| 136 | + expect(reporter.panic).not.toBeCalled() |
| 137 | + expect(reporter.warn).not.toBeCalled() |
| 138 | + expect(scope.isDone()).toBeTruthy() |
| 139 | + }) |
| 140 | +}) |
0 commit comments