Skip to content

Commit

Permalink
chore: replace browserEnv fn with jest.spyOn
Browse files Browse the repository at this point in the history
  • Loading branch information
otabek-magic committed Nov 27, 2024
1 parent dc180f7 commit dd691b6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 49 deletions.
7 changes: 3 additions & 4 deletions packages/magic-sdk/test/spec/iframe-controller/_post.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import browserEnv from '@ikscodes/browser-env';
import { createModalNotReadyError } from '@magic-sdk/provider';
import { createIframeController } from '../../factories';

beforeEach(() => {
browserEnv.restore();
browserEnv.stub('addEventListener', jest.fn());
jest.restoreAllMocks();
jest.spyOn(global, 'addEventListener').mockImplementation(jest.fn());
// Don't let JSDOM try to load the iframe
browserEnv.stub('document.body.appendChild', jest.fn());
jest.spyOn(document.body, 'appendChild').mockImplementation(jest.fn());
});

test('Calls iframe.contentWindow.postMessage with the expected arguments', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import browserEnv from '@ikscodes/browser-env';
import { createIframeController } from '../../factories';
import { IframeController } from '../../../src/iframe-controller';

beforeEach(() => {
browserEnv.restore();
browserEnv.stub('addEventListener', jest.fn());
browserEnv.stub('console.log', jest.fn());
jest.restoreAllMocks();
jest.spyOn(global, 'addEventListener').mockImplementation(jest.fn());
jest.spyOn(console, 'log').mockImplementation(jest.fn());
});

test('Change visibility style to `hidden` and opacity to 0', async () => {
Expand Down
71 changes: 35 additions & 36 deletions packages/magic-sdk/test/spec/iframe-controller/init.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import browserEnv from '@ikscodes/browser-env';
import { IframeController } from '../../../src/iframe-controller';
import { ENCODED_QUERY_PARAMS, MAGIC_RELAYER_FULL_URL } from '../../constants';
import { createIframeController } from '../../factories';
Expand Down Expand Up @@ -34,19 +33,19 @@ function createOverlayElementsStub() {
}

beforeEach(() => {
browserEnv.restore();
jest.restoreAllMocks();
(IframeController.prototype as any).listen = jest.fn();
});

test('Appends header with style, appends body with iframe, and resolves iframe', async () => {
const { appendChildStub, classListAddStub, createElementStub } = createOverlayElementsStub();

browserEnv.stub('addEventListener', jest.fn());
browserEnv.stub('removeEventListener', jest.fn());
browserEnv.stub('document.querySelectorAll', () => ({ length: 0 }));
browserEnv.stub('document.createElement', createElementStub);
browserEnv.stub('document.readyState', 'loaded');
browserEnv.stub('document.body.appendChild', appendChildStub);
jest.spyOn(global, 'addEventListener').mockImplementation(jest.fn());
jest.spyOn(global, 'removeEventListener').mockImplementation(jest.fn());
jest.spyOn(document, 'querySelectorAll').mockReturnValue({ length: 0 } as any);
jest.spyOn(document, 'createElement').mockImplementation(createElementStub);
jest.spyOn(document, 'readyState', 'get').mockReturnValue('complete');
jest.spyOn(document.body, 'appendChild').mockImplementation(appendChildStub);

const overlay = createIframeController();
const iframe = await (overlay as any).iframe;
Expand All @@ -63,12 +62,12 @@ test('Appends header with style, appends body with iframe, and resolves iframe',
test('Displays warning in console upon duplicate iframes', async () => {
const { appendChildStub, createElementStub } = createOverlayElementsStub();

browserEnv.stub('addEventListener', jest.fn());
browserEnv.stub('removeEventListener', jest.fn());
browserEnv.stub('document.querySelectorAll', () => [{ src: ENCODED_QUERY_PARAMS }]);
browserEnv.stub('document.createElement', createElementStub);
browserEnv.stub('document.readyState', 'loaded');
browserEnv.stub('document.body.appendChild', appendChildStub);
jest.spyOn(global, 'addEventListener').mockImplementation(jest.fn());
jest.spyOn(global, 'removeEventListener').mockImplementation(jest.fn());
jest.spyOn(document, 'querySelectorAll').mockReturnValue([{ src: ENCODED_QUERY_PARAMS }] as any);
jest.spyOn(document, 'createElement').mockImplementation(createElementStub);
jest.spyOn(document, 'readyState', 'get').mockReturnValue('complete');
jest.spyOn(document.body, 'appendChild').mockImplementation(appendChildStub);

const consoleWarnStub = jest.spyOn(console, 'warn').mockImplementation();

Expand All @@ -80,12 +79,12 @@ test('Displays warning in console upon duplicate iframes', async () => {
test('Waits until `document` is loaded/ready', async () => {
const { appendChildStub, createElementStub } = createOverlayElementsStub();

browserEnv.stub('addEventListener', jest.fn());
browserEnv.stub('removeEventListener', jest.fn());
browserEnv.stub('document.querySelectorAll', () => ({ length: 0 }));
browserEnv.stub('document.createElement', createElementStub);
browserEnv.stub('document.readyState', 'initializing');
browserEnv.stub('document.body.appendChild', appendChildStub);
jest.spyOn(global, 'addEventListener').mockImplementation(jest.fn());
jest.spyOn(global, 'removeEventListener').mockImplementation(jest.fn());
jest.spyOn(document, 'querySelectorAll').mockReturnValue({ length: 0 } as any);
jest.spyOn(document, 'createElement').mockImplementation(createElementStub);
jest.spyOn(document, 'readyState', 'get').mockReturnValue('loading');
jest.spyOn(document.body, 'appendChild').mockImplementation(appendChildStub);

createIframeController();

Expand All @@ -96,12 +95,12 @@ test('Assumes the iframe is not yet initialized if `src` is `undefined`', async
const { classListAddStub, createElementStub } = createOverlayElementsStub();
const appendChildStub = jest.fn();

browserEnv.stub('addEventListener', jest.fn());
browserEnv.stub('removeEventListener', jest.fn());
browserEnv.stub('document.querySelectorAll', () => ({ length: 0 }));
browserEnv.stub('document.createElement', createElementStub);
browserEnv.stub('document.readyState', 'loaded');
browserEnv.stub('document.body.appendChild', appendChildStub);
jest.spyOn(global, 'addEventListener').mockImplementation(jest.fn());
jest.spyOn(global, 'removeEventListener').mockImplementation(jest.fn());
jest.spyOn(document, 'querySelectorAll').mockReturnValue({ length: 0 } as any);
jest.spyOn(document, 'createElement').mockImplementation(createElementStub);
jest.spyOn(document, 'readyState', 'get').mockReturnValue('complete');
jest.spyOn(document.body, 'appendChild').mockImplementation(appendChildStub);

const overlay = createIframeController();
const iframe = await (overlay as any).iframe;
Expand All @@ -116,17 +115,17 @@ test('Assumes the iframe is not yet initialized if `src` is `undefined`', async

test('Adds `message` event listener', () => {
const addEventListenerStub = jest.fn();
browserEnv.stub('addEventListener', addEventListenerStub);
browserEnv.stub('removeEventListener', jest.fn());
jest.spyOn(global, 'addEventListener').mockImplementation(addEventListenerStub);
jest.spyOn(global, 'removeEventListener').mockImplementation(jest.fn());

createIframeController();

expect(addEventListenerStub.mock.calls[0][0]).toBe('message');
});

test('Ignores events with different origin than expected', done => {
browserEnv.stub('addEventListener', jest.fn());
browserEnv.stub('removeEventListener', jest.fn());
jest.spyOn(global, 'addEventListener').mockImplementation(jest.fn());
jest.spyOn(global, 'removeEventListener').mockImplementation(jest.fn());

const viewController = createIframeController('http://asdf');
const onHandlerStub = jest.fn();
Expand All @@ -141,8 +140,8 @@ test('Ignores events with different origin than expected', done => {
});

test('Ignores events with undefined `data` attribute', done => {
browserEnv.stub('addEventListener', jest.fn());
browserEnv.stub('removeEventListener', jest.fn());
jest.spyOn(global, 'addEventListener').mockImplementation(jest.fn());
jest.spyOn(global, 'removeEventListener').mockImplementation(jest.fn());

const viewController = createIframeController();
const onHandlerStub = jest.fn();
Expand All @@ -157,8 +156,8 @@ test('Ignores events with undefined `data` attribute', done => {
});

test('Ignores events with undefined `data.msgType`', done => {
browserEnv.stub('addEventListener', jest.fn());
browserEnv.stub('removeEventListener', jest.fn());
jest.spyOn(global, 'addEventListener').mockImplementation(jest.fn());
jest.spyOn(global, 'removeEventListener').mockImplementation(jest.fn());

const viewController = createIframeController();
const onHandlerStub = jest.fn();
Expand Down Expand Up @@ -188,7 +187,7 @@ test('Executes events where `messageHandlers` size is > 0', done => {
});

test('Ignores events where `messageHandlers` size is === 0', done => {
browserEnv.stub('location', new URL(MAGIC_RELAYER_FULL_URL));
jest.spyOn(global, 'location', 'get').mockImplementation(() => new URL(MAGIC_RELAYER_FULL_URL) as any);

const viewController = createIframeController();
(viewController as any).endpoint = ''; // Force `event.origin` and `this.endpoint` to be equivalent
Expand All @@ -202,7 +201,7 @@ test('Ignores events where `messageHandlers` size is === 0', done => {
});

test('Ignores events where `event.origin` and `this.endpoint` are not equivalent', done => {
browserEnv.stub('location', new URL(MAGIC_RELAYER_FULL_URL));
jest.spyOn(global, 'location', 'get').mockImplementation(() => new URL(MAGIC_RELAYER_FULL_URL) as any);

const viewController = createIframeController();
(viewController as any).messageHandlers = { size: 0 };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import browserEnv from '@ikscodes/browser-env';
import { createIframeController } from '../../factories';
import { IframeController } from '../../../src/iframe-controller';

beforeEach(() => {
browserEnv.restore();
browserEnv.stub('addEventListener', jest.fn());
browserEnv.stub('console.warn', jest.fn());
jest.restoreAllMocks();
jest.spyOn(global, 'addEventListener').mockImplementation(jest.fn());
jest.spyOn(console, 'warn').mockImplementation(jest.fn());
});

test('Change display style to `block`', async () => {
Expand Down Expand Up @@ -56,7 +55,7 @@ test('Saves the current `document.activeElement`', async () => {

const overlay = createIframeController();

browserEnv.stub('document.activeElement', 'qwertyqwerty');
jest.spyOn(document, 'activeElement', 'get').mockReturnValue('qwertyqwerty' as any);

expect((overlay as any).activeElement).toBe(null);

Expand Down

0 comments on commit dd691b6

Please sign in to comment.