Skip to content

Commit

Permalink
Merge pull request #12 in ONE/vitruvius from release/v1.2.0 to master
Browse files Browse the repository at this point in the history
* commit '4051898a3d64fc5b569962497468c054e58c9f20':
  chore(package): iterate version to 1.2.0
  feat(immutable): add support for redux-immutable
  • Loading branch information
10xLaCroixDrinker committed Jun 20, 2017
2 parents 461e597 + 4051898 commit 494ada3
Show file tree
Hide file tree
Showing 13 changed files with 449 additions and 129 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ Vitruvius extends redux's `combineReducers` to allow developers to include a
`buildInitialState` method on their reducer. This allows for the passing of
locals to build the initial state that wouldn't normally be available to a
reducer when setting its initial state. For instance, one could pass some data
from the request object. Below is an example of a reducer implementing a
`buildInitialState` method and an example of virtuvius being implemented.
from the request object.

## Installation

```
$ npm install --save @americanexpress/vitruvius
```

## Implementation

Below is an example of a reducer implementing a `buildInitialState` method and
an example of vitruvius being implemented.

```js
import { Map } from 'immutable';
Expand All @@ -26,8 +36,13 @@ export default function reducer(state = buildInitialState(), action) {
reducer.buildInitialState = buildInitialState;
```

> **TIP**: To extend `combineReducers` from `redux-immutable` instead of `redux`
import from `vitruvius/immutable`.

```js
const reducer = virtuvius({
import vitruvius from 'vitruvius';

const reducer = vitruvius({
stuff: stuffReducer,
things: thingsReducer,
...otherReducers,
Expand All @@ -36,6 +51,7 @@ const reducer = virtuvius({
const store = createStore(reducer, reducer.buildInitialState(locals), enhancer);
```


## Contributing
We welcome Your interest in the American Express Open Source Community on Github.
Any Contributor to any Open Source Project managed by the American Express Open
Expand Down
29 changes: 29 additions & 0 deletions __tests__/__snapshots__/collectBuiltState.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`collectBuiltState should build a mutable state 1`] = `
Object {
"other": Immutable.Map {
baz: "test",
},
"stuff": Immutable.Map {
foo: "test",
},
"things": Immutable.Map {
bar: "test",
},
}
`;

exports[`collectBuiltState should build an immutable state 1`] = `
Immutable.Map {
stuff: Immutable.Map {
foo: "test",
},
things: Immutable.Map {
bar: "test",
},
other: Immutable.Map {
baz: "test",
},
}
`;
81 changes: 81 additions & 0 deletions __tests__/__snapshots__/immutable.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`vitruviusImmutable should build the expected state for a flat tree of reducers 1`] = `
Immutable.Map {
stuff: Immutable.Map {
foo: "test",
},
things: Immutable.Map {
bar: "test",
},
other: Immutable.Map {
baz: "test",
},
}
`;

exports[`vitruviusImmutable should build the expected state for a nested tree of reducers 1`] = `
Immutable.Map {
stuff: Immutable.Map {
foo: "test",
},
nested: Immutable.Map {
things: Immutable.Map {
bar: "test",
},
other: Immutable.Map {
baz: "test",
},
},
}
`;

exports[`vitruviusImmutable should handle a mix of reducers having and not having buildInitialState method 1`] = `
Immutable.Map {
stuff: Immutable.Map {
foo: "test",
},
things: Immutable.Map {
bar: "test",
},
other: Immutable.Map {
baz: "test",
},
}
`;

exports[`vitruviusImmutable should handle all reducers having buildInitialState method 1`] = `
Immutable.Map {
stuff: Immutable.Map {
foo: "test",
},
things: Immutable.Map {
bar: "test",
},
other: Immutable.Map {
baz: "test",
},
}
`;

exports[`vitruviusImmutable should handle no reducers having buildInitialState method 1`] = `
Immutable.Map {
}
`;

exports[`vitruviusImmutable should return an initialState that is acceptable to redux's createStore 1`] = `
Immutable.Map {
stuff: Immutable.Map {
foo: "test",
},
things: Immutable.Map {
bar: "test",
},
other: Immutable.Map {
baz: "test",
},
static: Immutable.Map {
static: true,
},
}
`;
78 changes: 78 additions & 0 deletions __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`vitruvius should build the expected state for a flat tree of reducers 1`] = `
Object {
"other": Immutable.Map {
baz: "test",
},
"stuff": Immutable.Map {
foo: "test",
},
"things": Immutable.Map {
bar: "test",
},
}
`;

exports[`vitruvius should build the expected state for a nested tree of reducers 1`] = `
Object {
"nested": Object {
"other": Immutable.Map {
baz: "test",
},
"things": Immutable.Map {
bar: "test",
},
},
"stuff": Immutable.Map {
foo: "test",
},
}
`;

exports[`vitruvius should handle a mix of reducers having and not having buildInitialState method 1`] = `
Object {
"other": Immutable.Map {
baz: "test",
},
"stuff": Immutable.Map {
foo: "test",
},
"things": Immutable.Map {
bar: "test",
},
}
`;

exports[`vitruvius should handle all reducers having buildInitialState method 1`] = `
Object {
"other": Immutable.Map {
baz: "test",
},
"stuff": Immutable.Map {
foo: "test",
},
"things": Immutable.Map {
bar: "test",
},
}
`;

exports[`vitruvius should handle no reducers having buildInitialState method 1`] = `Object {}`;

exports[`vitruvius should return an initialState that is acceptable to redux's createStore 1`] = `
Object {
"other": Immutable.Map {
baz: "test",
},
"static": Immutable.Map {
static: true,
},
"stuff": Immutable.Map {
foo: "test",
},
"things": Immutable.Map {
bar: "test",
},
}
`;
54 changes: 54 additions & 0 deletions __tests__/collectBuiltState.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2017 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

import { Map } from 'immutable';
import collectBuiltState from '../src/collectBuiltState';
import {
stuffReducer,
thingsReducer,
otherReducer,
staticReducer,
} from './fixtures';

describe('collectBuiltState', () => {
const locals = { data: 'test' };

it('should build an immutable state', () => {
const result = collectBuiltState({
reducers: {
stuff: stuffReducer,
things: thingsReducer,
other: otherReducer,
static: staticReducer,
},
locals,
defaultState: new Map(),
});
expect(result).toMatchSnapshot();
});

it('should build a mutable state', () => {
const result = collectBuiltState({
reducers: {
stuff: stuffReducer,
things: thingsReducer,
other: otherReducer,
static: staticReducer,
},
locals,
defaultState: {},
});
expect(result).toMatchSnapshot();
});
});
71 changes: 71 additions & 0 deletions __tests__/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

import { Map } from 'immutable';
import { createStore } from 'redux';

export const SOME_ACTION = 'SOME_ACTION';

Expand Down Expand Up @@ -63,3 +64,73 @@ export function staticReducer(state = new Map({ static: true }), action) {
return state;
}
}

export function runVitruviusTests(describeSpec, vitruvius) {
return describe(describeSpec, () => {
const locals = { data: 'test' };

it('should handle all reducers having buildInitialState method', () => {
const reducer = vitruvius({
stuff: stuffReducer,
things: thingsReducer,
other: otherReducer,
});
const actual = reducer.buildInitialState(locals);
expect(actual).toMatchSnapshot();
});

it('should handle no reducers having buildInitialState method', () => {
const reducer = vitruvius({
static: staticReducer,
});
const actual = reducer.buildInitialState(locals);
expect(actual).toMatchSnapshot();
});

it('should handle a mix of reducers having and not having buildInitialState method', () => {
const reducer = vitruvius({
stuff: stuffReducer,
things: thingsReducer,
other: otherReducer,
static: staticReducer,
});
const actual = reducer.buildInitialState(locals);
expect(actual).toMatchSnapshot();
});

it('should build the expected state for a flat tree of reducers', () => {
const reducer = vitruvius({
stuff: stuffReducer,
things: thingsReducer,
other: otherReducer,
static: staticReducer,
});
const actual = reducer.buildInitialState(locals);
expect(actual).toMatchSnapshot();
});

it('should build the expected state for a nested tree of reducers', () => {
const reducer = vitruvius({
stuff: stuffReducer,
nested: vitruvius({
things: thingsReducer,
other: otherReducer,
}),
});
const actual = reducer.buildInitialState(locals);
expect(actual).toMatchSnapshot();
});

it('should return an initialState that is acceptable to redux\'s createStore', () => {
const reducer = vitruvius({
stuff: stuffReducer,
things: thingsReducer,
other: otherReducer,
static: staticReducer,
});
const store = createStore(reducer, reducer.buildInitialState(locals));
const actual = store.getState();
expect(actual).toMatchSnapshot();
});
});
}
27 changes: 27 additions & 0 deletions __tests__/immutable.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2017 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

import vitruviusImmutable from '../src/immutable';
import vitruviusImmutableExport from '../immutable';
import { runVitruviusTests } from './fixtures';

jest.mock('../lib/immutable', () => require('../src/immutable'), { virtual: true });

runVitruviusTests('vitruviusImmutable', vitruviusImmutable);

describe('vitruviusImmutable export', () => {
it('should export vitruviusImmutable', () => {
expect(vitruviusImmutableExport.name).toBe('vitruviusImmutable');
});
});
Loading

0 comments on commit 494ada3

Please sign in to comment.