1 | const { GroupOperation } = require('../src/GroupOperation');
|
2 | const { TestOperation } = require('./TestOperation');
|
3 |
|
4 | describe('GroupOperation', () => {
|
5 |
|
6 | describe('function GroupOperation', function () {
|
7 | test('should be defined', () => {
|
8 | expect(GroupOperation).toBeDefined();
|
9 | });
|
10 |
|
11 | test('should be instantiated', () => {
|
12 | const operation = new GroupOperation(1, () => {
|
13 | return true;
|
14 | });
|
15 |
|
16 | expect(operation).toBeDefined();
|
17 | });
|
18 |
|
19 | test('inherits from Operation', () => {
|
20 | const operation = new GroupOperation(1, () => {
|
21 | return true;
|
22 | });
|
23 |
|
24 | expect(operation.__proto__.__proto__.constructor.name).toBe('Operation');
|
25 | });
|
26 |
|
27 | test('should be valid type', () => {
|
28 | const operation = new GroupOperation(1, () => {
|
29 | return true;
|
30 | });
|
31 |
|
32 | expect(operation.constructor.name).toBe('GroupOperation');
|
33 | });
|
34 | });
|
35 |
|
36 | describe('function start', () => {
|
37 | test('using function addOperations, groupOperation ends when all of its dependencies are resolved', async (done) => {
|
38 | const operation1 = new TestOperation(1);
|
39 | const operation2 = new TestOperation(2);
|
40 | const groupOperation = new GroupOperation();
|
41 |
|
42 | groupOperation.addOperations([operation1, operation2]);
|
43 |
|
44 | let copyOperationState1 = null;
|
45 | operation1.completionCallback = op => {
|
46 | const { isCancelled, isExecuting, isFinished } = groupOperation;
|
47 | copyOperationState1 = { isCancelled, isExecuting, isFinished };
|
48 | }
|
49 | let copyOperationState2 = null;
|
50 | operation2.completionCallback = op => {
|
51 | const { isCancelled, isExecuting, isFinished } = groupOperation;
|
52 | copyOperationState2 = { isCancelled, isExecuting, isFinished };
|
53 | }
|
54 |
|
55 | const promise = groupOperation.start();
|
56 |
|
57 | await promise;
|
58 |
|
59 | expect(copyOperationState1.isExecuting).toBe(true);
|
60 | expect(copyOperationState1.isFinished).toBe(false);
|
61 | expect(copyOperationState1.isCancelled).toBe(false);
|
62 |
|
63 | expect(copyOperationState2.isExecuting).toBe(true);
|
64 | expect(copyOperationState2.isFinished).toBe(false);
|
65 | expect(copyOperationState2.isCancelled).toBe(false);
|
66 |
|
67 | expect(groupOperation.isExecuting).toBe(true);
|
68 | expect(groupOperation.isFinished).toBe(true);
|
69 | done();
|
70 | });
|
71 |
|
72 | test('using function addOperation, groupOperation ends when all of its dependencies are resolved', async (done) => {
|
73 | const operation1 = new TestOperation(1);
|
74 | const operation2 = new TestOperation(2);
|
75 | const groupOperation = new GroupOperation();
|
76 |
|
77 | groupOperation.addOperation(operation1);
|
78 | groupOperation.addOperation(operation2);
|
79 |
|
80 | let copyOperationState1 = null;
|
81 | operation1.completionCallback = op => {
|
82 | const { isCancelled, isExecuting, isFinished } = groupOperation;
|
83 | copyOperationState1 = { isCancelled, isExecuting, isFinished };
|
84 | }
|
85 | let copyOperationState2 = null;
|
86 | operation2.completionCallback = op => {
|
87 | const { isCancelled, isExecuting, isFinished } = groupOperation;
|
88 | copyOperationState2 = { isCancelled, isExecuting, isFinished };
|
89 | }
|
90 |
|
91 | const promise = groupOperation.start();
|
92 |
|
93 | await promise;
|
94 |
|
95 | expect(copyOperationState1.isExecuting).toBe(true);
|
96 | expect(copyOperationState1.isFinished).toBe(false);
|
97 | expect(copyOperationState1.isCancelled).toBe(false);
|
98 |
|
99 | expect(copyOperationState2.isExecuting).toBe(true);
|
100 | expect(copyOperationState2.isFinished).toBe(false);
|
101 | expect(copyOperationState2.isCancelled).toBe(false);
|
102 |
|
103 | expect(groupOperation.isExecuting).toBe(true);
|
104 | expect(groupOperation.isFinished).toBe(true);
|
105 | done();
|
106 | });
|
107 |
|
108 | test('groupOperation returns array of sorted results of operations', async (done) => {
|
109 | const operation1 = new TestOperation(1);
|
110 | const operation2 = new TestOperation(2);
|
111 | const groupOperation = new GroupOperation();
|
112 |
|
113 | groupOperation.addOperation(operation1);
|
114 | groupOperation.addOperation(operation2);
|
115 |
|
116 | const [result1, result2] = await groupOperation.start();
|
117 |
|
118 | expect(result1).toBe(operation1.result);
|
119 | expect(result2).toBe(operation2.result);
|
120 |
|
121 | done();
|
122 | })
|
123 | })
|
124 |
|
125 | }) |
\ | No newline at end of file |