This repository has been archived by the owner on Feb 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51ef9c7
commit 6754394
Showing
4 changed files
with
82 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |