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

Commit

Permalink
cover tab migration with test
Browse files Browse the repository at this point in the history
  • Loading branch information
moribellamy committed May 18, 2020
1 parent f3f83ed commit dc7864e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "graytabby",
"version": "19.12.11",
"version": "20.05.17",
"icons": {
"128": "assets/img/blobbycat/browseraction128.png"
},
Expand Down
8 changes: 8 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ export function fieldKeeper<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T,
return copy;
}

export function dictOf(...args: any[]): { [key: string]: any } {
const ret: { [key: string]: any } = {};
for (let i = 0; i < args.length; i += 2) {
ret[<string>args[i]] = args[i + 1];
}
return ret;
}

export async function save(key: string, value: any): Promise<void> {
const record: any = {};
record[key] = value;
Expand Down
10 changes: 5 additions & 5 deletions tests/archive.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { expect } from 'chai';
import * as mockBrowser from 'sinon-chrome';
import { archivePlan } from '../src/bg/archive';
import { Broker, BrokerConsumer } from '../src/lib/brokers';
import { ARCHIVAL, DOCUMENT } from '../src/lib/globals';
import { dateFromKey, INDEX_V2_KEY } from '../src/app/tabs';
import { assertElement, testTab, stubGlobals, unstubGlobals } from './utils';
import { assertElement, testTab, stubGlobals, unstubGlobals, mockedBrowser } from './utils';
import { BrowserTab } from '../src/lib/types';
import { graytabby } from '../src/app/ui';
import { dictOf } from '../src/lib/utils';

describe('archive operation', function() {
beforeEach(async function() {
Expand All @@ -32,9 +32,9 @@ describe('archive operation', function() {
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)]));
mockedBrowser()
.storage.local.get.withArgs(INDEX_V2_KEY)
.returns(Promise.resolve(dictOf(INDEX_V2_KEY, [dateFromKey(group.id)])));
const a = <HTMLAnchorElement>assertElement('a', group);
a.click();
});
Expand Down
52 changes: 52 additions & 0 deletions tests/tabs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect } from 'chai';
import { GrayTabGroup, INDEX_V1_KEY, loadAllTabGroups } from '../src/app/tabs';
import { dictOf } from '../src/lib/utils';
import { mockedBrowser, stubGlobals, unstubGlobals } from './utils';

describe('tabs', function() {
beforeEach(async function() {
await stubGlobals();
});

afterEach(function() {
unstubGlobals();
});

it('should load old string format', async function() {
const oldGroups: GrayTabGroup[] = [
{
tabs: [
{
url: '1',
title: 'one',
key: 0,
},
{
url: '2',
title: 'two',
key: 1,
},
],
date: 1589760256, // May 17 2020 in Linux Timestamp.
},
{
tabs: [
{
url: '3',
title: 'three',
key: 0,
},
],
date: 1589760257,
},
];
mockedBrowser()
.storage.local.get.withArgs(INDEX_V1_KEY)
.returns(Promise.resolve(dictOf(INDEX_V1_KEY, JSON.stringify(oldGroups))));
const groups = await loadAllTabGroups();
for (const oldGroup of oldGroups) {
oldGroup.date *= 1000; // Seconds to millis conversion.
}
expect(groups).to.deep.equal(oldGroups);
});
});
6 changes: 5 additions & 1 deletion tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { expect } from 'chai';
import { JSDOM } from 'jsdom';
import * as mockBrowser from 'sinon-chrome';
import { INDEX_V1_KEY, INDEX_V2_KEY } from '../src/app/tabs';
import { graytabby } from '../src/app/ui';
import { BROWSER, DOCUMENT } from '../src/lib/globals';
import { OPTIONS_KEY } from '../src/lib/options';
import { BrowserTab } from '../src/lib/types';
import SinonChrome from 'sinon-chrome';

export async function stubGlobals(): Promise<void> {
BROWSER.set(<any>mockBrowser);
Expand All @@ -22,6 +22,10 @@ export function unstubGlobals(): void {
DOCUMENT.set(null);
}

export function mockedBrowser(): typeof SinonChrome {
return <typeof SinonChrome>(<any>BROWSER.get());
}

export function testTab(args: Partial<BrowserTab>): BrowserTab {
return {
index: 1,
Expand Down

0 comments on commit dc7864e

Please sign in to comment.