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

Commit e43a0f9

Browse files
committed
cover tab migration with test
1 parent f3f83ed commit e43a0f9

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

src/lib/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ export function fieldKeeper<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T,
77
return copy;
88
}
99

10+
export function dictOf(...args: any[]): { [key: string]: any } {
11+
const ret: { [key: string]: any } = {};
12+
for (let i = 0; i < args.length; i += 2) {
13+
ret[<string>args[i]] = args[i + 1];
14+
}
15+
return ret;
16+
}
17+
1018
export async function save(key: string, value: any): Promise<void> {
1119
const record: any = {};
1220
record[key] = value;

tests/archive.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { expect } from 'chai';
2-
import * as mockBrowser from 'sinon-chrome';
32
import { archivePlan } from '../src/bg/archive';
43
import { Broker, BrokerConsumer } from '../src/lib/brokers';
54
import { ARCHIVAL, DOCUMENT } from '../src/lib/globals';
65
import { dateFromKey, INDEX_V2_KEY } from '../src/app/tabs';
7-
import { assertElement, testTab, stubGlobals, unstubGlobals } from './utils';
6+
import { assertElement, testTab, stubGlobals, unstubGlobals, mockedBrowser } from './utils';
87
import { BrowserTab } from '../src/lib/types';
98
import { graytabby } from '../src/app/ui';
9+
import { dictOf } from '../src/lib/utils';
1010

1111
describe('archive operation', function() {
1212
beforeEach(async function() {
@@ -32,9 +32,9 @@ describe('archive operation', function() {
3232
consumer([testTab({ url: 'http://example.com' })], {}, () => null);
3333
const group = assertElement('#groups > div', DOCUMENT.get());
3434

35-
mockBrowser.storage.local.get
36-
.withArgs(INDEX_V2_KEY)
37-
.returns(new Promise(() => [dateFromKey(group.id)]));
35+
mockedBrowser()
36+
.storage.local.get.withArgs(INDEX_V2_KEY)
37+
.returns(Promise.resolve(dictOf(INDEX_V2_KEY, [dateFromKey(group.id)])));
3838
const a = <HTMLAnchorElement>assertElement('a', group);
3939
a.click();
4040
});

tests/tabs.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { expect } from 'chai';
2+
import { GrayTabGroup, INDEX_V1_KEY, loadAllTabGroups } from '../src/app/tabs';
3+
import { dictOf } from '../src/lib/utils';
4+
import { mockedBrowser, stubGlobals, unstubGlobals } from './utils';
5+
6+
describe('tabs', function() {
7+
beforeEach(async function() {
8+
await stubGlobals();
9+
});
10+
11+
afterEach(function() {
12+
unstubGlobals();
13+
});
14+
15+
it('should load old string format', async function() {
16+
const oldGroups: GrayTabGroup[] = [
17+
{
18+
tabs: [
19+
{
20+
url: '1',
21+
title: 'one',
22+
key: 0,
23+
},
24+
{
25+
url: '2',
26+
title: 'two',
27+
key: 1,
28+
},
29+
],
30+
date: 1589760256, // May 17 2020 in Linux Timestamp.
31+
},
32+
{
33+
tabs: [
34+
{
35+
url: '3',
36+
title: 'three',
37+
key: 0,
38+
},
39+
],
40+
date: 1589760257,
41+
},
42+
];
43+
mockedBrowser()
44+
.storage.local.get.withArgs(INDEX_V1_KEY)
45+
.returns(Promise.resolve(dictOf(INDEX_V1_KEY, JSON.stringify(oldGroups))));
46+
const groups = await loadAllTabGroups();
47+
for (const oldGroup of oldGroups) {
48+
oldGroup.date *= 1000; // Seconds to millis conversion.
49+
}
50+
expect(groups).to.deep.equal(oldGroups);
51+
});
52+
});

tests/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { expect } from 'chai';
22
import { JSDOM } from 'jsdom';
33
import * as mockBrowser from 'sinon-chrome';
44
import { INDEX_V1_KEY, INDEX_V2_KEY } from '../src/app/tabs';
5-
import { graytabby } from '../src/app/ui';
65
import { BROWSER, DOCUMENT } from '../src/lib/globals';
76
import { OPTIONS_KEY } from '../src/lib/options';
87
import { BrowserTab } from '../src/lib/types';
8+
import SinonChrome from 'sinon-chrome';
99

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

25+
export function mockedBrowser(): typeof SinonChrome {
26+
return <typeof SinonChrome>(<any>BROWSER.get());
27+
}
28+
2529
export function testTab(args: Partial<BrowserTab>): BrowserTab {
2630
return {
2731
index: 1,

0 commit comments

Comments
 (0)