Skip to content

Commit

Permalink
Add example E2E-style test for the app, showing async/await and routing.
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Apr 18, 2017
1 parent b6e0484 commit 5f48257
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"jsx": true
}
},
"globals": {},
"globals": {
"sleep": 1
},
"rules": {
"react/jsx-no-bind": [2, { "ignoreRefs": true }],
"react/jsx-no-duplicate-props": 2,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"setupFiles": [
"./test/setup.js"
],
"testURL": "http://localhost:8080",
"moduleFileExtensions": [
"js",
"jsx"
Expand Down Expand Up @@ -73,6 +74,7 @@
"postcss-loader": "^1.2.1",
"preact-jsx-chai": "^2.2.1",
"raw-loader": "^0.5.1",
"regenerator-runtime": "^0.10.3",
"replace-bundle-webpack-plugin": "^1.0.0",
"script-ext-html-webpack-plugin": "^1.3.4",
"sinon": "^1.17.5",
Expand Down
50 changes: 50 additions & 0 deletions test/components/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { h, render } from 'preact';
import { route } from 'preact-router';
import { expect } from 'chai';

import App from '../../src/components/app';

describe('App', () => {
let scratch;

beforeAll( () => {
scratch = document.createElement('div');
(document.body || document.documentElement).appendChild(scratch);
});

beforeEach( () => {
scratch.innerHTML = '';
});

afterAll( () => {
scratch.parentNode.removeChild(scratch);
scratch = null;
});


describe('routing', () => {
it('should render the homepage', () => {
render(<App />, scratch);

expect(scratch.innerHTML).to.contain('Home');
});

it('should render /profile', async () => {
render(<App />, scratch);
route('/profile');

await sleep(1);

expect(scratch.innerHTML).to.contain('Profile: me');
});

it('should render /profile/:user', async () => {
render(<App />, scratch);
route('/profile/john');

await sleep(1);

expect(scratch.innerHTML).to.contain('Profile: john');
});
});
});
3 changes: 3 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'regenerator-runtime/runtime';
import chai from 'chai';
import assertJsx, { options } from 'preact-jsx-chai';

Expand All @@ -6,3 +7,5 @@ options.functions = false;

// activate the JSX assertion extension:
chai.use(assertJsx);

global.sleep = ms => new Promise( resolve => setTimeout(resolve, ms) );

0 comments on commit 5f48257

Please sign in to comment.