Make find as a clean architecture

This commit is contained in:
Shin'ya Ueoka 2019-05-11 11:37:18 +09:00
parent bacf83a320
commit 1ba1660269
15 changed files with 424 additions and 182 deletions

View file

@ -1,22 +0,0 @@
import * as actions from 'content/actions';
import findReducer from 'content/reducers/find';
describe("find reducer", () => {
it('return the initial state', () => {
let state = findReducer(undefined, {});
expect(state).to.have.property('keyword', null);
expect(state).to.have.property('found', false);
});
it('return next state for FIND_SET_KEYWORD', () => {
let action = {
type: actions.FIND_SET_KEYWORD,
keyword: 'xyz',
found: true,
};
let state = findReducer({}, action);
expect(state.keyword).is.equal('xyz');
expect(state.found).to.be.true;
});
});

View file

@ -0,0 +1,15 @@
import { FindRepositoryImpl } from '../../../src/content/repositories/FindRepository';
import { expect } from 'chai';
describe('FindRepositoryImpl', () => {
it('updates and gets last keyword', () => {
let sut = new FindRepositoryImpl();
expect(sut.getLastKeyword()).to.be.null;
sut.setLastKeyword('monkey');
expect(sut.getLastKeyword()).to.equal('monkey');
});
});

View file

@ -0,0 +1,184 @@
import FindRepository from '../../../src/content/repositories/FindRepository';
import FindPresenter from '../../../src/content/presenters/FindPresenter';
import ConsoleClient from '../../../src/content/client/ConsoleClient';
import FindClient from '../../../src/content/client/FindClient';
import FindUseCase from '../../../src/content/usecases/FindUseCase';
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();
}
}
class MockConsoleClient implements ConsoleClient {
public isError: boolean;
public text: string;
constructor() {
this.isError = false;
this.text = '';
}
info(text: string): Promise<void> {
this.isError = false;
this.text = text;
return Promise.resolve();
}
error(text: string): Promise<void> {
this.isError = true;
this.text = text;
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(null);
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(null);
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(null);
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', () => {
});
});