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

Commit

Permalink
reorg tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moribellamy committed May 16, 2020
1 parent 51ef9c7 commit 6754394
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 79 deletions.
29 changes: 29 additions & 0 deletions tests/archive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as mockBrowser from 'sinon-chrome';
import { BrowserTab } from '../@types/graytabby';
import { Broker, BrokerConsumer } from '../src/brokers';
import { ARCHIVAL, DOCUMENT } from '../src/globals';
import { dateFromKey, INDEX_V2_KEY } from '../src/tabs';
import { assertElement, initGrayTabby, testTab } from './utils';

describe('archive', function() {
it('should work', async function() {
const archival = new Broker<BrowserTab[]>('moreTabs');
// Capture callback for tab archival so we can inject tabs in test.
let consumer: BrokerConsumer<BrowserTab[]>;
archival.sub = func => {
consumer = func;
};
ARCHIVAL.set(archival);
await initGrayTabby();
assertElement('#groups > div', DOCUMENT.get(), 0);

consumer([testTab({ url: 'http://example.com' })], {}, () => null);
const group = assertElement('#groups > div', DOCUMENT.get());

mockBrowser.storage.local.get
.withArgs(INDEX_V2_KEY)
.returns(new Promise(() => [dateFromKey(group.id)]));
const a = <HTMLAnchorElement>assertElement('a', group);
a.click();
});
});
69 changes: 2 additions & 67 deletions tests/grayTabby.test.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,7 @@
import * as assert from 'assert';
import { JSDOM } from 'jsdom';
import * as mockBrowser from 'sinon-chrome';
import { grayTabby } from '../src/ui';
import { OPTIONS_KEY } from '../src/options';
import { INDEX_V1_KEY, INDEX_V2_KEY, dateFromKey } from '../src/tabs';
import { BROWSER, DOCUMENT, ARCHIVAL } from '../src/globals';
import { BrowserTab } from '../@types/graytabby';
import { Broker, BrokerConsumer } from '../src/brokers';

function testTab(args: Partial<BrowserTab>): BrowserTab {
return {
index: 1,
incognito: false,
highlighted: false,
active: false,
pinned: false,
...args,
};
}

// function wait(ms: number) {
// return new Promise(resolve => setTimeout(resolve, ms));
// }
import { initGrayTabby } from './utils';

describe('graytabby', function() {
this.beforeEach(async function() {
BROWSER.set(<any>mockBrowser);
const jsdom = await JSDOM.fromFile('src/app.html');
DOCUMENT.set(jsdom.window.document);
mockBrowser.tabs.query.returns([]);
mockBrowser.storage.local.get.withArgs(INDEX_V1_KEY).returns('[]');
mockBrowser.storage.local.get.withArgs(OPTIONS_KEY).returns([]);
mockBrowser.storage.local.get.withArgs(INDEX_V2_KEY).returns([]);
});

async function getGrayTabby(): Promise<void> {
try {
return await grayTabby();
} catch (err) {
assert.fail('Uncaught error: ' + err);
}
}

it('should attach to dom without throwing', async () => {
await getGrayTabby();
});

it('should suppport basic archival', async () => {
const archival = new Broker<BrowserTab[]>('moreTabs');
// Capture callback for tab archival so we can inject tabs in test.
let consumer: BrokerConsumer<BrowserTab[]>;
archival.sub = func => {
consumer = func;
};
ARCHIVAL.set(archival);
await getGrayTabby();

let group: HTMLDivElement = DOCUMENT.get().querySelector('#groups > div');
assert.equal(group, null);

consumer([testTab({ url: 'http://example.com' })], {}, () => null);
group = DOCUMENT.get().querySelector('#groups > div');
assert.notEqual(group, null);

mockBrowser.storage.local.get
.withArgs(INDEX_V2_KEY)
.returns(new Promise(() => [dateFromKey(group.id)]));
const a: HTMLAnchorElement = group.querySelector('a');
a.click();
await initGrayTabby();
});
});
12 changes: 0 additions & 12 deletions tests/testUtils.ts

This file was deleted.

51 changes: 51 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { BrowserTab } from '../@types/graytabby';
import * as assert from 'assert';
import { BROWSER, DOCUMENT } from '../src/globals';
import * as mockBrowser from 'sinon-chrome';
import { JSDOM } from 'jsdom';
import { INDEX_V1_KEY, INDEX_V2_KEY } from '../src/tabs';
import { OPTIONS_KEY } from '../src/options';
import { grayTabby } from '../src/ui';

export async function initGrayTabby(): Promise<void> {
BROWSER.set(<any>mockBrowser);
const jsdom = await JSDOM.fromFile('src/app.html');
DOCUMENT.set(jsdom.window.document);
mockBrowser.tabs.query.returns([]);
mockBrowser.storage.local.get.withArgs(INDEX_V1_KEY).returns('[]');
mockBrowser.storage.local.get.withArgs(OPTIONS_KEY).returns([]);
mockBrowser.storage.local.get.withArgs(INDEX_V2_KEY).returns([]);
try {
return await grayTabby();
} catch (err) {
assert.fail('Uncaught error: ' + err);
}
}

export function testTab(args: Partial<BrowserTab>): BrowserTab {
return {
index: 1,
highlighted: true,
active: true,
pinned: true,
incognito: false,
...args,
};
}

export function getElements(query: string, parent: ParentNode): Element[] {
const nodes = parent.querySelectorAll(query);
return Array.from(nodes);
}

export function assertElement(
query: string,
parent: ParentNode = null,
total = 1,
idx = 1,
): Element {
const nodes = getElements(query, parent);
assert.equal(nodes.length, total);
if (total != 0) return nodes[idx - 1];
return null;
}

0 comments on commit 6754394

Please sign in to comment.