Skip to content

Commit 889f134

Browse files
Merge pull request #68 from pjweisberg/remove-string-ids
Better handling of string IDs
2 parents cb093d1 + 712a7a6 commit 889f134

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

src/resolver/Mutation/remove.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
export default (entityData = []) => (_, { id }) => {
2-
const parsedId = parseInt(id, 10); // FIXME fails for non-integer ids
3-
const indexOfEntity = entityData.findIndex(e => e.id === parsedId);
42
let removedEntity = undefined;
3+
if (id != null) {
4+
const stringId = id.toString();
5+
const indexOfEntity = entityData.findIndex(
6+
e => e.id != null && e.id.toString() === stringId
7+
);
58

6-
if (indexOfEntity !== -1) {
7-
removedEntity = entityData.splice(indexOfEntity, 1)[0];
9+
if (indexOfEntity !== -1) {
10+
removedEntity = entityData.splice(indexOfEntity, 1)[0];
11+
}
812
}
913
return removedEntity;
1014
};

src/resolver/Mutation/remove.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,21 @@ test('removes record when found', () => {
2727
remove(data)(null, { id: 1 });
2828
expect(data).toEqual([{ id: 2, value: 'bar' }]);
2929
});
30+
31+
test('removes with string data id', () => {
32+
const data = [{ id: '1', value: 'foo' }, { id: '2', value: 'bar' }];
33+
remove(data)(null, { id: 1 });
34+
expect(data).toEqual([{ id: '2', value: 'bar' }]);
35+
});
36+
37+
test('removes with string input and data ids', () => {
38+
const data = [{ id: 'abc', value: 'foo' }, { id: 'def', value: 'bar' }];
39+
remove(data)(null, { id: 'abc' });
40+
expect(data).toEqual([{ id: 'def', value: 'bar' }]);
41+
});
42+
43+
test("doesn't confuse undefined id with the id 'undefined'", () => {
44+
const data = [{ value: 'foo' }, { id: 'def', value: 'bar' }];
45+
expect(remove(data)(null, { id: 'undefined' })).toBeUndefined();
46+
expect(data).toEqual([{ value: 'foo' }, { id: 'def', value: 'bar' }]);
47+
});

src/resolver/Mutation/update.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
export default (entityData = []) => (_, params) => {
2-
const parsedId = parseInt(params.id, 10); // FIXME fails for non-integer ids
3-
const indexOfEntity = entityData.findIndex(
4-
e => parseInt(e.id, 10) === parsedId
5-
);
6-
if (indexOfEntity !== -1) {
7-
entityData[indexOfEntity] = Object.assign(
8-
{},
9-
entityData[indexOfEntity],
10-
params
2+
let updatedEntity = undefined;
3+
if (params.id != null) {
4+
const stringId = params.id.toString();
5+
const indexOfEntity = entityData.findIndex(
6+
e => e.id != null && e.id.toString() === stringId
117
);
12-
return entityData[indexOfEntity];
8+
if (indexOfEntity !== -1) {
9+
entityData[indexOfEntity] = Object.assign(
10+
{},
11+
entityData[indexOfEntity],
12+
params
13+
);
14+
updatedEntity = entityData[indexOfEntity];
15+
}
1316
}
17+
return updatedEntity;
1418
};

src/resolver/Mutation/update.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,22 @@ test('updates record when found', () => {
2424
expect(data).toEqual([{ id: 1, value: 'bar', bar: 'baz' }]);
2525
});
2626

27+
test('updates record with string id', () => {
28+
const data = [{ id: 'abc', value: 'foo' }];
29+
update(data)(null, { id: 'abc', value: 'bar', bar: 'baz' });
30+
expect(data).toEqual([{ id: 'abc', value: 'bar', bar: 'baz' }]);
31+
});
32+
2733
test('removes property when setting the value to undefined', () => {
2834
const data = [{ id: 1, value: 'foo' }];
2935
update(data)(null, { id: 1, value: undefined });
3036
expect(data).toEqual([{ id: 1 }]);
3137
});
38+
39+
test("doesn't confuse undefined id with the id 'undefined'", () => {
40+
const data = [{ value: 'foo' }];
41+
expect(
42+
update(data)(null, { id: 'undefined', value: 'bar', bar: 'baz' })
43+
).toBeUndefined();
44+
expect(data).toEqual([{ value: 'foo' }]);
45+
});

0 commit comments

Comments
 (0)