Merge pull request #892 from umputun/fetch-mock

Make fetch mock global
This commit is contained in:
Umputun
2021-02-24 12:56:55 -06:00
committed by GitHub
5 changed files with 11 additions and 54 deletions
+9
View File
@@ -0,0 +1,9 @@
import fetchMock from 'jest-fetch-mock';
beforeAll(() => {
fetchMock.enableMocks();
});
beforeEach(() => {
fetchMock.mockClear();
});
-38
View File
@@ -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);
});
}
};
+1 -8
View File
@@ -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: {
-7
View File
@@ -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);
+1 -1
View File
@@ -17,8 +17,8 @@ module.exports = {
},
setupFiles: ['<rootDir>/jest.setup.ts'],
setupFilesAfterEnv: [
'<rootDir>/app/__mocks__/fetch.ts',
'<rootDir>/app/__mocks__/localstorage.ts',
'<rootDir>/app/__mocks__/headers.ts',
'<rootDir>/app/__stubs__/remark-config.ts',
'<rootDir>/app/__stubs__/static-config.ts',
],