From fedfe92781798fc65836ce91c714d58b236f4b3e Mon Sep 17 00:00:00 2001 From: Vyrtsev Mikhail Date: Sat, 29 Jun 2019 22:38:56 +0300 Subject: [PATCH] add mockHeaders test util --- frontend/app/common/fetcher.test.ts | 16 +++------------- frontend/app/testUtils/mockHeaders.ts | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 frontend/app/testUtils/mockHeaders.ts diff --git a/frontend/app/common/fetcher.test.ts b/frontend/app/common/fetcher.test.ts index 8d1e54f2..d45be047 100644 --- a/frontend/app/common/fetcher.test.ts +++ b/frontend/app/common/fetcher.test.ts @@ -1,23 +1,13 @@ import fetcher from './fetcher'; +import { mockHeaders } from '@app/testUtils/mockHeaders'; describe('fetcher', () => { - let originalHeaders = (window as any).Headers; - beforeAll(() => { - originalHeaders = (window as any).Headers; - (window as any).Headers = class { - append() {} - has() { - return false; - } - get() { - return null; - } - }; + mockHeaders.mock(); }); afterAll(() => { - (window as any).Headers = originalHeaders; + mockHeaders.restore(); }); afterEach(() => { diff --git a/frontend/app/testUtils/mockHeaders.ts b/frontend/app/testUtils/mockHeaders.ts new file mode 100644 index 00000000..6c335f1b --- /dev/null +++ b/frontend/app/testUtils/mockHeaders.ts @@ -0,0 +1,21 @@ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +const originalHeaders = (window as any).Headers; + +export const mockHeaders = { + mock: () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (window as any).Headers = class { + append() {} + has() { + return false; + } + get() { + return null; + } + }; + }, + restore: () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (window as any).Headers = originalHeaders; + }, +};