From 6754394da7e32d72b580b0a78c5c063a30c5a7a7 Mon Sep 17 00:00:00 2001 From: Mori Bellamy Date: Fri, 15 May 2020 19:25:49 -0700 Subject: [PATCH] reorg tests --- tests/archive.test.ts | 29 +++++++++++++++++ tests/grayTabby.test.ts | 69 ++--------------------------------------- tests/testUtils.ts | 12 ------- tests/utils.ts | 51 ++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 79 deletions(-) create mode 100644 tests/archive.test.ts delete mode 100644 tests/testUtils.ts create mode 100644 tests/utils.ts diff --git a/tests/archive.test.ts b/tests/archive.test.ts new file mode 100644 index 0000000..f143f8c --- /dev/null +++ b/tests/archive.test.ts @@ -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('moreTabs'); + // Capture callback for tab archival so we can inject tabs in test. + let consumer: BrokerConsumer; + 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 = assertElement('a', group); + a.click(); + }); +}); diff --git a/tests/grayTabby.test.ts b/tests/grayTabby.test.ts index 0165edc..5ee7be1 100644 --- a/tests/grayTabby.test.ts +++ b/tests/grayTabby.test.ts @@ -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 { - 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(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 { - 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('moreTabs'); - // Capture callback for tab archival so we can inject tabs in test. - let consumer: BrokerConsumer; - 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(); }); }); diff --git a/tests/testUtils.ts b/tests/testUtils.ts deleted file mode 100644 index 713f253..0000000 --- a/tests/testUtils.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { BrowserTab } from '../@types/graytabby'; - -export function testTab(args: Partial): BrowserTab { - return { - index: 1, - highlighted: true, - active: true, - pinned: true, - incognito: false, - ...args, - }; -} diff --git a/tests/utils.ts b/tests/utils.ts new file mode 100644 index 0000000..269632c --- /dev/null +++ b/tests/utils.ts @@ -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 { + BROWSER.set(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 { + 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; +}