|
|
@@ -1,20 +1,29 @@
|
|
|
1
|
+import * as _ from 'lodash';
|
|
1
|
2
|
import { LayoutTreeParser } from './LayoutTreeParser';
|
|
2
|
3
|
import { LayoutTreeCrawler } from './LayoutTreeCrawler';
|
|
3
|
4
|
import { Store } from '../components/Store';
|
|
4
|
5
|
import { UniqueIdProvider } from '../adapters/UniqueIdProvider.mock';
|
|
5
|
6
|
import { NativeCommandsSender } from '../adapters/NativeCommandsSender.mock';
|
|
6
|
7
|
import { Commands } from './Commands';
|
|
|
8
|
+import { CommandsObserver } from '../events/CommandsObserver';
|
|
7
|
9
|
|
|
8
|
10
|
describe('Commands', () => {
|
|
9
|
|
- let uut;
|
|
|
11
|
+ let uut: Commands;
|
|
10
|
12
|
let mockCommandsSender;
|
|
11
|
13
|
let store;
|
|
|
14
|
+ let commandsObserver: CommandsObserver;
|
|
12
|
15
|
|
|
13
|
16
|
beforeEach(() => {
|
|
14
|
17
|
mockCommandsSender = new NativeCommandsSender();
|
|
15
|
18
|
store = new Store();
|
|
16
|
|
-
|
|
17
|
|
- uut = new Commands(mockCommandsSender, new LayoutTreeParser(), new LayoutTreeCrawler(new UniqueIdProvider(), store));
|
|
|
19
|
+ commandsObserver = new CommandsObserver();
|
|
|
20
|
+
|
|
|
21
|
+ uut = new Commands(
|
|
|
22
|
+ mockCommandsSender,
|
|
|
23
|
+ new LayoutTreeParser(),
|
|
|
24
|
+ new LayoutTreeCrawler(new UniqueIdProvider(), store),
|
|
|
25
|
+ commandsObserver
|
|
|
26
|
+ );
|
|
18
|
27
|
});
|
|
19
|
28
|
|
|
20
|
29
|
describe('setRoot', () => {
|
|
|
@@ -183,9 +192,9 @@ describe('Commands', () => {
|
|
183
|
192
|
|
|
184
|
193
|
describe('pop', () => {
|
|
185
|
194
|
it('pops a component, passing componentId', () => {
|
|
186
|
|
- uut.pop('theComponentId');
|
|
|
195
|
+ uut.pop('theComponentId', {});
|
|
187
|
196
|
expect(mockCommandsSender.pop).toHaveBeenCalledTimes(1);
|
|
188
|
|
- expect(mockCommandsSender.pop).toHaveBeenCalledWith('theComponentId', undefined);
|
|
|
197
|
+ expect(mockCommandsSender.pop).toHaveBeenCalledWith('theComponentId', {});
|
|
189
|
198
|
});
|
|
190
|
199
|
it('pops a component, passing componentId and options', () => {
|
|
191
|
200
|
const options = {
|
|
|
@@ -203,7 +212,7 @@ describe('Commands', () => {
|
|
203
|
212
|
|
|
204
|
213
|
it('pop returns a promise that resolves to componentId', async () => {
|
|
205
|
214
|
mockCommandsSender.pop.mockReturnValue(Promise.resolve('theComponentId'));
|
|
206
|
|
- const result = await uut.pop('theComponentId');
|
|
|
215
|
+ const result = await uut.pop('theComponentId', {});
|
|
207
|
216
|
expect(result).toEqual('theComponentId');
|
|
208
|
217
|
});
|
|
209
|
218
|
});
|
|
|
@@ -282,4 +291,58 @@ describe('Commands', () => {
|
|
282
|
291
|
expect(mockCommandsSender.dismissOverlay).toHaveBeenCalledWith('Component1');
|
|
283
|
292
|
});
|
|
284
|
293
|
});
|
|
|
294
|
+
|
|
|
295
|
+ describe('notifies commandsObserver', () => {
|
|
|
296
|
+ let cb;
|
|
|
297
|
+
|
|
|
298
|
+ beforeEach(() => {
|
|
|
299
|
+ cb = jest.fn();
|
|
|
300
|
+ const mockParser = { parse: () => 'parsed' };
|
|
|
301
|
+ const mockCrawler = { crawl: (x) => x };
|
|
|
302
|
+ commandsObserver.register(cb);
|
|
|
303
|
+ uut = new Commands(mockCommandsSender, mockParser, mockCrawler, commandsObserver);
|
|
|
304
|
+ });
|
|
|
305
|
+
|
|
|
306
|
+ it('always call last', () => {
|
|
|
307
|
+ const nativeCommandsSenderFns = _.functions(mockCommandsSender);
|
|
|
308
|
+ expect(nativeCommandsSenderFns.length).toBeGreaterThan(1);
|
|
|
309
|
+
|
|
|
310
|
+ // throw when calling any native commands sender
|
|
|
311
|
+ _.forEach(nativeCommandsSenderFns, (fn) => {
|
|
|
312
|
+ mockCommandsSender[fn].mockImplementation(() => {
|
|
|
313
|
+ throw new Error(`throwing from mockNativeCommandsSender`);
|
|
|
314
|
+ });
|
|
|
315
|
+ });
|
|
|
316
|
+
|
|
|
317
|
+ // call all commands on uut, all should throw, no commandObservers called
|
|
|
318
|
+ const uutFns = Object.getOwnPropertyNames(Commands.prototype);
|
|
|
319
|
+ const methods = _.filter(uutFns, (fn) => fn !== 'constructor');
|
|
|
320
|
+ expect(methods.sort()).toEqual(nativeCommandsSenderFns.sort());
|
|
|
321
|
+
|
|
|
322
|
+ _.forEach(methods, (m) => {
|
|
|
323
|
+ expect(() => uut[m]()).toThrow();
|
|
|
324
|
+ expect(cb).not.toHaveBeenCalled();
|
|
|
325
|
+ });
|
|
|
326
|
+ });
|
|
|
327
|
+
|
|
|
328
|
+ it('setRoot', () => {
|
|
|
329
|
+ uut.setRoot({});
|
|
|
330
|
+ expect(cb).toHaveBeenCalledTimes(1);
|
|
|
331
|
+ expect(cb).toHaveBeenCalledWith('setRoot', { layout: 'parsed' });
|
|
|
332
|
+ });
|
|
|
333
|
+
|
|
|
334
|
+ it('setDefaultOptions', () => {
|
|
|
335
|
+ const options = { x: 1 };
|
|
|
336
|
+ uut.setDefaultOptions(options);
|
|
|
337
|
+ expect(cb).toHaveBeenCalledTimes(1);
|
|
|
338
|
+ expect(cb).toHaveBeenCalledWith('setDefaultOptions', { options });
|
|
|
339
|
+ });
|
|
|
340
|
+
|
|
|
341
|
+ xit('setOptions', () => {
|
|
|
342
|
+ const options = { x: 1 };
|
|
|
343
|
+ uut.setOptions('compId', options);
|
|
|
344
|
+ expect(cb).toHaveBeenCalledTimes(1);
|
|
|
345
|
+ expect(cb).toHaveBeenCalledWith('setOptions', { componentId: 'compId', options: {} });
|
|
|
346
|
+ });
|
|
|
347
|
+ });
|
|
285
|
348
|
});
|