1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
import "reflect-metadata"
import TabRepositoryImpl from "../../../src/background/completion/impl/TabRepositoryImpl";
import {Tab} from "../../../src/background/completion/TabRepository";
import TabPresenter from "../../../src/background/presenters/TabPresenter";
import TabCompletionUseCase from "../../../src/background/completion/TabCompletionUseCase";
import sinon from 'sinon';
import { expect } from 'chai';
import TabFlag from "../../../src/shared/TabFlag";
class MockTabRepository implements TabRepositoryImpl {
async queryTabs(_query: string, _excludePinned: boolean): Promise<Tab[]> {
throw new Error("not implemented")
}
async getAllTabs(_excludePinned: boolean): Promise<Tab[]> {
throw new Error("not implemented")
}
}
class MockTabPresenter implements TabPresenter {
create(_url: string, _opts?: object): Promise<browser.tabs.Tab> {
throw new Error("not implemented")
}
duplicate(_id: number): Promise<browser.tabs.Tab> {
throw new Error("not implemented")
}
getAll(): Promise<browser.tabs.Tab[]> {
throw new Error("not implemented")
}
getByKeyword(_keyword: string, _excludePinned: boolean): Promise<browser.tabs.Tab[]> {
throw new Error("not implemented")
}
getCurrent(): Promise<browser.tabs.Tab> {
throw new Error("not implemented")
}
getLastSelectedId(): Promise<number | undefined> {
throw new Error("not implemented")
}
getZoom(_tabId: number): Promise<number> {
throw new Error("not implemented")
}
onSelected(_listener: (arg: { tabId: number; windowId: number }) => void): void {
throw new Error("not implemented")
}
open(_url: string, _tabId?: number): Promise<browser.tabs.Tab> {
throw new Error("not implemented")
}
reload(_tabId: number, _cache: boolean): Promise<void> {
throw new Error("not implemented")
}
remove(_ids: number[]): Promise<void> {
throw new Error("not implemented")
}
reopen(): Promise<any> {
throw new Error("not implemented")
}
select(_tabId: number): Promise<void> {
throw new Error("not implemented")
}
setPinned(_tabId: number, _pinned: boolean): Promise<void> {
throw new Error("not implemented")
}
setZoom(_tabId: number, _factor: number): Promise<void> {
throw new Error("not implemented")
}
}
describe('TabCompletionUseCase', () => {
let tabRepository: MockTabRepository;
let tabPresenter: TabPresenter;
let sut: TabCompletionUseCase;
beforeEach(() => {
tabRepository = new MockTabRepository();
tabPresenter = new MockTabPresenter();
sut = new TabCompletionUseCase(tabRepository, tabPresenter);
sinon.stub(tabPresenter, 'getLastSelectedId').returns(Promise.resolve(12));
sinon.stub(tabRepository, 'getAllTabs').returns(Promise.resolve([
{ id: 10, index: 0, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', active: false },
{ id: 11, index: 1, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', active: true },
{ id: 12, index: 2, title: 'Bing', url: 'https://bing.com/', active: false },
]));
});
describe('#queryTabs', () => {
it("returns tab items", async () => {
sinon.stub(tabRepository, 'queryTabs').withArgs('', false).returns(Promise.resolve([
{ id: 10, index: 0, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', active: false },
{ id: 11, index: 1, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', active: true },
{ id: 12, index: 2, title: 'Bing', url: 'https://bing.com/', active: false },
])).withArgs('oo', false).returns(Promise.resolve([
{ id: 10, index: 0, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', active: false },
{ id: 11, index: 1, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', active: true },
]));
expect(await sut.queryTabs('', false)).to.deep.equal([
{ index: 1, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', flag: TabFlag.None },
{ index: 2, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', flag: TabFlag.CurrentTab },
{ index: 3, title: 'Bing', url: 'https://bing.com/', faviconUrl: undefined, flag: TabFlag.LastTab },
]);
expect(await sut.queryTabs('oo', false)).to.deep.equal([
{ index: 1, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', flag: TabFlag.None },
{ index: 2, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', flag: TabFlag.CurrentTab },
]);
});
it("returns a tab by the index", async () => {
expect(await sut.queryTabs('1', false)).to.deep.equal([
{ index: 1, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', flag: TabFlag.None },
]);
expect(await sut.queryTabs('10', false)).to.be.empty;
expect(await sut.queryTabs('-1', false)).to.be.empty;
});
it("returns the current tab by % flag", async () => {
expect(await sut.queryTabs('%', false)).to.deep.equal([
{ index: 2, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', flag: TabFlag.CurrentTab },
]);
});
it("returns the current tab by # flag", async () => {
expect(await sut.queryTabs('#', false)).to.deep.equal([
{ index: 3, title: 'Bing', url: 'https://bing.com/', faviconUrl: undefined, flag: TabFlag.LastTab },
]);
})
});
});
|