Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
exogen committed Dec 10, 2018
0 parents commit 365c1de
Show file tree
Hide file tree
Showing 12 changed files with 6,933 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: ["babel-plugin-dynamic-import-node"]
};
64 changes: 64 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# Build cache
.cache

# next.js build output
.next
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# jest-next-dynamic

Unlike [react-loadable](https://github.com/jamiebuilds/react-loadable), the
[dynamic import](https://github.com/zeit/next.js#dynamic-import) support
offered by Next.js does not expose a way to preload the dynamically imported
components in Jest’s environment (which mimics a DOM environment).

This module can be imported in your tests or setup file to make sure any
component created with `next/dynamic` is added to a queue, which can then be
flushed with the exported `preloadAll` function.

## Usage

```js
// This will mock `next/dynamic`, so make sure to import it before any of your
// components.
import preloadAll from "jest-next-dynamic";

beforeAll(async () => {
await preloadAll();
});
```
18 changes: 18 additions & 0 deletions __snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renders nested next/dynamic components 1`] = `
<div>
<p>
Nested dynamic loaded component:
</p>
<div>
<p>
Dynamic loaded component:
</p>
<h1>
Leaf component says:
Loaded with next/dynamic!
</h1>
</div>
</div>
`;
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const mockPromises = [];

module.exports = function preloadAll() {
if (mockPromises.length) {
const promises = mockPromises.slice();
mockPromises.length = 0;
// While loading the components in `mockPromises`, more components may have
// been dynamically loaded, adding more promises we should wait for.
return Promise.all(promises).then(preloadAll);
} else {
return Promise.resolve();
}
};

jest.doMock("next/dynamic", () => {
const { default: dynamic } = jest.requireActual("next/dynamic");

const mockDynamic = (...args) => {
const LoadableComponent = dynamic(...args);
mockPromises.push(LoadableComponent.preload());
return LoadableComponent;
};

return mockDynamic;
});
12 changes: 12 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from "react";
import TestRenderer from "react-test-renderer";
import preloadAll from "./index";
import NextDynamicNested from "./test/NextDynamicNested";

beforeAll(async () => {
await preloadAll();
});

it("renders nested next/dynamic components", () => {
expect(TestRenderer.create(<NextDynamicNested />).toJSON()).toMatchSnapshot();
});
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "jest-next-dynamic",
"version": "0.1.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "jest"
},
"devDependencies": {
"@babel/core": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"@babel/preset-react": "^7.0.0",
"babel-core": "^7.0.0-bridge",
"babel-jest": "^23.6.0",
"babel-plugin-dynamic-import-node": "^2.2.0",
"jest": "^23.6.0",
"next": "^7.0.2",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react-test-renderer": "^16.6.3"
}
}
31 changes: 31 additions & 0 deletions test/NextDynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from "react";
import dynamic from "next/dynamic";

const LeafComponent = dynamic(
() =>
// To prevent cheating by working without waiting for `preloadAll` due to the
// way Jest schedules tests on the event loop, delay the promise by more than
// what this `import()` transform would do.
new Promise((resolve, reject) => {
setTimeout(() => {
import("./NextDynamicLeaf")
.then(component => {
return new Promise(resolve => {
setTimeout(() => {
resolve(component);
}, 500);
});
})
.then(resolve, reject);
}, 500);
})
);

export default function NextDynamic() {
return (
<div>
<p>Dynamic loaded component:</p>
<LeafComponent>Loaded with next/dynamic!</LeafComponent>
</div>
);
}
5 changes: 5 additions & 0 deletions test/NextDynamicLeaf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from "react";

export default function LeafComponent(props) {
return <h1>Leaf component says: {props.children}</h1>;
}
31 changes: 31 additions & 0 deletions test/NextDynamicNested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from "react";
import dynamic from "next/dynamic";

const NextDynamic = dynamic(
() =>
// To prevent cheating by working without waiting for `preloadAll` due to the
// way Jest schedules tests on the event loop, delay the promise by more than
// what this `import()` transform would do.
new Promise((resolve, reject) => {
setTimeout(() => {
import("./NextDynamic")
.then(component => {
return new Promise(resolve => {
setTimeout(() => {
resolve(component);
}, 500);
});
})
.then(resolve, reject);
}, 500);
})
);

export default function NextDynamicNested() {
return (
<div>
<p>Nested dynamic loaded component:</p>
<NextDynamic />
</div>
);
}
Empty file added test/util.js
Empty file.
Loading

0 comments on commit 365c1de

Please sign in to comment.