Skip to content

Commit def52ba

Browse files
committed
Add unit tests for reducers
...and make them work (!)
1 parent 085270b commit def52ba

File tree

8 files changed

+107
-12
lines changed

8 files changed

+107
-12
lines changed

src/resolver/Mutation/create.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
export default entityData => (_, entity) => {
1+
export default (entityData = []) => (_, entity) => {
2+
const newId =
3+
entityData.length > 0 ? entityData[entityData.length - 1].id + 1 : 0;
24
const newEntity = {
3-
id: entityData[entityData.length - 1].id + 1,
5+
id: newId,
46
...entity,
57
};
68

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import create from './create';
2+
3+
test('returns a new object with id 0 on empty datastore', () => {
4+
expect(create()(null, {})).toEqual({ id: 0 });
5+
});
6+
7+
test('returns a new object with incremental id', () => {
8+
const data = [{ id: 1 }, { id: 3 }];
9+
expect(create(data)(null, {})).toEqual({ id: 4 });
10+
});
11+
12+
test('returns a new object using create data', () => {
13+
const data = [{ id: 0, value: 'foo' }];
14+
expect(create(data)(null, { value: 'toto' })).toEqual({
15+
id: 1,
16+
value: 'toto',
17+
});
18+
});
19+
20+
test('creates a new record', () => {
21+
const data = [{ id: 1 }, { id: 3 }];
22+
create(data)(null, { value: 'foo' });
23+
expect(data).toEqual([{ id: 1 }, { id: 3 }, { id: 4, value: 'foo' }]);
24+
});

src/resolver/Mutation/remove.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export default entityData => (_, { id }) => {
1+
export default (entityData = []) => (_, { id }) => {
22
const parsedId = parseInt(id, 10); // FIXME fails for non-integer ids
33
const indexOfEntity = entityData.findIndex(e => e.id === parsedId);
44
const removedEntity = entityData[indexOfEntity];
55

6-
entityData = entityData.filter(e => e.id !== id);
6+
entityData.splice(indexOfEntity, 1);
77
return removedEntity;
88
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import remove from './remove';
2+
3+
test('returns undefined by default', () => {
4+
expect(remove()(null, {})).toBeUndefined();
5+
});
6+
7+
test('returns removed record when found', () => {
8+
const data = [{ id: 1, value: 'foo' }, { id: 2, value: 'bar' }];
9+
expect(remove(data)(null, { id: 1 })).toEqual({ id: 1, value: 'foo' });
10+
});
11+
12+
test('returns undefined when not found', () => {
13+
const data = [{ id: 1, value: 'foo' }, { id: 2, value: 'bar' }];
14+
expect(remove(data)(null, { id: 3 })).toBeUndefined();
15+
});
16+
17+
test('removes record when found', () => {
18+
const data = [{ id: 1, value: 'foo' }, { id: 2, value: 'bar' }];
19+
remove(data)(null, { id: 1 });
20+
expect(data).toEqual([{ id: 2, value: 'bar' }]);
21+
});

src/resolver/Mutation/update.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
export default entityData => (_, { id, ...updates }) => {
1+
export default (entityData = []) => (_, { id, ...updates }) => {
22
const parsedId = parseInt(id, 10); // FIXME fails for non-integer ids
33
const indexOfEntity = entityData.findIndex(e => e.id === parsedId);
4-
5-
entityData[indexOfEntity] = {
6-
...entityData[indexOfEntity],
7-
...updates,
8-
};
9-
return entityData[indexOfEntity];
4+
if (indexOfEntity !== -1) {
5+
entityData[indexOfEntity] = {
6+
...entityData[indexOfEntity],
7+
...updates,
8+
};
9+
return entityData[indexOfEntity];
10+
}
1011
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import update from './update';
2+
3+
test('returns undefined by default', () => {
4+
expect(update()(null, {})).toBeUndefined();
5+
});
6+
7+
test('returns updated record when found', () => {
8+
const data = [{ id: 1, value: 'foo', bar: 'baz' }];
9+
expect(update(data)(null, { id: 1, value: 'bar' })).toEqual({
10+
id: 1,
11+
value: 'bar',
12+
bar: 'baz',
13+
});
14+
});
15+
16+
test('returns undefined when not found', () => {
17+
const data = [{ id: 1, value: 'foo' }, { id: 2, value: 'bar' }];
18+
expect(update(data)(null, { id: 3 })).toBeUndefined();
19+
});
20+
21+
test('updates record when found', () => {
22+
const data = [{ id: 1, value: 'foo' }];
23+
update(data)(null, { id: 1, value: 'bar', bar: 'baz' });
24+
expect(data).toEqual([{ id: 1, value: 'bar', bar: 'baz' }]);
25+
});
26+
27+
test('removes property when setting the value to undefined', () => {
28+
const data = [{ id: 1, value: 'foo' }];
29+
update(data)(null, { id: 1, value: undefined });
30+
expect(data).toEqual([{ id: 1 }]);
31+
});

src/resolver/Query/single.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export default entityData => (_, { id }) => entityData.find(d => d.id == id);
1+
export default (entityData = []) => (_, { id }) =>
2+
entityData.find(d => d.id == id);

src/resolver/Query/single.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import single from './single';
2+
3+
test('returns undefined by default', () => {
4+
expect(single()(null, {})).toBeUndefined();
5+
});
6+
7+
test('returns record when found', () => {
8+
const data = [{ id: 1, value: 'foo' }, { id: 2, value: 'bar' }];
9+
expect(single(data)(null, { id: 1 })).toEqual({ id: 1, value: 'foo' });
10+
});
11+
12+
test('returns undefined when not found', () => {
13+
const data = [{ id: 1, value: 'foo' }, { id: 2, value: 'bar' }];
14+
expect(single(data)(null, { id: 3 })).toBeUndefined();
15+
});

0 commit comments

Comments
 (0)