Fix ClipboardUseCase.test

jh-changes
Shin'ya Ueoka 6 years ago
parent 48e005dc82
commit 5133705dde
  1. 83
      test/content/usecases/ClipboardUseCase.test.ts

@ -1,82 +1,69 @@
import ClipboardRepository from '../../../src/content/repositories/ClipboardRepository'; import ClipboardRepository from '../../../src/content/repositories/ClipboardRepository';
import { SettingRepositoryImpl } from '../../../src/content/repositories/SettingRepository'; import { SettingRepositoryImpl } from '../../../src/content/repositories/SettingRepository';
import TabsClient from '../../../src/content/client/TabsClient';
import MockConsoleClient from '../mock/MockConsoleClient';
import ClipboardUseCase from '../../../src/content/usecases/ClipboardUseCase'; import ClipboardUseCase from '../../../src/content/usecases/ClipboardUseCase';
import { expect } from 'chai'; import OperationClient from '../../../src/content/client/OperationClient';
import ConsoleClient from '../../../src/content/client/ConsoleClient';
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> { import * as sinon from 'sinon';
this.last = url; import { expect } from 'chai';
return Promise.resolve();
}
}
describe('ClipboardUseCase', () => { describe('ClipboardUseCase', () => {
let repository: MockClipboardRepository; let clipboardRepository: ClipboardRepository;
let client: MockTabsClient; let operationClient: OperationClient;
let consoleClient: MockConsoleClient; let consoleClient: ConsoleClient;
let sut: ClipboardUseCase; let sut: ClipboardUseCase;
beforeEach(() => { beforeEach(() => {
repository = new MockClipboardRepository(); var modal = <ConsoleClient>{};
client = new MockTabsClient();
consoleClient = new MockConsoleClient(); clipboardRepository = <ClipboardRepository>{ read() {}, write(_) {} };
operationClient = <OperationClient>{ internalOpenUrl(_) {} };
consoleClient = <ConsoleClient>{ info(_) {}};
sut = new ClipboardUseCase( sut = new ClipboardUseCase(
repository, clipboardRepository,
new SettingRepositoryImpl(), new SettingRepositoryImpl(),
client, consoleClient,
consoleClient operationClient,
); );
}); });
describe('#yankCurrentURL', () => { describe('#yankCurrentURL', () => {
it('yanks current url', async () => { it('yanks current url', async () => {
let href = window.location.href;
var mockRepository = sinon.mock(clipboardRepository);
mockRepository.expects('write').withArgs(href);
var mockConsoleClient = sinon.mock(consoleClient);
mockConsoleClient.expects('info').withArgs('Yanked ' + href);
let yanked = await sut.yankCurrentURL(); let yanked = await sut.yankCurrentURL();
expect(yanked).to.equal(window.location.href); expect(yanked).to.equal(href);
expect(repository.clipboard).to.equal(yanked); mockRepository.verify();
expect(consoleClient.text).to.equal('Yanked ' + yanked); mockConsoleClient.verify();
}); });
}); });
describe('#openOrSearch', () => { describe('#openOrSearch', () => {
it('opens url from the clipboard', async () => { it('opens url from the clipboard', async () => {
let url = 'https://github.com/ueokande/vim-vixen' let url = 'https://github.com/ueokande/vim-vixen'
repository.clipboard = url; sinon.stub(clipboardRepository, 'read').returns(url);
let mockOperationClient = sinon.mock(operationClient);
mockOperationClient.expects('internalOpenUrl').withArgs(url, true);
await sut.openOrSearch(true); await sut.openOrSearch(true);
expect(client.last).to.equal(url); mockOperationClient.verify();
}); });
it('opens search results from the clipboard', async () => { it('opens search results from the clipboard', async () => {
repository.clipboard = 'banana'; let url = 'https://google.com/search?q=banana';
sinon.stub(clipboardRepository, 'read').returns('banana');
let mockOperationClient = sinon.mock(operationClient);
mockOperationClient.expects('internalOpenUrl').withArgs(url, true);
await sut.openOrSearch(true); await sut.openOrSearch(true);
expect(client.last).to.equal('https://google.com/search?q=banana'); mockOperationClient.verify();
}); });
}); });
}); });