Skip to content

Commit

Permalink
Move to Jest Expect (#6)
Browse files Browse the repository at this point in the history
* Initial jest conversion

* Remove macho setup and remove jsdom package.

* PromiseMiddleware test refactor.

* Refactor ReduxTaxi tests to jest expect

* Refactor ReduxTaxiMiddleware test to jest expect

* Refactor ReduxTaxiProvider to jest expect

* Refactor RegisterAsyncActions test to jest expect

* Remove chai package

* Remove trailing commas

* Add Prettier

* Add Prettier

* Update list-staged file catch

* Run Prettier on all JS files

* Remove sinon in favor for jest mocks.

* removing prettier deps from this PR.

* remove trailing comma
  • Loading branch information
frytyler authored and tizmagik committed Oct 6, 2017
1 parent ef26915 commit 12b86d7
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 294 deletions.
96 changes: 47 additions & 49 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
{
"name": "redux-taxi",
"version": "1.2.0",
"description": "Inform server-side rendering from the component level when dealing with asynchronous actions.",
"main": "lib/index",
"scripts": {
"compile": "rm -rf ./lib && babel -d lib/ src/",
"prepublish": "npm run test && npm run compile",
"test": "jest"
},
"repository": "NYTimes/redux-taxi",
"keywords": [
"redux",
"react",
"taxi",
"server rendering",
"ssr",
"isomorphic",
"universal",
"nytimes"
],
"author": "Jeremy Gayed (https://github.com/tizmagik)",
"contributors": [
"Ivan Kravchenko (https://github.com/ivankravchenko)",
"Devorah Moskowitz (https://github.com/devorah)",
"Oleh Ziniak (https://github.com/oziniak)"
],
"license": "Apache 2.0",
"bugs": {
"url": "https://github.com/NYTimes/redux-taxi/issues"
},
"homepage": "https://github.com/NYTimes/redux-taxi#readme",
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-core": "^6.7.4",
"babel-jest": "^21.2.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.6.0",
"babel-polyfill": "^6.7.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "^6.7.2",
"chai": "^3.5.0",
"jest": "^21.2.1",
"prop-types": "^15.5.8",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"sinon": "^1.17.3"
}
"name": "redux-taxi",
"version": "1.2.0",
"description": "Inform server-side rendering from the component level when dealing with asynchronous actions.",
"main": "lib/index",
"scripts": {
"compile": "rm -rf ./lib && babel -d lib/ src/",
"prepublish": "npm run test && npm run compile",
"test": "jest"
},
"repository": "NYTimes/redux-taxi",
"keywords": [
"redux",
"react",
"taxi",
"server rendering",
"ssr",
"isomorphic",
"universal",
"nytimes"
],
"author": "Jeremy Gayed (https://github.com/tizmagik)",
"contributors": [
"Ivan Kravchenko (https://github.com/ivankravchenko)",
"Devorah Moskowitz (https://github.com/devorah)",
"Oleh Ziniak (https://github.com/oziniak)"
],
"license": "Apache 2.0",
"bugs": {
"url": "https://github.com/NYTimes/redux-taxi/issues"
},
"homepage": "https://github.com/NYTimes/redux-taxi#readme",
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-core": "^6.7.4",
"babel-jest": "^21.2.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.6.0",
"babel-polyfill": "^6.7.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "^6.7.2",
"jest": "^21.2.1",
"prop-types": "^15.5.8",
"react": "^15.5.4",
"react-dom": "^15.5.4"
}
}
17 changes: 10 additions & 7 deletions src/PromiseMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,43 @@ export const START = 'START';
export const DONE = 'DONE';

function generateRandomId() {
return Math.random().toString(36).slice(-5);
return Math.random()
.toString(36)
.slice(-5);
}

function sequence(id, type) {
return {
sequence: {id, type}
sequence: { id, type },
};
}

export default function PromiseMiddleware() {
return next => action => {
const {promise, ...rest} = action;
const { promise, ...rest } = action;
const id = generateRandomId();

if (!promise) {
return next(action);
}

next({...rest, ...sequence(id, START)});
next({ ...rest, ...sequence(id, START) });
return promise.then(
payload => next({...rest, payload, ...sequence(id, DONE)}),
payload => next({ ...rest, payload, ...sequence(id, DONE) }),
reason => {
let error = reason;
// By FSA definition, payload SHOULD be an error object
// Promises in turn don't require reason to be an Error
if (!(error instanceof Error)) {
let message = reason;
if (typeof message !== 'string') {
message = 'Promise rejected with data. See error.data field.';
message =
'Promise rejected with data. See error.data field.';
}
error = new Error(message);
error.data = reason;
}
next({...rest, payload: error, error: true, ...sequence(id)});
next({ ...rest, payload: error, error: true, ...sequence(id) });
return Promise.reject(reason); // Don't break the promise chain
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/ReduxTaxi.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export default function ReduxTaxi() {

getAllPromises() {
return promises;
}
},
};
}
4 changes: 2 additions & 2 deletions src/ReduxTaxiMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
**/
export default function ReduxTaxiMiddleware(reduxTaxi) {
return () => next => action => {
const {promise, type} = action;
const { promise, type } = action;

if (!promise) {
return next(action);
Expand All @@ -27,7 +27,7 @@ export default function ReduxTaxiMiddleware(reduxTaxi) {
reduxTaxi.collectPromise(promise);
} else {
throw new Error(
`The async action ${type} was dispatched in a server context without being explicitly registered.
`The async action ${type} was dispatched in a server context without being explicitly registered.
This usually means an asynchronous action (an action that contains a Promise) was dispatched in a component's instantiation.
Expand Down
10 changes: 5 additions & 5 deletions src/ReduxTaxiProvider.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import React, {Component, Children} from 'react';
import React, { Component, Children } from 'react';
import PropTypes from 'prop-types';

export default class ReduxTaxiProvider extends Component {
static propTypes = {
children: PropTypes.node.isRequired,
reduxTaxi: PropTypes.object.isRequired
reduxTaxi: PropTypes.object.isRequired,
};

static childContextTypes = {
reduxTaxi: PropTypes.object.isRequired
reduxTaxi: PropTypes.object.isRequired,
};

getChildContext() {
return {
reduxTaxi: this.props.reduxTaxi
reduxTaxi: this.props.reduxTaxi,
};
}

render() {
const {children} = this.props;
const { children } = this.props;
return Children.only(children);
}
}
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {default as ReduxTaxi} from './ReduxTaxi';
export {default as ReduxTaxiMiddleware} from './ReduxTaxiMiddleware';
export {default as ReduxTaxiProvider} from './ReduxTaxiProvider';
export {default as registerAsyncActions} from './registerAsyncActions';
export {default as PromiseMiddleware} from './PromiseMiddleware';
export { default as ReduxTaxi } from './ReduxTaxi';
export { default as ReduxTaxiMiddleware } from './ReduxTaxiMiddleware';
export { default as ReduxTaxiProvider } from './ReduxTaxiProvider';
export { default as registerAsyncActions } from './registerAsyncActions';
export { default as PromiseMiddleware } from './PromiseMiddleware';
32 changes: 15 additions & 17 deletions src/registerAsyncActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,22 @@ import PropTypes from 'prop-types';

export default function registerAsyncActions(...actionTypes) {
return DecoratedComponent =>
class AsyncDecorator extends React.Component {
static contextTypes = {
reduxTaxi: PropTypes.object // not required because we don't define it for client context
};
class AsyncDecorator extends React.Component {
static contextTypes = {
reduxTaxi: PropTypes.object, // not required because we don't define it for client context
};

constructor(props, context) {
super(props, context);
if (context.reduxTaxi) {
actionTypes.forEach(actionType => {
context.reduxTaxi.register(actionType);
});
constructor(props, context) {
super(props, context);
if (context.reduxTaxi) {
actionTypes.forEach(actionType => {
context.reduxTaxi.register(actionType);
});
}
}
}

render() {
return (
<DecoratedComponent {...this.props} />
);
}
};
render() {
return <DecoratedComponent {...this.props} />;
}
};
}
Loading

0 comments on commit 12b86d7

Please sign in to comment.