@modern-js/plugin 1.3.0 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +8 -0
- package/CHANGELOG.md +16 -0
- package/dist/js/modern/manager/async.js +9 -5
- package/dist/js/modern/manager/sync.js +10 -7
- package/dist/js/modern/waterfall/async.js +3 -5
- package/dist/js/modern/waterfall/sync.js +3 -5
- package/dist/js/modern/workflow/async.js +0 -3
- package/dist/js/modern/workflow/parallel.js +1 -2
- package/dist/js/node/manager/async.js +9 -5
- package/dist/js/node/manager/sync.js +10 -7
- package/dist/js/node/waterfall/async.js +3 -5
- package/dist/js/node/waterfall/sync.js +3 -5
- package/dist/js/node/workflow/async.js +0 -3
- package/dist/js/node/workflow/parallel.js +1 -2
- package/dist/js/treeshaking/manager/async.js +9 -5
- package/dist/js/treeshaking/manager/sync.js +10 -7
- package/dist/js/treeshaking/waterfall/async.js +7 -10
- package/dist/js/treeshaking/waterfall/sync.js +7 -10
- package/dist/js/treeshaking/workflow/async.js +0 -2
- package/dist/js/treeshaking/workflow/parallel.js +1 -2
- package/dist/types/manager/async.d.ts +1 -1
- package/dist/types/manager/sync.d.ts +1 -1
- package/jest.config.js +0 -1
- package/package.json +1 -1
- package/tests/.eslintrc.js +0 -6
- package/tests/async.test.ts +0 -712
- package/tests/fixtures/async/base/bar.ts +0 -22
- package/tests/fixtures/async/base/foo.ts +0 -20
- package/tests/fixtures/async/base/fooManager.ts +0 -47
- package/tests/fixtures/async/core/index.ts +0 -174
- package/tests/fixtures/async/dynamic/bar.ts +0 -18
- package/tests/fixtures/async/dynamic/foo.ts +0 -27
- package/tests/fixtures/sync/base/bar.ts +0 -21
- package/tests/fixtures/sync/base/foo.ts +0 -20
- package/tests/fixtures/sync/base/fooManager.ts +0 -47
- package/tests/fixtures/sync/core/index.ts +0 -171
- package/tests/fixtures/sync/dynamic/bar.ts +0 -22
- package/tests/fixtures/sync/dynamic/foo.ts +0 -27
- package/tests/helpers.ts +0 -4
- package/tests/pipeline.test.ts +0 -607
- package/tests/sync.test.ts +0 -677
- package/tests/tsconfig.json +0 -13
- package/tests/waterfall.test.ts +0 -172
- package/tests/workflow.test.ts +0 -111
package/tests/sync.test.ts
DELETED
@@ -1,677 +0,0 @@
|
|
1
|
-
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
2
|
-
/* eslint-disable max-lines */
|
3
|
-
import {
|
4
|
-
createPipeline,
|
5
|
-
createAsyncPipeline,
|
6
|
-
createContext,
|
7
|
-
createContainer,
|
8
|
-
} from 'farrow-pipeline';
|
9
|
-
import type { PluginOptions, Setup } from '../src';
|
10
|
-
import { createManager, createAsyncManager, useRunner } from '../src/manager';
|
11
|
-
import { createWaterfall, createAsyncWaterfall } from '../src/waterfall';
|
12
|
-
import {
|
13
|
-
createWorkflow,
|
14
|
-
createAsyncWorkflow,
|
15
|
-
createParallelWorkflow,
|
16
|
-
} from '../src/workflow';
|
17
|
-
import { main, TestHooks, TestPlugin } from './fixtures/sync/core';
|
18
|
-
import foo from './fixtures/sync/base/foo';
|
19
|
-
import bar, { getBar } from './fixtures/sync/base/bar';
|
20
|
-
import dFoo from './fixtures/sync/dynamic/foo';
|
21
|
-
import dBar, { getNumber, setNumber } from './fixtures/sync/dynamic/bar';
|
22
|
-
|
23
|
-
describe('sync manager', () => {
|
24
|
-
it('base useage', () => {
|
25
|
-
const countContext = createContext(0);
|
26
|
-
const useCount = () => countContext.use().value;
|
27
|
-
const manager = createManager();
|
28
|
-
|
29
|
-
manager.usePlugin(
|
30
|
-
manager.createPlugin(() => {
|
31
|
-
expect(useCount()).toBe(1);
|
32
|
-
}),
|
33
|
-
);
|
34
|
-
|
35
|
-
manager.run(() => {
|
36
|
-
countContext.set(1);
|
37
|
-
});
|
38
|
-
|
39
|
-
expect(manager.run(() => countContext.get())).toBe(1);
|
40
|
-
});
|
41
|
-
|
42
|
-
it('with sub waterfall', () => {
|
43
|
-
type Context = {
|
44
|
-
count: number;
|
45
|
-
};
|
46
|
-
|
47
|
-
type Config = {
|
48
|
-
mode: string;
|
49
|
-
};
|
50
|
-
|
51
|
-
const prepare = createWaterfall<{
|
52
|
-
config: Config;
|
53
|
-
}>();
|
54
|
-
|
55
|
-
const preDev = createWaterfall<{
|
56
|
-
context: Context;
|
57
|
-
}>();
|
58
|
-
|
59
|
-
const postDev = createWaterfall<{
|
60
|
-
context: Context;
|
61
|
-
}>();
|
62
|
-
|
63
|
-
const manager = createManager({ prepare, preDev, postDev });
|
64
|
-
|
65
|
-
const runner = manager.init();
|
66
|
-
|
67
|
-
const prepareResult = runner.prepare({ config: { mode: 'test' } });
|
68
|
-
const preDevResult = runner.preDev({ context: { count: 0 } });
|
69
|
-
const postDevResult = runner.postDev({ context: { count: 1 } });
|
70
|
-
|
71
|
-
expect(prepareResult).toStrictEqual({ config: { mode: 'test' } });
|
72
|
-
expect(preDevResult).toStrictEqual({ context: { count: 0 } });
|
73
|
-
expect(postDevResult).toStrictEqual({ context: { count: 1 } });
|
74
|
-
});
|
75
|
-
|
76
|
-
it('could without progress hook in plugin', () => {
|
77
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
78
|
-
const foo = createWaterfall<number>();
|
79
|
-
const manager = createManager({ foo });
|
80
|
-
|
81
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
82
|
-
const plugin = manager.createPlugin(() => {});
|
83
|
-
manager.usePlugin(plugin);
|
84
|
-
|
85
|
-
const runner = manager.init();
|
86
|
-
|
87
|
-
expect(runner.foo(0)).toBe(0);
|
88
|
-
});
|
89
|
-
|
90
|
-
describe('pre order plugin', () => {
|
91
|
-
it('default order is right order', () => {
|
92
|
-
const manager = createManager();
|
93
|
-
|
94
|
-
const list: number[] = [];
|
95
|
-
const plugin0 = manager.createPlugin(
|
96
|
-
() => {
|
97
|
-
list.push(0);
|
98
|
-
},
|
99
|
-
{ name: 'plugin0' },
|
100
|
-
);
|
101
|
-
const plugin1 = manager.createPlugin(
|
102
|
-
() => {
|
103
|
-
list.push(1);
|
104
|
-
},
|
105
|
-
{
|
106
|
-
name: 'plugin1',
|
107
|
-
pre: ['plugin0'],
|
108
|
-
},
|
109
|
-
);
|
110
|
-
const plugin2 = manager.createPlugin(
|
111
|
-
() => {
|
112
|
-
list.push(2);
|
113
|
-
},
|
114
|
-
{ name: 'plugin2' },
|
115
|
-
);
|
116
|
-
|
117
|
-
manager.usePlugin(plugin0, plugin1, plugin2);
|
118
|
-
|
119
|
-
manager.init();
|
120
|
-
|
121
|
-
expect(list).toStrictEqual([0, 1, 2]);
|
122
|
-
});
|
123
|
-
|
124
|
-
it('default order is incorrect order', () => {
|
125
|
-
const manager = createManager();
|
126
|
-
|
127
|
-
const list: number[] = [];
|
128
|
-
const plugin0 = manager.createPlugin(
|
129
|
-
() => {
|
130
|
-
list.push(0);
|
131
|
-
},
|
132
|
-
{
|
133
|
-
name: 'plugin0',
|
134
|
-
pre: ['plugin1'],
|
135
|
-
},
|
136
|
-
);
|
137
|
-
const plugin1 = manager.createPlugin(
|
138
|
-
() => {
|
139
|
-
list.push(1);
|
140
|
-
},
|
141
|
-
{ name: 'plugin1' },
|
142
|
-
);
|
143
|
-
const plugin2 = manager.createPlugin(
|
144
|
-
() => {
|
145
|
-
list.push(2);
|
146
|
-
},
|
147
|
-
{ name: 'plugin2' },
|
148
|
-
);
|
149
|
-
|
150
|
-
manager.usePlugin(plugin0, plugin1, plugin2);
|
151
|
-
|
152
|
-
manager.init();
|
153
|
-
|
154
|
-
expect(list).toStrictEqual([1, 0, 2]);
|
155
|
-
});
|
156
|
-
});
|
157
|
-
|
158
|
-
describe('post order plugin', () => {
|
159
|
-
it('default order is right order', () => {
|
160
|
-
const manager = createManager();
|
161
|
-
|
162
|
-
const list: number[] = [];
|
163
|
-
const plugin0 = manager.createPlugin(
|
164
|
-
() => {
|
165
|
-
list.push(0);
|
166
|
-
},
|
167
|
-
{
|
168
|
-
name: 'plugin0',
|
169
|
-
post: ['plugin1'],
|
170
|
-
},
|
171
|
-
);
|
172
|
-
const plugin1 = manager.createPlugin(
|
173
|
-
() => {
|
174
|
-
list.push(1);
|
175
|
-
},
|
176
|
-
{ name: 'plugin1' },
|
177
|
-
);
|
178
|
-
const plugin2 = manager.createPlugin(
|
179
|
-
() => {
|
180
|
-
list.push(2);
|
181
|
-
},
|
182
|
-
{ name: 'plugin2' },
|
183
|
-
);
|
184
|
-
|
185
|
-
manager.usePlugin(plugin0, plugin1, plugin2);
|
186
|
-
|
187
|
-
manager.init();
|
188
|
-
|
189
|
-
expect(list).toStrictEqual([0, 1, 2]);
|
190
|
-
});
|
191
|
-
|
192
|
-
it('default order is incorrect order', () => {
|
193
|
-
const manager = createManager();
|
194
|
-
|
195
|
-
const list: number[] = [];
|
196
|
-
const plugin0 = manager.createPlugin(
|
197
|
-
() => {
|
198
|
-
list.push(0);
|
199
|
-
},
|
200
|
-
{ name: 'plugin0' },
|
201
|
-
);
|
202
|
-
const plugin1 = manager.createPlugin(
|
203
|
-
() => {
|
204
|
-
list.push(1);
|
205
|
-
},
|
206
|
-
{
|
207
|
-
name: 'plugin1',
|
208
|
-
post: ['plugin0'],
|
209
|
-
},
|
210
|
-
);
|
211
|
-
const plugin2 = manager.createPlugin(
|
212
|
-
() => {
|
213
|
-
list.push(2);
|
214
|
-
},
|
215
|
-
{ name: 'plugin2' },
|
216
|
-
);
|
217
|
-
|
218
|
-
manager.usePlugin(plugin0, plugin1, plugin2);
|
219
|
-
|
220
|
-
manager.init();
|
221
|
-
|
222
|
-
expect(list).toStrictEqual([1, 0, 2]);
|
223
|
-
});
|
224
|
-
|
225
|
-
it('should support more plugin', () => {
|
226
|
-
const manager = createManager();
|
227
|
-
|
228
|
-
let status = 0;
|
229
|
-
const plugin1 = manager.createPlugin(
|
230
|
-
() => {
|
231
|
-
status = 1;
|
232
|
-
},
|
233
|
-
{ name: 'plugin1' },
|
234
|
-
);
|
235
|
-
|
236
|
-
const plugin2 = manager.createPlugin(() => {
|
237
|
-
status = 2;
|
238
|
-
});
|
239
|
-
|
240
|
-
const plugin3 = manager.createPlugin(() => {
|
241
|
-
status = 3;
|
242
|
-
});
|
243
|
-
|
244
|
-
const plugin4 = manager.createPlugin(
|
245
|
-
() => {
|
246
|
-
status = 4;
|
247
|
-
},
|
248
|
-
{ post: ['plugin1'] },
|
249
|
-
);
|
250
|
-
|
251
|
-
manager.usePlugin(plugin1);
|
252
|
-
manager.usePlugin(plugin2);
|
253
|
-
manager.usePlugin(plugin3);
|
254
|
-
manager.usePlugin(plugin4);
|
255
|
-
|
256
|
-
manager.init({});
|
257
|
-
|
258
|
-
expect(status).toBe(1);
|
259
|
-
});
|
260
|
-
});
|
261
|
-
|
262
|
-
describe('rivals plugin', () => {
|
263
|
-
it('should throw error when attaching rival plugin', () => {
|
264
|
-
const manager = createManager();
|
265
|
-
|
266
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
267
|
-
const plugin1 = manager.createPlugin(() => {}, { name: 'plugin1' });
|
268
|
-
|
269
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
270
|
-
const plugin2 = manager.createPlugin(() => {}, { rivals: ['plugin1'] });
|
271
|
-
|
272
|
-
manager.usePlugin(plugin1);
|
273
|
-
manager.usePlugin(plugin2);
|
274
|
-
|
275
|
-
expect(() => manager.init({})).toThrowError();
|
276
|
-
});
|
277
|
-
|
278
|
-
it('should not throw error without attaching rival plugin', () => {
|
279
|
-
const manager = createManager();
|
280
|
-
|
281
|
-
let count = 0;
|
282
|
-
const plugin0 = manager.createPlugin(
|
283
|
-
() => {
|
284
|
-
count = 0;
|
285
|
-
},
|
286
|
-
{ name: 'plugin0' },
|
287
|
-
);
|
288
|
-
const plugin1 = manager.createPlugin(
|
289
|
-
() => {
|
290
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
291
|
-
count += 1;
|
292
|
-
},
|
293
|
-
{
|
294
|
-
name: 'plugin1',
|
295
|
-
rivals: ['plugin2'],
|
296
|
-
},
|
297
|
-
);
|
298
|
-
|
299
|
-
manager.usePlugin(plugin0, plugin1);
|
300
|
-
|
301
|
-
manager.init();
|
302
|
-
});
|
303
|
-
});
|
304
|
-
|
305
|
-
describe('required plugin', () => {
|
306
|
-
it('should throw error when it is without required plugin', () => {
|
307
|
-
const manager = createManager();
|
308
|
-
|
309
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
310
|
-
const plugin0 = manager.createPlugin(() => {}, {
|
311
|
-
name: 'plugin0',
|
312
|
-
required: ['plugin1'],
|
313
|
-
});
|
314
|
-
|
315
|
-
manager.usePlugin(plugin0);
|
316
|
-
|
317
|
-
expect(manager.init).toThrowError();
|
318
|
-
});
|
319
|
-
|
320
|
-
it('should not throw error without attaching rival plugin', () => {
|
321
|
-
const manager = createManager();
|
322
|
-
|
323
|
-
let count = 0;
|
324
|
-
const plugin0 = manager.createPlugin(
|
325
|
-
() => {
|
326
|
-
count = 0;
|
327
|
-
},
|
328
|
-
{
|
329
|
-
name: 'plugin0',
|
330
|
-
required: ['plugin1'],
|
331
|
-
},
|
332
|
-
);
|
333
|
-
const plugin1 = manager.createPlugin(
|
334
|
-
() => {
|
335
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
336
|
-
count += 1;
|
337
|
-
},
|
338
|
-
{ name: 'plugin1' },
|
339
|
-
);
|
340
|
-
|
341
|
-
manager.usePlugin(plugin0, plugin1);
|
342
|
-
|
343
|
-
manager.init();
|
344
|
-
});
|
345
|
-
});
|
346
|
-
|
347
|
-
it('common attach plugin', () => {
|
348
|
-
const manager = main.clone().usePlugin(foo).usePlugin(bar);
|
349
|
-
|
350
|
-
expect(getBar()).toBe(0);
|
351
|
-
|
352
|
-
const runner = manager.init();
|
353
|
-
|
354
|
-
expect(getBar()).toBe(1);
|
355
|
-
|
356
|
-
runner.preDev();
|
357
|
-
|
358
|
-
expect(getBar()).toBe(2);
|
359
|
-
|
360
|
-
runner.postDev();
|
361
|
-
|
362
|
-
expect(getBar()).toBe(3);
|
363
|
-
});
|
364
|
-
|
365
|
-
it('should support dynamic register', () => {
|
366
|
-
const manager = main.clone().usePlugin(dFoo).usePlugin(dBar);
|
367
|
-
|
368
|
-
expect(getNumber()).toBe(0);
|
369
|
-
|
370
|
-
const runner = manager.init();
|
371
|
-
|
372
|
-
expect(getNumber()).toBe(1);
|
373
|
-
|
374
|
-
runner.preDev();
|
375
|
-
|
376
|
-
expect(getNumber()).toBe(2);
|
377
|
-
});
|
378
|
-
|
379
|
-
it('should de-duplicate plugins', () => {
|
380
|
-
const manager = createManager();
|
381
|
-
|
382
|
-
let count = 0;
|
383
|
-
const plugin = manager.createPlugin(() => {
|
384
|
-
count += 1;
|
385
|
-
});
|
386
|
-
|
387
|
-
manager.usePlugin(plugin, plugin);
|
388
|
-
manager.usePlugin(plugin);
|
389
|
-
|
390
|
-
manager.init();
|
391
|
-
|
392
|
-
expect(count).toBe(1);
|
393
|
-
});
|
394
|
-
|
395
|
-
it('should support manager clone and override pluginAPI', done => {
|
396
|
-
const myAPI = { hello: () => 1 };
|
397
|
-
const manager = createManager({}, myAPI);
|
398
|
-
const plugin = {
|
399
|
-
setup(api: typeof myAPI) {
|
400
|
-
expect(api.hello()).toEqual(2);
|
401
|
-
done();
|
402
|
-
},
|
403
|
-
};
|
404
|
-
const clonedManager = manager.clone({
|
405
|
-
hello: () => 2,
|
406
|
-
});
|
407
|
-
|
408
|
-
clonedManager.usePlugin(() => plugin).init();
|
409
|
-
});
|
410
|
-
|
411
|
-
it('isPlugin: exclusive plugins of manager', () => {
|
412
|
-
const manager0 = createAsyncManager();
|
413
|
-
const manager1 = createManager();
|
414
|
-
|
415
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
416
|
-
const plugin = manager0.createPlugin(() => {});
|
417
|
-
|
418
|
-
expect(manager0.isPlugin(plugin)).toBeTruthy();
|
419
|
-
expect(manager1.isPlugin(plugin)).toBeFalsy();
|
420
|
-
expect(manager1.isPlugin({})).toBeFalsy();
|
421
|
-
expect(manager1.isPlugin('' as any)).toBeFalsy();
|
422
|
-
});
|
423
|
-
|
424
|
-
it('usePlugin: exclusive plugins of manager', () => {
|
425
|
-
const manager0 = createAsyncManager();
|
426
|
-
const manager1 = createManager();
|
427
|
-
|
428
|
-
let count = 0;
|
429
|
-
const plugin = manager0.createPlugin(() => {
|
430
|
-
count += 1;
|
431
|
-
});
|
432
|
-
|
433
|
-
manager1.usePlugin(plugin as any);
|
434
|
-
manager1.init();
|
435
|
-
|
436
|
-
expect(count).toBe(0);
|
437
|
-
});
|
438
|
-
|
439
|
-
it('should support clear plugins', () => {
|
440
|
-
setNumber(0);
|
441
|
-
|
442
|
-
const manager = main.clone().usePlugin(dFoo).usePlugin(dBar);
|
443
|
-
|
444
|
-
manager.clear();
|
445
|
-
|
446
|
-
expect(getNumber()).toBe(0);
|
447
|
-
|
448
|
-
const runner = manager.init();
|
449
|
-
|
450
|
-
expect(getNumber()).toBe(0);
|
451
|
-
|
452
|
-
runner.preDev();
|
453
|
-
|
454
|
-
expect(getNumber()).toBe(0);
|
455
|
-
});
|
456
|
-
|
457
|
-
it('should run under the internal container', () => {
|
458
|
-
const manager = createManager();
|
459
|
-
const Count = createContext(0);
|
460
|
-
|
461
|
-
const plugin = manager.createPlugin(() => {
|
462
|
-
Count.set(1);
|
463
|
-
});
|
464
|
-
|
465
|
-
manager.usePlugin(plugin);
|
466
|
-
|
467
|
-
manager.init();
|
468
|
-
expect(manager.run(Count.get)).toBe(1);
|
469
|
-
|
470
|
-
const container = createContainer();
|
471
|
-
|
472
|
-
manager.init({ container });
|
473
|
-
expect(manager.run(Count.get, { container })).toBe(1);
|
474
|
-
});
|
475
|
-
|
476
|
-
it('should support all progress', async () => {
|
477
|
-
const manager = createManager({
|
478
|
-
pipeline: createPipeline(),
|
479
|
-
asyncPipeline: createAsyncPipeline(),
|
480
|
-
waterfall: createWaterfall(),
|
481
|
-
asyncWaterfall: createAsyncWaterfall(),
|
482
|
-
workflow: createWorkflow(),
|
483
|
-
asyncWorkflow: createAsyncWorkflow(),
|
484
|
-
parallelWorkflow: createParallelWorkflow(),
|
485
|
-
});
|
486
|
-
|
487
|
-
const list: string[] = [];
|
488
|
-
const plugin = manager.createPlugin(() => ({
|
489
|
-
pipeline: () => {
|
490
|
-
list.push('pipeline');
|
491
|
-
},
|
492
|
-
asyncPipeline: () => {
|
493
|
-
list.push('asyncPipeline');
|
494
|
-
},
|
495
|
-
waterfall: () => {
|
496
|
-
list.push('waterfall');
|
497
|
-
},
|
498
|
-
asyncWaterfall: () => {
|
499
|
-
list.push('asyncWaterfall');
|
500
|
-
},
|
501
|
-
workflow: () => {
|
502
|
-
list.push('workflow');
|
503
|
-
},
|
504
|
-
asyncWorkflow: () => {
|
505
|
-
list.push('asyncWorkflow');
|
506
|
-
},
|
507
|
-
parallelWorkflow: () => {
|
508
|
-
list.push('parallelWorkflow');
|
509
|
-
},
|
510
|
-
}));
|
511
|
-
manager.usePlugin(plugin);
|
512
|
-
|
513
|
-
const runner = manager.init();
|
514
|
-
|
515
|
-
runner.pipeline({});
|
516
|
-
await runner.asyncPipeline({});
|
517
|
-
runner.waterfall();
|
518
|
-
await runner.asyncWaterfall();
|
519
|
-
runner.workflow();
|
520
|
-
await runner.asyncWorkflow();
|
521
|
-
await runner.parallelWorkflow();
|
522
|
-
|
523
|
-
expect(list).toStrictEqual([
|
524
|
-
'pipeline',
|
525
|
-
'asyncPipeline',
|
526
|
-
'waterfall',
|
527
|
-
'asyncWaterfall',
|
528
|
-
'workflow',
|
529
|
-
'asyncWorkflow',
|
530
|
-
'parallelWorkflow',
|
531
|
-
]);
|
532
|
-
});
|
533
|
-
|
534
|
-
it('should throw error when progress is illegal', () => {
|
535
|
-
const manager = createManager({ test: '' as any });
|
536
|
-
|
537
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
538
|
-
const plugin = manager.createPlugin(() => {});
|
539
|
-
|
540
|
-
manager.usePlugin(plugin);
|
541
|
-
|
542
|
-
expect(manager.init).toThrowError();
|
543
|
-
});
|
544
|
-
|
545
|
-
describe('useRunner', () => {
|
546
|
-
it('base usage', () => {
|
547
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
548
|
-
const foo = createPipeline();
|
549
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
550
|
-
const bar = createPipeline();
|
551
|
-
const manager = createManager({ foo, bar });
|
552
|
-
|
553
|
-
let count = 0;
|
554
|
-
|
555
|
-
const plugin = manager.createPlugin(() => ({
|
556
|
-
foo: () => {
|
557
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
558
|
-
const runner = useRunner();
|
559
|
-
runner.bar();
|
560
|
-
},
|
561
|
-
bar: () => {
|
562
|
-
count = 1;
|
563
|
-
},
|
564
|
-
}));
|
565
|
-
manager.usePlugin(plugin);
|
566
|
-
|
567
|
-
const runner = manager.init();
|
568
|
-
runner.foo({});
|
569
|
-
|
570
|
-
expect(count).toBe(1);
|
571
|
-
});
|
572
|
-
|
573
|
-
it('can not use useRunner out plugin hook', () => {
|
574
|
-
expect(useRunner).toThrowError(
|
575
|
-
new Error(
|
576
|
-
`Can't call useContainer out of scope, it should be placed on top of the function`,
|
577
|
-
),
|
578
|
-
);
|
579
|
-
});
|
580
|
-
});
|
581
|
-
|
582
|
-
describe('setup api', () => {
|
583
|
-
it('should allow to access api.useHookRunners by default', done => {
|
584
|
-
const manager = createManager<TestHooks>();
|
585
|
-
const plugin: TestPlugin = {
|
586
|
-
name: 'plugin',
|
587
|
-
setup: api => {
|
588
|
-
expect(api.useHookRunners).toBeTruthy();
|
589
|
-
done();
|
590
|
-
},
|
591
|
-
};
|
592
|
-
manager.usePlugin(() => plugin);
|
593
|
-
manager.init();
|
594
|
-
});
|
595
|
-
|
596
|
-
it('should allow to register extra api', done => {
|
597
|
-
type API = { foo: () => void };
|
598
|
-
|
599
|
-
const manager = createAsyncManager<TestHooks, API>(
|
600
|
-
{},
|
601
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
602
|
-
{ foo: () => {} },
|
603
|
-
);
|
604
|
-
|
605
|
-
const plugin: PluginOptions<TestHooks, Setup<TestHooks, API>> = {
|
606
|
-
name: 'plugin',
|
607
|
-
setup: api => {
|
608
|
-
expect(api.foo).toBeTruthy();
|
609
|
-
done();
|
610
|
-
},
|
611
|
-
};
|
612
|
-
|
613
|
-
manager.usePlugin(() => plugin);
|
614
|
-
manager.init();
|
615
|
-
});
|
616
|
-
});
|
617
|
-
|
618
|
-
describe('usePlugins option', () => {
|
619
|
-
it('should allow to use single plugin', async () => {
|
620
|
-
const manager = createManager<TestHooks>();
|
621
|
-
|
622
|
-
const list: number[] = [];
|
623
|
-
const plugin0: TestPlugin = {
|
624
|
-
name: 'plugin0',
|
625
|
-
setup: () => {
|
626
|
-
list.push(0);
|
627
|
-
},
|
628
|
-
};
|
629
|
-
|
630
|
-
const plugin1: TestPlugin = {
|
631
|
-
name: 'plugin1',
|
632
|
-
usePlugins: [plugin0],
|
633
|
-
setup: () => {
|
634
|
-
list.push(1);
|
635
|
-
},
|
636
|
-
};
|
637
|
-
|
638
|
-
manager.usePlugin(() => plugin1);
|
639
|
-
manager.init();
|
640
|
-
|
641
|
-
expect(list).toStrictEqual([0, 1]);
|
642
|
-
});
|
643
|
-
|
644
|
-
it('should allow to use multiple plugins', async () => {
|
645
|
-
const manager = createManager<TestHooks>();
|
646
|
-
|
647
|
-
const list: number[] = [];
|
648
|
-
const plugin0: TestPlugin = {
|
649
|
-
name: 'plugin0',
|
650
|
-
setup: () => {
|
651
|
-
list.push(0);
|
652
|
-
},
|
653
|
-
};
|
654
|
-
|
655
|
-
const plugin1: TestPlugin = {
|
656
|
-
name: 'plugin1',
|
657
|
-
usePlugins: [plugin0],
|
658
|
-
setup: () => {
|
659
|
-
list.push(1);
|
660
|
-
},
|
661
|
-
};
|
662
|
-
|
663
|
-
const plugin2: TestPlugin = {
|
664
|
-
name: 'plugin2',
|
665
|
-
usePlugins: [plugin1],
|
666
|
-
setup: () => {
|
667
|
-
list.push(2);
|
668
|
-
},
|
669
|
-
};
|
670
|
-
|
671
|
-
manager.usePlugin(() => plugin2);
|
672
|
-
manager.init();
|
673
|
-
|
674
|
-
expect(list).toStrictEqual([0, 1, 2]);
|
675
|
-
});
|
676
|
-
});
|
677
|
-
});
|
package/tests/tsconfig.json
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"extends": "@modern-js/tsconfig/base",
|
3
|
-
"compilerOptions": {
|
4
|
-
"declaration": false,
|
5
|
-
"jsx": "preserve",
|
6
|
-
"baseUrl": "./",
|
7
|
-
"isolatedModules": true,
|
8
|
-
"target": "ES5",
|
9
|
-
"esModuleInterop": true,
|
10
|
-
"paths": {},
|
11
|
-
"types": ["node", "jest"]
|
12
|
-
}
|
13
|
-
}
|