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

finish initial tests (close #9) #13

Merged
merged 1 commit into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extension/__snapshots__/advanzia-assistant.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Content Script when on transactions page to insert correct dom modifications 1`] = `
exports[`Content Script when run on mein.gebuhrenfrei.com somewhere when on transactions page to insert correct dom modifications 1`] = `
<body>
<div
class="card"
Expand Down
84 changes: 46 additions & 38 deletions extension/advanzia-assistant.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as chromeMock from 'jest-chrome';
import { Response } from 'cross-fetch';
import { ContentScript, Script } from './advanzia-assistant';
import { Script } from './advanzia-assistant';

describe('Content Script', () => {
const eventsRaised: { [key: string]: boolean } = {
Expand All @@ -15,57 +15,65 @@ describe('Content Script', () => {
const resetRaised = (eventName: string) => { eventsRaised[eventName] = false };
const resetAllRaised = () => Object.keys(eventsRaised).forEach(resetRaised);

let script: ContentScript;
let script: Script;

beforeAll(() => Object.assign(global, chromeMock));
beforeEach(resetAllRaised);
beforeEach(() => {
script = new Script()
.on('noop', setRaised('noop'))
.on('ready', setRaised('ready'))
.on('done', setRaised('done'))
script = new Script();
});
beforeEach(() => Object.keys(eventsRaised).forEach(eventName => script.on(eventName, setRaised(eventName))));
beforeEach(jest.resetAllMocks);
beforeEach(jest.restoreAllMocks);
describe('when not on transactions page', () => {
it('should not do anything', async () => {
await script.execute();

expect(chrome.runtime.getURL).toBeCalledTimes(0);
expect(eventsRaised.noop).toBe(true);
expect(eventsRaised.ready).toBe(false);
expect(eventsRaised.done).toBe(false);
expect(eventsRaised.error).toBe(false);
});
});

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

describe('when run on mein.gebuhrenfrei.com somewhere', () => {
beforeEach(() => {
jest.spyOn(window, "location", "get").mockReturnValue({
...window.location,
...{ pathname: '/retail-app-de' },
...{ host: 'mein.gebuhrenfrei.com' },
});
global.fetch = async () => new Response(fs.readFileSync('../dist/advanzia-assistant.wasm'));
});
describe('when not on transactions page', () => {
it('should not do anything', async () => {
await script.execute();

beforeEach(simulateAdvanziaAppDoingStuff);
beforeEach(() => script.execute());
it('should indicate ready status', async () => {
expect(chrome.runtime.getURL).toBeCalledTimes(1);
expect(eventsRaised.noop).toBe(false);
expect(eventsRaised.ready).toBe(true);
expect(eventsRaised.done).toBe(false);
expect(eventsRaised.error).toBe(false);
expect(eventsRaised.noop).toBe(true);
});
});

it('to insert correct dom modifications', async () => {
expect(document.body).toMatchSnapshot();
describe('when on transactions page', () => {
const simulateAdvanziaAppDoingStuff = () => setTimeout(() => {
const div = document.createElement('div');
div.className = 'card';
document.body.appendChild(div);
}, 2000);

beforeEach(() => {
jest.spyOn(window, "location", "get").mockReturnValue({
...window.location,
...{ pathname: '/retail-app-de' },
});
global.fetch = async () => new Response(fs.readFileSync('../dist/advanzia-assistant.wasm'));
});

beforeEach(simulateAdvanziaAppDoingStuff);
beforeEach(() => script.execute());
it('should indicate ready status', async () => {
expect(eventsRaised.ready).toBe(true);
});

it('to insert correct dom modifications', async () => {
expect(document.body).toMatchSnapshot();
});
});
})
});

describe('otherwise', () => {
it('should raise error event and create script error indicating that the script cannot be run if not on mein.gebuhrenfrei.com', async () => {
await script.execute();

expect(eventsRaised.error).toBe(true);
expect(script.errors.length).toBe(1);
expect(script.errors[0]).toEqual(new Error('this script can only be run on mein.gebuhrenfrei.com'));
});
});
});
5 changes: 5 additions & 0 deletions extension/advanzia-assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export class Script extends EventTarget implements ContentScript, EventListenerO
}

async execute() {
if (location.host !== 'mein.gebuhrenfrei.com') {
this.error(new Error('this script can only be run on mein.gebuhrenfrei.com'));
return;
}

if (location.pathname.indexOf('retail-app') === -1) {
this.dispatchEvent(this.events.noop);
return;
Expand Down