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

Commit

Permalink
add neccesary mocks and wip test for ready status (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkolson committed Nov 26, 2021
1 parent fa645a6 commit cdb437f
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 12 deletions.
50 changes: 39 additions & 11 deletions extension/advanzia-assistant.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
import { Script } from './advanzia-assistant';
import FetchMock from 'jest-fetch-mock';
import * as ChromeMock from 'jest-chrome';
import { ContentScript, Script } from './advanzia-assistant';

describe('Content Script', () => {
it('should not do anything if not on transactions page', async () => {
const eventsRaised: { [key: string]: boolean } = {
noop: false,
ready: false,
done: false,
error: false,
};
const eventsRaised: { [key: string]: boolean } = {
noop: false,
ready: false,
done: false,
error: false,
};

const setRaised = (eventName: string) => () => { eventsRaised[eventName] = true };
const setRaised = (eventName: string) => () => { eventsRaised[eventName] = true };
const resetRaised = (eventName: string) => { eventsRaised[eventName] = false };
const resetAllRaised = () => Object.keys(eventsRaised).forEach(resetRaised);

await new Script()
let script: ContentScript;

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

beforeEach(jest.restoreAllMocks);
beforeEach(resetAllRaised);
beforeEach(() => {
script = new Script()
.on('noop', setRaised('noop'))
.on('ready', () => setRaised('ready'))
.on('done', () => setRaised('done'))
.execute();
})

it('should not do anything if executed outside of transactions page', async () => {
await script.execute();


expect(eventsRaised.noop).toBe(true);
expect(eventsRaised.ready).toBe(false);
expect(eventsRaised.done).toBe(false);
expect(eventsRaised.error).toBe(false);
});

it('should indicate ready status if executed on transaction page', async () => {
jest.spyOn(window, "location", "get").mockReturnValue({
...window.location,
...{ pathname: '/retail-app-de' },
});

await script.execute();
expect(eventsRaised.noop).toBe(false);
expect(eventsRaised.ready).toBe(true);
expect(eventsRaised.done).toBe(false);
expect(eventsRaised.error).toBe(false);
});
});
110 changes: 110 additions & 0 deletions extension/package-lock.json

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

4 changes: 3 additions & 1 deletion extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"@types/chrome": "^0.0.164",
"@types/jest": "^27.0.3",
"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 All @@ -26,4 +28,4 @@
"build": "webpack",
"test": "jest"
}
}
}

0 comments on commit cdb437f

Please sign in to comment.