-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
54 lines (49 loc) · 2.07 KB
/
index.js
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
const mockInitializers = [];
module.exports = function preloadAll() {
if (mockInitializers.length) {
// Copy and empty out `mockInitializers` right away so that any newly
// enqueued components are found in the next pass.
const initializers = mockInitializers.slice();
mockInitializers.length = 0;
// While loading the components in this round of initializers, more
// components may have been dynamically imported, adding more initializers
// we should run and await.
return Promise.all(initializers.map(preload => preload())).then(preloadAll);
} else {
return Promise.resolve();
}
};
function createFactory(moduleName) {
return () => {
const dynamicModule = jest.requireActual(moduleName);
const dynamic = dynamicModule.default;
const mockDynamic = (loader, options) => {
// Remove webpack-specific functionality added by the next/babel preset.
if (loader && typeof loader === "object" && loader.loadableGenerated) {
const { loadableGenerated, ...mockLoader } = loader;
loader = mockLoader;
}
if (options && typeof options === "object" && options.loadableGenerated) {
const { loadableGenerated, ...mockOptions } = options;
options = mockOptions;
}
const LoadableComponent = dynamic(loader, options);
mockInitializers.push(() =>
LoadableComponent.preload
? LoadableComponent.preload()
: LoadableComponent.render.preload());
return LoadableComponent;
};
// Replace the `default` export on the existing module so that other exports
// and non-enumerable properties remain.
dynamicModule.default = mockDynamic;
return dynamicModule;
};
}
// In order to more easily include this feature in shared Jest setups (like
// presets), use `virtual: true` to avoid throwing an error when `next` isn't
// actually installed or it's a version without `next-server` (introduced in
// Next.js 8).
for (const moduleName of ["next/dynamic", "next-server/dynamic"]) {
jest.doMock(moduleName, createFactory(moduleName), { virtual: true });
}