aboutsummaryrefslogtreecommitdiff
path: root/test/content/usecases
diff options
context:
space:
mode:
Diffstat (limited to 'test/content/usecases')
-rw-r--r--test/content/usecases/AddonEnabledUseCase.test.ts90
-rw-r--r--test/content/usecases/ClipboardUseCase.test.ts76
-rw-r--r--test/content/usecases/FindUseCase.test.ts161
-rw-r--r--test/content/usecases/HintKeyProducer.test.ts25
-rw-r--r--test/content/usecases/MarkUseCase.test.ts107
-rw-r--r--test/content/usecases/SettingUseCaase.test.ts71
6 files changed, 530 insertions, 0 deletions
diff --git a/test/content/usecases/AddonEnabledUseCase.test.ts b/test/content/usecases/AddonEnabledUseCase.test.ts
new file mode 100644
index 0000000..912bddf
--- /dev/null
+++ b/test/content/usecases/AddonEnabledUseCase.test.ts
@@ -0,0 +1,90 @@
+import AddonEnabledRepository from '../../../src/content/repositories/AddonEnabledRepository';
+import AddonEnabledUseCase from '../../../src/content/usecases/AddonEnabledUseCase';
+import AddonIndicatorClient from '../../../src/content/client/AddonIndicatorClient';
+import { expect } from 'chai';
+
+class MockAddonEnabledRepository implements AddonEnabledRepository {
+ private enabled: boolean;
+
+ constructor(init: boolean) {
+ this.enabled = init;
+ }
+
+ set(on: boolean): void {
+ this.enabled = on;
+ }
+
+ get(): boolean {
+ return this.enabled;
+ }
+}
+
+class MockAddonIndicatorClient implements AddonIndicatorClient {
+ public enabled: boolean;
+
+ constructor(init: boolean) {
+ this.enabled = init;
+ }
+
+ async setEnabled(enabled: boolean): Promise<void> {
+ this.enabled = enabled;
+ return
+ }
+}
+
+describe('AddonEnabledUseCase', () => {
+ let repository: MockAddonEnabledRepository;
+ let indicator: MockAddonIndicatorClient;
+ let sut: AddonEnabledUseCase;
+
+ beforeEach(() => {
+ repository = new MockAddonEnabledRepository(true);
+ indicator = new MockAddonIndicatorClient(false);
+ sut = new AddonEnabledUseCase({ repository, indicator });
+ });
+
+ describe('#enable', () => {
+ it('store and indicate as enabled', async() => {
+ await sut.enable();
+
+ expect(repository.get()).to.be.true;
+ expect(indicator.enabled).to.be.true;
+ });
+ });
+
+ describe('#disable', async() => {
+ it('store and indicate as disabled', async() => {
+ await sut.disable();
+
+ expect(repository.get()).to.be.false;
+ expect(indicator.enabled).to.be.false;
+ });
+ });
+
+ describe('#toggle', () => {
+ it('toggled enabled and disabled', async() => {
+ repository.set(true);
+ await sut.toggle();
+
+ expect(repository.get()).to.be.false;
+ expect(indicator.enabled).to.be.false;
+
+ repository.set(false);
+
+ await sut.toggle();
+
+ expect(repository.get()).to.be.true;
+ expect(indicator.enabled).to.be.true;
+ });
+ });
+
+ describe('#getEnabled', () => {
+ it('returns current addon enabled', () => {
+ repository.set(true);
+ expect(sut.getEnabled()).to.be.true;
+
+ repository.set(false);
+ expect(sut.getEnabled()).to.be.false;
+ });
+ });
+});
diff --git a/test/content/usecases/ClipboardUseCase.test.ts b/test/content/usecases/ClipboardUseCase.test.ts
new file mode 100644
index 0000000..862ee8a
--- /dev/null
+++ b/test/content/usecases/ClipboardUseCase.test.ts
@@ -0,0 +1,76 @@
+import ClipboardRepository from '../../../src/content/repositories/ClipboardRepository';
+import TabsClient from '../../../src/content/client/TabsClient';
+import MockConsoleClient from '../mock/MockConsoleClient';
+import ClipboardUseCase from '../../../src/content/usecases/ClipboardUseCase';
+import { expect } from 'chai';
+
+class MockClipboardRepository implements ClipboardRepository {
+ public clipboard: string;
+
+ constructor() {
+ this.clipboard = '';
+ }
+
+ read(): string {
+ return this.clipboard;
+ }
+
+ write(text: string): void {
+ this.clipboard = text;
+ }
+}
+
+class MockTabsClient implements TabsClient {
+ public last: string;
+
+ constructor() {
+ this.last = '';
+ }
+
+ openUrl(url: string, _newTab: boolean): Promise<void> {
+ this.last = url;
+ return Promise.resolve();
+ }
+}
+
+describe('ClipboardUseCase', () => {
+ let repository: MockClipboardRepository;
+ let client: MockTabsClient;
+ let consoleClient: MockConsoleClient;
+ let sut: ClipboardUseCase;
+
+ beforeEach(() => {
+ repository = new MockClipboardRepository();
+ client = new MockTabsClient();
+ consoleClient = new MockConsoleClient();
+ sut = new ClipboardUseCase({ repository, client: client, consoleClient });
+ });
+
+ describe('#yankCurrentURL', () => {
+ it('yanks current url', async () => {
+ let yanked = await sut.yankCurrentURL();
+
+ expect(yanked).to.equal(window.location.href);
+ expect(repository.clipboard).to.equal(yanked);
+ expect(consoleClient.text).to.equal('Yanked ' + yanked);
+ });
+ });
+
+ describe('#openOrSearch', () => {
+ it('opens url from the clipboard', async () => {
+ let url = 'https://github.com/ueokande/vim-vixen'
+ repository.clipboard = url;
+ await sut.openOrSearch(true);
+
+ expect(client.last).to.equal(url);
+ });
+
+ it('opens search results from the clipboard', async () => {
+ repository.clipboard = 'banana';
+ await sut.openOrSearch(true);
+
+ expect(client.last).to.equal('https://google.com/search?q=banana');
+ });
+ });
+});
+
diff --git a/test/content/usecases/FindUseCase.test.ts b/test/content/usecases/FindUseCase.test.ts
new file mode 100644
index 0000000..c7bfd39
--- /dev/null
+++ b/test/content/usecases/FindUseCase.test.ts
@@ -0,0 +1,161 @@
+import FindRepository from '../../../src/content/repositories/FindRepository';
+import FindPresenter from '../../../src/content/presenters/FindPresenter';
+import FindClient from '../../../src/content/client/FindClient';
+import FindUseCase from '../../../src/content/usecases/FindUseCase';
+import MockConsoleClient from '../mock/MockConsoleClient';
+import { expect } from 'chai';
+
+class MockFindRepository implements FindRepository {
+ public keyword: string | null;
+
+ constructor() {
+ this.keyword = null;
+ }
+
+ getLastKeyword(): string | null {
+ return this.keyword;
+ }
+
+ setLastKeyword(keyword: string): void {
+ this.keyword = keyword;
+ }
+}
+
+class MockFindPresenter implements FindPresenter {
+ public document: string;
+
+ public highlighted: boolean;
+
+ constructor() {
+ this.document = '';
+ this.highlighted = false;
+ }
+
+ find(keyword: string, _backward: boolean): boolean {
+ let found = this.document.includes(keyword);
+ this.highlighted = found;
+ return found;
+ }
+
+ clearSelection(): void {
+ this.highlighted = false;
+ }
+}
+
+class MockFindClient implements FindClient {
+ public keyword: string | null;
+
+ constructor() {
+ this.keyword = null;
+ }
+
+ getGlobalLastKeyword(): Promise<string | null> {
+ return Promise.resolve(this.keyword);
+ }
+
+ setGlobalLastKeyword(keyword: string): Promise<void> {
+ this.keyword = keyword;
+ return Promise.resolve();
+ }
+}
+
+describe('FindUseCase', () => {
+ let repository: MockFindRepository;
+ let presenter: MockFindPresenter;
+ let client: MockFindClient;
+ let consoleClient: MockConsoleClient;
+ let sut: FindUseCase;
+
+ beforeEach(() => {
+ repository = new MockFindRepository();
+ presenter = new MockFindPresenter();
+ client = new MockFindClient();
+ consoleClient = new MockConsoleClient();
+ sut = new FindUseCase({ repository, presenter, client, consoleClient });
+ });
+
+ describe('#startFind', () => {
+ it('find next by ketword', async() => {
+ presenter.document = 'monkey punch';
+
+ await sut.startFind('monkey');
+
+ expect(await presenter.highlighted).to.be.true;
+ expect(await consoleClient.text).to.equal('Pattern found: monkey');
+ expect(await repository.getLastKeyword()).to.equal('monkey');
+ expect(await client.getGlobalLastKeyword()).to.equal('monkey');
+ });
+
+ it('find next by last keyword', async() => {
+ presenter.document = 'gorilla kick';
+ repository.keyword = 'gorilla';
+
+ await sut.startFind(undefined);
+
+ expect(await presenter.highlighted).to.be.true;
+ expect(await consoleClient.text).to.equal('Pattern found: gorilla');
+ expect(await repository.getLastKeyword()).to.equal('gorilla');
+ expect(await client.getGlobalLastKeyword()).to.equal('gorilla');
+ });
+
+ it('find next by global last keyword', async() => {
+ presenter.document = 'chimpanzee typing';
+
+ repository.keyword = null;
+ client.keyword = 'chimpanzee';
+
+ await sut.startFind(undefined);
+
+ expect(await presenter.highlighted).to.be.true;
+ expect(await consoleClient.text).to.equal('Pattern found: chimpanzee');
+ expect(await repository.getLastKeyword()).to.equal('chimpanzee');
+ expect(await client.getGlobalLastKeyword()).to.equal('chimpanzee');
+ });
+
+ it('find not found error', async() => {
+ presenter.document = 'zoo';
+
+ await sut.startFind('giraffe');
+
+ expect(await presenter.highlighted).to.be.false;
+ expect(await consoleClient.text).to.equal('Pattern not found: giraffe');
+ expect(await repository.getLastKeyword()).to.equal('giraffe');
+ expect(await client.getGlobalLastKeyword()).to.equal('giraffe');
+ });
+
+ it('show errors when no last keywords', async() => {
+ repository.keyword = null;
+ client.keyword = null;
+
+ await sut.startFind(undefined);
+
+ expect(await consoleClient.text).to.equal('No previous search keywords');
+ expect(await consoleClient.isError).to.be.true;
+ });
+ });
+
+ describe('#findNext', () => {
+ it('finds by last keyword', async() => {
+ presenter.document = 'monkey punch';
+ repository.keyword = 'monkey';
+
+ await sut.findNext();
+
+ expect(await presenter.highlighted).to.be.true;
+ expect(await consoleClient.text).to.equal('Pattern found: monkey');
+ });
+
+ it('show errors when no last keywords', async() => {
+ repository.keyword = null;
+ client.keyword = null;
+
+ await sut.findNext();
+
+ expect(await consoleClient.text).to.equal('No previous search keywords');
+ expect(await consoleClient.isError).to.be.true;
+ });
+ });
+
+ describe('#findPrev', () => {
+ });
+});
diff --git a/test/content/usecases/HintKeyProducer.test.ts b/test/content/usecases/HintKeyProducer.test.ts
new file mode 100644
index 0000000..feafffb
--- /dev/null
+++ b/test/content/usecases/HintKeyProducer.test.ts
@@ -0,0 +1,25 @@
+import HintKeyProducer from '../../../src/content/usecases/HintKeyProducer';
+import { expect } from 'chai';
+
+describe('HintKeyProducer class', () => {
+ describe('#constructor', () => {
+ it('throws an exception on empty charset', () => {
+ expect(() => new HintKeyProducer('')).to.throw(TypeError);
+ });
+ });
+
+ describe('#produce', () => {
+ it('produce incremented keys', () => {
+ let charset = 'abc';
+ let sequences = [
+ 'a', 'b', 'c',
+ 'aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc',
+ 'aaa', 'aab', 'aac', 'aba']
+
+ let producer = new HintKeyProducer(charset);
+ for (let i = 0; i < sequences.length; ++i) {
+ expect(producer.produce()).to.equal(sequences[i]);
+ }
+ });
+ });
+});
diff --git a/test/content/usecases/MarkUseCase.test.ts b/test/content/usecases/MarkUseCase.test.ts
new file mode 100644
index 0000000..4f2dee4
--- /dev/null
+++ b/test/content/usecases/MarkUseCase.test.ts
@@ -0,0 +1,107 @@
+import MarkRepository from '../../../src/content/repositories/MarkRepository';
+import MarkUseCase from '../../../src/content/usecases/MarkUseCase';
+import MarkClient from '../../../src/content/client/MarkClient';
+import MockConsoleClient from '../mock/MockConsoleClient';
+import MockScrollPresenter from '../mock/MockScrollPresenter';
+import Mark from '../../../src/content/domains/Mark';
+import { expect } from 'chai';
+
+class MockMarkRepository implements MarkRepository {
+ private current: {[key: string]: Mark};
+
+ constructor() {
+ this.current = {};
+ }
+
+ set(key: string, mark: Mark): void {
+ this.current[key] = mark;
+ }
+
+ get(key: string): Mark | null {
+ return this.current[key];
+ }
+}
+
+class MockMarkClient implements MarkClient {
+ public marks: {[key: string]: Mark};
+ public last: string;
+
+ constructor() {
+ this.marks = {};
+ this.last = '';
+ }
+
+ setGloablMark(key: string, mark: Mark): Promise<void> {
+ this.marks[key] = mark;
+ return Promise.resolve();
+ }
+
+ jumpGlobalMark(key: string): Promise<void> {
+ this.last = key
+ return Promise.resolve();
+ }
+}
+
+describe('MarkUseCase', () => {
+ let repository: MockMarkRepository;
+ let client: MockMarkClient;
+ let consoleClient: MockConsoleClient;
+ let scrollPresenter: MockScrollPresenter;
+ let sut: MarkUseCase;
+
+ beforeEach(() => {
+ repository = new MockMarkRepository();
+ client = new MockMarkClient();
+ consoleClient = new MockConsoleClient();
+ scrollPresenter = new MockScrollPresenter();
+ sut = new MarkUseCase({
+ repository, client, consoleClient, scrollPresenter,
+ });
+ });
+
+ describe('#set', () => {
+ it('sets local mark', async() => {
+ scrollPresenter.scrollTo(10, 20, false);
+
+ await sut.set('x');
+
+ expect(repository.get('x')).to.deep.equals({ x: 10, y: 20 });
+ expect(consoleClient.text).to.equal("Set local mark to 'x'");
+ });
+
+ it('sets global mark', async() => {
+ scrollPresenter.scrollTo(30, 40, false);
+
+ await sut.set('Z');
+
+ expect(client.marks['Z']).to.deep.equals({ x: 30, y: 40 });
+ expect(consoleClient.text).to.equal("Set global mark to 'Z'");
+ });
+ });
+
+ describe('#jump', () => {
+ it('jumps to local mark', async() => {
+ repository.set('x', { x: 20, y: 40 });
+
+ await sut.jump('x');
+
+ expect(scrollPresenter.getScroll()).to.deep.equals({ x: 20, y: 40 });
+ });
+
+ it('throws an error when no local marks', () => {
+ return sut.jump('a').then(() => {
+ throw new Error('error');
+ }).catch((e) => {
+ expect(e).to.be.instanceof(Error);
+ })
+ })
+
+ it('jumps to global mark', async() => {
+ client.marks['Z'] = { x: 20, y: 0 };
+
+ await sut.jump('Z');
+
+ expect(client.last).to.equal('Z')
+ });
+ });
+});
diff --git a/test/content/usecases/SettingUseCaase.test.ts b/test/content/usecases/SettingUseCaase.test.ts
new file mode 100644
index 0000000..02cef78
--- /dev/null
+++ b/test/content/usecases/SettingUseCaase.test.ts
@@ -0,0 +1,71 @@
+import SettingRepository from '../../../src/content/repositories/SettingRepository';
+import SettingClient from '../../../src/content/client/SettingClient';
+import SettingUseCase from '../../../src/content/usecases/SettingUseCase';
+import Settings, { DefaultSetting } from '../../../src/shared/Settings';
+import { expect } from 'chai';
+
+class MockSettingRepository implements SettingRepository {
+ private current: Settings;
+
+ constructor() {
+ this.current = DefaultSetting;
+ }
+
+ set(settings: Settings): void {
+ this.current= settings;
+ }
+
+ get(): Settings {
+ return this.current;
+ }
+}
+
+class MockSettingClient implements SettingClient {
+ private data: Settings;
+
+ constructor(data: Settings) {
+ this.data = data;
+ }
+
+ load(): Promise<Settings> {
+ return Promise.resolve(this.data);
+ }
+}
+
+describe('AddonEnabledUseCase', () => {
+ let repository: MockSettingRepository;
+ let client: MockSettingClient;
+ let sut: SettingUseCase;
+
+ beforeEach(() => {
+ let testSettings = {
+ keymaps: {},
+ search: {
+ default: 'google',
+ engines: {
+ google: 'https://google.com/?q={}',
+ }
+ },
+ properties: {
+ hintchars: 'abcd1234',
+ smoothscroll: false,
+ complete: 'sbh',
+ },
+ blacklist: [],
+ };
+
+ repository = new MockSettingRepository();
+ client = new MockSettingClient(testSettings);
+ sut = new SettingUseCase({ repository, client });
+ });
+
+ describe('#reload', () => {
+ it('loads settings and store to repository', async() => {
+ let settings = await sut.reload();
+ expect(settings.properties.hintchars).to.equal('abcd1234');
+
+ let saved = repository.get();
+ expect(saved.properties.hintchars).to.equal('abcd1234');
+ });
+ });
+});