aboutsummaryrefslogtreecommitdiff
path: root/test/content/usecases/FindUseCase.test.ts
blob: c7bfd39db92efdb5d86139860d3e1b41e00f7ffe (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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', () => {
  });
});