Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
test ready status (#9)
Browse files Browse the repository at this point in the history
mock fetch manually using cross-fetch.Response and change it to jest-fetch-mock if/when jefflau/jest-fetch-mock#223 lands
  • Loading branch information
alexkolson committed Nov 27, 2021
1 parent 92d623a commit 9bb8e25
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 41 deletions.
19 changes: 14 additions & 5 deletions extension/advanzia-assistant.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import FetchMock from 'jest-fetch-mock';
import * as ChromeMock from 'jest-chrome';
import * as fs from 'fs';
import * as chromeMock from 'jest-chrome';
import { Response } from 'cross-fetch';
import { ContentScript, Script } from './advanzia-assistant';

describe('Content Script', () => {
Expand All @@ -16,9 +17,7 @@ describe('Content Script', () => {

let script: ContentScript;

beforeAll(FetchMock.enableMocks);
beforeAll(() => Object.assign(global, ChromeMock));

beforeAll(() => Object.assign(global, chromeMock));
beforeEach(jest.restoreAllMocks);
beforeEach(resetAllRaised);
beforeEach(() => {
Expand All @@ -43,9 +42,19 @@ describe('Content Script', () => {
...window.location,
...{ pathname: '/retail-app-de' },
});
global.fetch = async () => new Response(fs.readFileSync('../dist/advanzia-assistant.wasm'));

const simulateAdvanziaAppDoingStuff = () => setTimeout(() => {
const div = document.createElement('div');
div.className = 'card';
document.body.appendChild(div);
}, 2000);

simulateAdvanziaAppDoingStuff();

await script.execute();

expect(chrome.runtime.getURL).toBeCalledTimes(1);
expect(eventsRaised.noop).toBe(false);
expect(eventsRaised.ready).toBe(true);
expect(eventsRaised.done).toBe(false);
Expand Down
8 changes: 6 additions & 2 deletions extension/advanzia-assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ export class Script extends EventTarget implements ContentScript, EventListenerO
return;
}

const wasmResponse = await fetch(chrome.runtime.getURL('advanzia-assistant.wasm'));
const wasm = await WebAssembly.instantiateStreaming(wasmResponse, { env: { memory: this.memory } });
const wasmPath = chrome.runtime.getURL('advanzia-assistant.wasm');
const wasmResponse = await fetch(wasmPath);

const wasmResponseArrayBuffer = await wasmResponse.arrayBuffer();

const wasm = await WebAssembly.instantiate(wasmResponseArrayBuffer, { env: { memory: this.memory } });

This comment has been minimized.

Copy link
@alexkolson

alexkolson Nov 27, 2021

Author Contributor

Changed from .instantiateStreaming() to .instantiate() because NodejS doesn't support instantiateStreaming() and when running the tests, the WebAssembly object comes from Node, not from jsdom.

this.wasmExports = wasm.instance.exports as WasmExports;

await this.pageReasonablyLoaded();
Expand Down
34 changes: 1 addition & 33 deletions extension/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"devDependencies": {
"@types/chrome": "^0.0.164",
"@types/jest": "^27.0.3",
"cross-fetch": "^3.1.4",
"jest": "^27.3.1",
"jest-chrome": "^0.7.2",
"jest-fetch-mock": "^3.0.3",
"ts-jest": "^27.0.7",
"ts-loader": "^9.2.6",
"typescript": "^4.5.2",
Expand Down

0 comments on commit 9bb8e25

Please sign in to comment.