aboutsummaryrefslogtreecommitdiff
path: root/test/content/usecases/ClipboardUseCase.test.ts
blob: 862ee8abd7a328ebbb09531458bd9b066e36cdf0 (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
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');
    });
  });
});