aboutsummaryrefslogtreecommitdiff
path: root/test/background/operators/impls/RepeatLastOperator.test.ts
blob: 57f1227a6cd2b06b89faec026ffaad0507241742 (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
import RepeatLastOperator from "../../../../src/background/operators/impls/RepeatLastOperator";
import MockRepeatRepository from "../../mock/MockRepeatRepository";
import OperatorFactory from "../../../../src/background/operators/OperatorFactory";
import * as operations from "../../../../src/shared/operations";
import Operator from "../../../../src/background/operators/Operator";
import sinon from "sinon";

class MockOperatorFactory implements OperatorFactory {
  create(_op: operations.Operation): Operator {
    throw new Error("not implemented");
  }
}

class MockOperator implements Operator {
  run(): Promise<void> {
    throw new Error("not implemented");
  }
}

describe("RepeatLastOperator", () => {
  describe("#run", () => {
    it("repeat last operation", async () => {
      const operator = new MockOperator();
      const operatorMock = sinon.mock(operator).expects("run").once();
      const repeatRepository = new MockRepeatRepository();
      repeatRepository.setLastOperation({ type: operations.CANCEL });

      const operatorFactory = new MockOperatorFactory();
      const operatorFactoryMock = sinon
        .mock(operatorFactory)
        .expects("create")
        .withArgs({ type: operations.CANCEL });
      operatorFactoryMock.returns(operator);

      const sut = new RepeatLastOperator(repeatRepository, operatorFactory);
      await sut.run();

      operatorFactoryMock.verify();
      operatorMock.verify();
    });

    it("does nothing if no last operations", async () => {
      const repeatRepository = new MockRepeatRepository();
      const operatorFactory = new MockOperatorFactory();
      const operatorFactoryMock = sinon
        .mock(operatorFactory)
        .expects("create")
        .never();

      const sut = new RepeatLastOperator(repeatRepository, operatorFactory);
      await sut.run();

      operatorFactoryMock.verify();
    });
  });
});