From b184bb5b7b7db24898680577ea796da654789e1d Mon Sep 17 00:00:00 2001 From: Pavel Mineev Date: Sun, 21 Feb 2021 23:53:23 +0300 Subject: [PATCH] Make fetch mock global --- frontend/app/__mocks__/fetch.ts | 9 +++++ frontend/app/__mocks__/headers.ts | 38 --------------------- frontend/app/store/comments/actions.test.ts | 9 +---- frontend/app/store/user/actions.test.ts | 7 ---- frontend/jest.config.js | 2 +- 5 files changed, 11 insertions(+), 54 deletions(-) create mode 100644 frontend/app/__mocks__/fetch.ts delete mode 100644 frontend/app/__mocks__/headers.ts diff --git a/frontend/app/__mocks__/fetch.ts b/frontend/app/__mocks__/fetch.ts new file mode 100644 index 00000000..60b545fa --- /dev/null +++ b/frontend/app/__mocks__/fetch.ts @@ -0,0 +1,9 @@ +import fetchMock from 'jest-fetch-mock'; + +beforeAll(() => { + fetchMock.enableMocks(); +}); + +beforeEach(() => { + fetchMock.mockClear(); +}); diff --git a/frontend/app/__mocks__/headers.ts b/frontend/app/__mocks__/headers.ts deleted file mode 100644 index 48746784..00000000 --- a/frontend/app/__mocks__/headers.ts +++ /dev/null @@ -1,38 +0,0 @@ -global.Headers = class HeadersMock extends Headers implements Headers { - private headers = new Map(); - - constructor(headers?: HeadersInit) { - super(headers); - - if (typeof headers === 'object' && headers !== null) { - const entries = Object.entries(headers) as [string, string][]; - - if (entries.length > 0) { - entries.forEach(([k, v]) => { - this.append(k, v); - }); - } - } - } - - append(key: string, value: string) { - this.headers.set(key, value); - } - set(key: string, value: string) { - this.headers.set(key, value); - } - has(key: string) { - return this.headers.has(key); - } - get(key: string) { - return this.headers.get(key) || null; - } - delete(key: string) { - this.headers.delete(key); - } - forEach(callbackfn: (value: string, key: string, parent: Headers) => void, thisArg?: unknown) { - this.headers.forEach((value, key) => { - callbackfn.call(thisArg || this, value, key, this); - }); - } -}; diff --git a/frontend/app/store/comments/actions.test.ts b/frontend/app/store/comments/actions.test.ts index 9bd604fd..0c5206e2 100644 --- a/frontend/app/store/comments/actions.test.ts +++ b/frontend/app/store/comments/actions.test.ts @@ -5,14 +5,7 @@ import { updateSorting } from './actions'; import { COMMENTS_SET_SORT } from './types'; describe('Store comments actions', () => { - beforeAll(() => { - require('jest-fetch-mock').enableMocks(); - }); - afterAll(() => { - require('jest-fetch-mock').resetMocks(); - }); - - it('handles changing a purchase status and fetches all purchases', async () => { + it('should save last sort to localstorage', async () => { const newSort = '+controversy'; const store = stubStore({ comments: { diff --git a/frontend/app/store/user/actions.test.ts b/frontend/app/store/user/actions.test.ts index 759abeeb..b810d0fc 100644 --- a/frontend/app/store/user/actions.test.ts +++ b/frontend/app/store/user/actions.test.ts @@ -8,13 +8,6 @@ import { setVerifiedStatus, hideUser, unhideUser, unblockUser, fetchBlockedUsers import { USER_UNHIDE, USER_HIDE, USER_UNBAN, USER_BANLIST_SET, USER_HIDELIST_SET } from './types'; describe('store user actions', () => { - beforeAll(() => { - require('jest-fetch-mock').enableMocks(); - }); - afterAll(() => { - require('jest-fetch-mock').resetMocks(); - }); - test('fetchBlockedUsers', async () => { const store = stubStore(INITIAL_STORE); diff --git a/frontend/jest.config.js b/frontend/jest.config.js index 2b583129..55512d7a 100644 --- a/frontend/jest.config.js +++ b/frontend/jest.config.js @@ -18,7 +18,7 @@ module.exports = { setupFiles: ['/jest.setup.ts'], setupFilesAfterEnv: [ 'jest-localstorage-mock', - '/app/__mocks__/headers.ts', + '/app/__mocks__/fetch.ts', '/app/__stubs__/remark-config.ts', '/app/__stubs__/static-config.ts', ],