Skip to content
This repository has been archived by the owner on Nov 20, 2019. It is now read-only.

Commit

Permalink
Merge pull request #23 from oliviertassinari/add-unit-tests
Browse files Browse the repository at this point in the history
feat(js): add unit tests
  • Loading branch information
oliviertassinari authored Feb 14, 2017
2 parents d7dfc50 + 9749552 commit db5d5ad
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 34 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/lib
/docs/dist
/flow/interfaces
15 changes: 9 additions & 6 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ module.exports = {
'flowtype',
],
rules: {
'array-bracket-spacing': ['error', 'never'],
'arrow-body-style': 'off',
'arrow-body-style': 'off', // Not our taste?
'arrow-parens': ['error', 'always'], // airbnb use as-needed
'consistent-this': ['error', 'self'],
'max-len': ['error', 110], // airbnb use 100, wishlist, one day
'max-len': ['error', 100, 2, {
ignoreUrls: true,
}], // airbnb is allowing some edge cases
'no-console': 'error', // airbnb is using warn
'no-param-reassign': 'off',
'no-prototype-builtins': 'off',
'no-param-reassign': 'off', // Not our taste?
'no-prototype-builtins': 'off', // airbnb use error
'no-use-before-define': ['error', { 'functions': false }], // airbnb have functions: true, annoying
'object-curly-spacing': 'off', // use babel plugin rule
'operator-linebreak': ['error', 'after'], // aibnb is disabling this rule
'babel/object-curly-spacing': ['error', 'always'],
'no-restricted-properties': 'off', // To remove once react-docgen support ** operator.
'import/no-unresolved': 'off',
'import/no-named-as-default': 'off',
'import/extensions': 'off',
Expand All @@ -39,6 +42,7 @@ module.exports = {
eventHandlerPrefix: 'handle',
eventHandlerPropPrefix: 'on',
}],
'react/require-default-props': 'off', // airbnb use error
'react/forbid-prop-types': 'off', // airbnb use error
'react/jsx-filename-extension': ['error', {extensions: ['.js']}], // airbnb is using .jsx
'react/jsx-max-props-per-line': ['error', {maximum: 3}], // airbnb is disabling this rule
Expand All @@ -59,7 +63,6 @@ module.exports = {
'render'
],
}],
'jsx-a11y/label-has-for': 'warn', // wishlist, one day
'mocha/handle-done-callback': 'error',
'mocha/no-exclusive-tests': 'error',
'mocha/no-global-tests': 'error',
Expand Down
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.*/node_modules/fbjs/*

[libs]
flow/interfaces

[options]
esproposal.class_instance_fields=enable
Expand Down
2 changes: 1 addition & 1 deletion docs/src/Head.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Head = (props) => {
};

Head.propTypes = {
children: PropTypes.node,
children: PropTypes.node.isRequired,
description: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
};
Expand Down
4 changes: 2 additions & 2 deletions docs/src/Main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable flowtype/require-valid-file-annotation */
/* eslint-disable no-console */
/* eslint-disable flowtype/require-valid-file-annotation, no-console */
/* eslint-disable react/no-array-index-key */

import React, { Component } from 'react';
import runtime from 'serviceworker-webpack-plugin/lib/runtime';
Expand Down
11 changes: 5 additions & 6 deletions docs/src/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

const DEBUG = false;

/**
* When the user navigates to your site,
* the browser tries to redownload the script file that defined the service worker in the background.
* If there is even a byte's difference in the service worker file compared to what it currently has,
* it considers it 'new'.
*/
// When the user navigates to your site,
// the browser tries to redownload the script file that defined the service
// worker in the background.
// If there is even a byte's difference in the service worker file compared
// to what it currently has, it considers it 'new'.
const {
assets,
} = global.serviceWorkerOption;
Expand Down
22 changes: 22 additions & 0 deletions flow/interfaces/mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type TestFunction = ((done: () => void) => void | Promise<mixed>);

declare var describe : {
(name:string, spec:() => void): void;
only(description:string, spec:() => void): void;
skip(description:string, spec:() => void): void;
timeout(ms:number): void;
};

declare var context : typeof describe;

declare var it : {
(name:string, spec?:TestFunction): void;
only(description:string, spec:TestFunction): void;
skip(description:string, spec:TestFunction): void;
timeout(ms:number): void;
};

declare function before(method : TestFunction):void;
declare function beforeEach(method : TestFunction):void;
declare function after(method : TestFunction):void;
declare function afterEach(method : TestFunction):void;
38 changes: 21 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
],
"scripts": {
"lint": "eslint . && echo \"eslint: no lint errors\"",
"test": "npm run lint && npm run flow",
"test": "npm run lint && npm run test:unit && npm run flow",
"test:unit": "NODE_ENV=test mocha",
"test:watch": "NODE_ENV=test mocha -w",
"prebuild": "rm -rf lib/",
"flow": "flow",
"build": "babel src --out-dir lib",
Expand Down Expand Up @@ -39,36 +41,38 @@
"webpack": "1 || ^2 || ^2.2.0-rc"
},
"devDependencies": {
"autoprefixer": "^6.6.0",
"babel-cli": "^6.18.0",
"babel-core": "^6.21.0",
"autoprefixer": "^6.7.2",
"babel-cli": "^6.23.0",
"babel-core": "^6.23.1",
"babel-eslint": "^7.1.1",
"babel-loader": "^6.2.10",
"babel-loader": "^6.3.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"babel-preset-react": "^6.23.0",
"babel-preset-stage-1": "^6.16.0",
"chai": "^3.5.0",
"css-loader": "^0.26.1",
"eslint": "^3.12.2",
"eslint-config-airbnb": "^13.0.0",
"eslint": "^3.15.0",
"eslint-config-airbnb": "^14.1.0",
"eslint-plugin-babel": "^4.0.0",
"eslint-plugin-flowtype": "^2.29.2",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^2.2.3",
"eslint-plugin-jsx-a11y": "^3.0.0",
"eslint-plugin-mocha": "^4.8.0",
"eslint-plugin-react": "^6.8.0",
"extract-text-webpack-plugin": "^1.0.1",
"flow-bin": "^0.37.4",
"extract-text-webpack-plugin": "^2.0.0-rc.3",
"flow-bin": "^0.39.0",
"force-case-sensitivity-webpack-plugin": "^0.2.1",
"html-webpack-plugin": "^2.24.1",
"node-sass": "^4.1.1",
"postcss-loader": "^1.2.1",
"html-webpack-plugin": "^2.28.0",
"mocha": "^3.2.0",
"node-sass": "^4.5.0",
"postcss-loader": "^1.3.0",
"react": "^15.4.1",
"react-dom": "^15.4.1",
"react-hot-loader": "^3.0.0-beta.6",
"sass-loader": "^4.1.1",
"sass-loader": "^6.0.0",
"style-loader": "^0.13.1",
"webpack": "^2.2.0",
"webpack-dev-server": "^2.2.0"
"webpack": "^2.2.1",
"webpack-dev-server": "^2.3.0"
}
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default class ServiceWorkerPlugin {
new Error('ServiceWorkerPlugin: ServiceWorker entry is not found in output assets'),
);

return;
return Promise.reject();
}

delete compilation.assets[this.options.filename];
Expand Down Expand Up @@ -165,7 +165,7 @@ export default class ServiceWorkerPlugin {

const templatePromise = this.options.template(serviceWorkerOption);

templatePromise.then((template) => {
return templatePromise.then((template) => {
const serviceWorkerOptionInline = JSON.stringify(serviceWorkerOption, null, minify ? 0 : 2);

const source = `
Expand Down
44 changes: 44 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// @flow weak
/* eslint-env mocha */

import { assert } from 'chai';
import ServiceWorkerPlugin from './index';

function trim(str) {
return str.replace(/^\s+|\s+$/, '');
}

describe('ServiceWorkerPlugin', () => {
describe('options: includes', () => {
it('should allow to have a white list parameter', () => {
const filename = 'sw.js';
const serviceWorkerPlugin = new ServiceWorkerPlugin({
filename,
includes: ['bar.*'],
});

const compilation = {
assets: {
[filename]: {
source: () => '',
},
'bar.js': {},
'foo.js': {},
},
};

return serviceWorkerPlugin
.handleEmit(compilation, {
options: {},
}, () => {})
.then(() => {
assert.strictEqual(compilation.assets[filename].source(), trim(`
var serviceWorkerOption = {
"assets": [
"./bar.js"
]
};`));
});
});
});
});
4 changes: 4 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--require babel-register
--reporter dot
--recursive
src/{,**/}*.spec.js

0 comments on commit db5d5ad

Please sign in to comment.