-
Notifications
You must be signed in to change notification settings - Fork 134
/
strategy-test.ts
196 lines (151 loc) · 4.94 KB
/
strategy-test.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { Coordinator, ActivationOptions, LogLevel } from '../src/coordinator';
import { Strategy, StrategyOptions } from '../src/strategy';
import { Source } from '@orbit/data';
const { module, test } = QUnit;
module('Strategy', function (hooks) {
class MySource extends Source {}
let coordinator: Coordinator;
let strategy: Strategy;
let s1: MySource;
let s2: MySource;
let s3: MySource;
hooks.beforeEach(function () {
s1 = new MySource({ name: 's1' });
s2 = new MySource({ name: 's2' });
s3 = new MySource({ name: 's3' });
coordinator = new Coordinator({ sources: [s1, s2, s3] });
});
test('can be instantiated with a name', function (assert) {
class CustomStrategy extends Strategy {}
strategy = new CustomStrategy({
name: 'custom'
});
assert.ok(strategy);
});
test('requires a name', function (assert) {
class CustomStrategy extends Strategy {}
assert.throws(
function () {
strategy = new CustomStrategy();
},
Error('Assertion failed: Strategy requires a name'),
'assertion raised'
);
});
test('uses its name as the basis for a logPrefix', function (assert) {
class CustomStrategy extends Strategy {}
strategy = new CustomStrategy({
name: 'custom'
});
assert.equal(strategy.logPrefix, '[custom]');
});
test('can specify a custom logPrefix', function (assert) {
class CustomStrategy extends Strategy {}
strategy = new CustomStrategy({
name: 'custom',
logPrefix: '[foo-bar]'
});
assert.equal(strategy.logPrefix, '[foo-bar]');
});
test('applies to all sources by default', async function (assert) {
assert.expect(1);
class CustomStrategy extends Strategy {
constructor(options: StrategyOptions = {}) {
options.name = 'custom';
super(options);
}
async activate(
coordinator: Coordinator,
options: ActivationOptions = {}
): Promise<void> {
await super.activate(coordinator, options);
assert.deepEqual(this._sources, [s1, s2, s3]);
}
}
strategy = new CustomStrategy();
await strategy.activate(coordinator);
});
test('can include only specific sources', async function (assert) {
assert.expect(1);
class CustomStrategy extends Strategy {
constructor(options: StrategyOptions = {}) {
options.name = 'custom';
super(options);
}
async activate(
coordinator: Coordinator,
options: ActivationOptions = {}
): Promise<void> {
await super.activate(coordinator, options);
assert.deepEqual(this._sources, [s1, s2]);
}
}
strategy = new CustomStrategy({ sources: ['s1', 's2'] });
await strategy.activate(coordinator);
});
test('#activate - receives the `logLevel` from the coordinator', async function (assert) {
assert.expect(2);
class CustomStrategy extends Strategy {
constructor(options: StrategyOptions = {}) {
options.name = 'custom';
super(options);
assert.equal(
this.logLevel,
undefined,
'_logLevel is initially undefined'
);
}
async activate(
coordinator: Coordinator,
options: ActivationOptions = {}
): Promise<void> {
await super.activate(coordinator, options);
assert.equal(
this.logLevel,
LogLevel.Warnings,
'_logLevel is set by activate'
);
}
}
strategy = new CustomStrategy();
coordinator.addStrategy(strategy);
await coordinator.activate({ logLevel: LogLevel.Warnings });
});
test('a custom `logLevel` will override the level from the coordinator', async function (assert) {
assert.expect(2);
class CustomStrategy extends Strategy {
constructor(options: StrategyOptions = {}) {
options.name = 'custom';
super(options);
assert.equal(this.logLevel, LogLevel.Errors, '_logLevel is custom');
}
async activate(
coordinator: Coordinator,
options: ActivationOptions = {}
): Promise<void> {
await super.activate(coordinator, options);
assert.equal(
this.logLevel,
LogLevel.Errors,
'_logLevel is custom even after activate'
);
}
}
strategy = new CustomStrategy({ logLevel: LogLevel.Errors });
coordinator.addStrategy(strategy);
await coordinator.activate({ logLevel: LogLevel.Warnings });
});
test('activate sources', async function (assert) {
class CustomStrategy extends Strategy {}
strategy = new CustomStrategy({
name: 'custom',
sources: ['s1', 's2', 's3']
});
assert.equal(strategy.sources.length, 0, 'no sources activated');
coordinator.addStrategy(strategy);
await coordinator.activate();
assert.equal(strategy.sources.length, 3, '3 sources activated');
assert.ok(strategy.sources.map((s) => s.activated));
await coordinator.deactivate();
});
});