Skip to content

Commit

Permalink
1.0.0-alpha.5
Browse files Browse the repository at this point in the history
Fix broken behavior introduced by array store
  • Loading branch information
albancreton authored May 3, 2019
2 parents 1417a87 + 41dedfa commit 94887c4
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 40 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Redeuce Changelog

## 1.0.0-alpha.5 (2019-05-02)

- Fix a major bug introduced in alpha.4 (breaking action creator and reducers arity)
- Full Documentation

## 1.0.0-alpha.4 (2019-04-24)

- New arrayStore
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redeuce",
"version": "1.0.0-alpha.4",
"version": "1.0.0-alpha.5",
"main": "dist/redeuce.min.js",
"engines": {
"node": ">=8.9",
Expand Down
51 changes: 27 additions & 24 deletions src/generators/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ export const COLLECTION = 'COLLECTION';
export const ARRAY = 'ARRAY';
export const VERBS = {
[SIMPLE]: {
SET: 'set',
SET: { name: 'set' },
},
[COLLECTION]: {
SET: 'set',
UPDATE: 'update',
DELETE: 'delete',
MERGE: 'merge',
MERGEDEEP: 'mergeDeep',
DELETEALL: 'deleteAll',
CLEAR: 'clear',
SET: { name: 'set' },
UPDATE: { name: 'update' },
DELETE: { name: 'delete' },
MERGE: { name: 'merge' },
MERGEDEEP: { name: 'mergeDeep' },
DELETEALL: { name: 'deleteAll' },
CLEAR: { name: 'clear' },
},
[ARRAY]: {
SET: 'set',
SETIN: 'setIn',
DELETE: 'delete',
INSERT: 'insert',
CLEAR: 'clear',
PUSH: 'push',
POP: 'pop',
UNSHIFT: 'unshift',
SHIFT: 'shift',
SET: { name: 'set', arity: true },
SETIN: { name: 'setIn', arity: true },
DELETE: { name: 'delete' },
INSERT: { name: 'insert', arity: true },
CLEAR: { name: 'clear' },
PUSH: { name: 'push', arity: true },
POP: { name: 'pop', arity: true },
UNSHIFT: { name: 'unshift', arity: true },
SHIFT: { name: 'shift', arity: true },
},
};

Expand All @@ -38,14 +38,17 @@ export const generateActionTypes = (storeType, entityId) =>
{},
);

const generateActionCreator = type => (...payload) => ({ type, payload });
const generateActionCreator = (type, arity) => (...payload) =>
arity ? { type, payload } : { type, payload: payload[0] };

export const generateActionCreators = (storeType, entityId) => {
const actionTypes = generateActionTypes(storeType, entityId);
return Object.keys(VERBS[storeType]).reduce(
(actionCreators, verb) => ({
return Object.keys(VERBS[storeType]).reduce((actionCreators, verb) => {
const { name, arity } = VERBS[storeType][verb];

return {
...actionCreators,
[VERBS[storeType][verb]]: generateActionCreator(actionTypes[verb]),
}),
{},
);
[name]: generateActionCreator(actionTypes[verb], arity),
};
}, {});
};
10 changes: 5 additions & 5 deletions src/generators/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const buildCollectionReducers = (actionTypes, keyName) => {

// Reducer to SET a list of objects in the store
const setReducer = (state, payload) => {
const filteredPayload = filterByKeyname(asArray(payload[0]), keyName);
const filteredPayload = filterByKeyname(asArray(payload), keyName);
const setIndexes = getKeyNameValues(filteredPayload, keyName);

return [
Expand All @@ -35,7 +35,7 @@ export const buildCollectionReducers = (actionTypes, keyName) => {

// Reducer to UPDATE a list of objects in the store
const updateReducer = (state, payload) => {
const filteredPayload = filterByKeyname(asArray(payload[0]), keyName);
const filteredPayload = filterByKeyname(asArray(payload), keyName);
const updateIndexes = getKeyNameValues(filteredPayload, keyName);
const existingIndexes = getKeyNameValues(state, keyName);

Expand All @@ -55,7 +55,7 @@ export const buildCollectionReducers = (actionTypes, keyName) => {

// Reducer to DELETE a list of objects in the store
const deleteReducer = (state, payload) => {
const filteredPayload = filterByKeyname(asArray(payload[0]), keyName);
const filteredPayload = filterByKeyname(asArray(payload), keyName);
const setIndexes = getKeyNameValues(filteredPayload, keyName);

return [
Expand All @@ -81,7 +81,7 @@ export const buildSimpleReducer = actionTypes => {
const { SET } = actionTypes;

// Reducer to SET a list of objects in the store
const setReducer = (_, payload) => payload[0];
const setReducer = (_, payload) => payload;

return {
[SET]: setReducer,
Expand All @@ -94,7 +94,7 @@ export const buildArrayReducers = (actionTypes, keyName) => {
const popReducer = state => state.slice(0, state.length - 1);
const unshiftReducer = (state, payload) => [...payload, ...state];
const shiftReducer = state => state.slice(1, state.length);
const deleteReducer = (state, payload) => state.filter((_, i) => i !== payload[0]);
const deleteReducer = (state, payload) => state.filter((_, i) => i !== payload);
const insertReducer = (state, payload) => {
let a = [...state];
a.splice(payload[1], 0, payload[0]);
Expand Down
11 changes: 9 additions & 2 deletions tests/arrayStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('Array Store', () => {
test('delete', () => {
expect(actions.delete(2)).toEqual({
type: `REDEUCE:ARRAY@@${entityName}@@DELETE`,
payload: [2],
payload: 2,
});
});

Expand All @@ -111,7 +111,6 @@ describe('Array Store', () => {
test('clear', () => {
expect(actions.clear()).toEqual({
type: `REDEUCE:ARRAY@@${entityName}@@CLEAR`,
payload: [],
});
});

Expand All @@ -121,6 +120,14 @@ describe('Array Store', () => {
type: `REDEUCE:ARRAY@@${entityName}@@${verb.toUpperCase()}`,
payload: ['hello'],
});
expect(actions[verb]('hello', 'world')).toEqual({
type: `REDEUCE:ARRAY@@${entityName}@@${verb.toUpperCase()}`,
payload: ['hello', 'world'],
});
expect(actions[verb]('hello', 'cruel', 'world')).toEqual({
type: `REDEUCE:ARRAY@@${entityName}@@${verb.toUpperCase()}`,
payload: ['hello', 'cruel', 'world'],
});
});
});
});
Expand Down
13 changes: 6 additions & 7 deletions tests/collectionStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,44 +73,43 @@ describe('Collection Store', () => {
test('single entity set action creator', () => {
expect(actions.set('hello')).toEqual({
type: `REDEUCE:COLLECTION@@${entityName}@@SET`,
payload: ['hello'],
payload: 'hello',
});
});
test('single update action creator', () => {
expect(actions.update('hello')).toEqual({
type: `REDEUCE:COLLECTION@@${entityName}@@UPDATE`,
payload: ['hello'],
payload: 'hello',
});
});
test('single delete action creator', () => {
expect(actions.delete('hello')).toEqual({
type: `REDEUCE:COLLECTION@@${entityName}@@DELETE`,
payload: ['hello'],
payload: 'hello',
});
});

test('bulk entities set action creator', () => {
expect(actions.merge('hello')).toEqual({
type: `REDEUCE:COLLECTION@@${entityName}@@MERGE`,
payload: ['hello'],
payload: 'hello',
});
});
test('bulk update action creator', () => {
expect(actions.mergeDeep('hello')).toEqual({
type: `REDEUCE:COLLECTION@@${entityName}@@MERGEDEEP`,
payload: ['hello'],
payload: 'hello',
});
});
test('bulk delete action creator', () => {
expect(actions.deleteAll('hello')).toEqual({
type: `REDEUCE:COLLECTION@@${entityName}@@DELETEALL`,
payload: ['hello'],
payload: 'hello',
});
});
test('clear action creator', () => {
expect(actions.clear()).toEqual({
type: `REDEUCE:COLLECTION@@${entityName}@@CLEAR`,
payload: [],
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/simpleStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('Simple Store', () => {
test('single entity set action creator', () => {
expect(actions.set('hello')).toEqual({
type: `REDEUCE:SIMPLE@@${entityName}@@SET`,
payload: ['hello'],
payload: 'hello',
});
});
});
Expand Down

0 comments on commit 94887c4

Please sign in to comment.