Skip to content

Commit da55840

Browse files
committed
Normalize still works when server does not send an 'included' or when relationship is null
1 parent 7443bb3 commit da55840

File tree

4 files changed

+101
-6
lines changed

4 files changed

+101
-6
lines changed

src/functions/find-items.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ export function findItems(response: any, temp: any) {
1818
throw '[Normalize] [JSON:API Syntax Error] A data relationship item does not contain id field!';
1919
}
2020
const result = [];
21-
// Adding all found results
22-
result.push(
23-
response.included.find((item: any) => (String(item.id) === String(temp.id)) &&
24-
(String(item.type) === String(temp.type))),
25-
);
21+
22+
// If response has a 'included' property then look for results
23+
if (response.hasOwnProperty('included')) {
24+
// Adding all found results
25+
result.push(
26+
response.included.find((item: any) => (String(item.id) === String(temp.id)) &&
27+
(String(item.type) === String(temp.type))),
28+
);
29+
}
2630
return result;
2731
}

src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ export default function normalize(response: any) {
4747
// Using a for loop to go through all relationships of every item in data
4848
for (const key of Object.keys(item.relationships)) {
4949
const relation = item.relationships[key];
50+
51+
// Skip if relationship is not present
52+
if (!relation.data) {
53+
continue;
54+
}
55+
5056
// Checking if there is only one relationship stored in an object
5157
if (relation.data.constructor === Array) {
5258
// Checking if a data relationship item has type field

src/test/normalize.spec.ts

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Validation test file
3+
* Description: This file contains tests which check if any modifications
4+
* make the new code incompatible with the old one.
5+
*/
6+
7+
// Importing the normalize function
8+
import normalize from '../index';
9+
// Importing mocha and chai for the tests
10+
import { expect } from 'chai';
11+
import 'mocha';
12+
13+
// Creating the test set
14+
describe('normalize test', () => {
15+
it('should normalize when included relationship is null', () => {
16+
const test = {
17+
"data":[
18+
{
19+
"id":"3",
20+
"type":"conversation",
21+
"attributes":{
22+
"subject":"Test subject"
23+
},
24+
"relationships":{
25+
"last_message":{
26+
"data":{ type: "people", id: "9" }
27+
}
28+
}
29+
},
30+
],
31+
"included": [],
32+
};
33+
expect(() => normalize(test)).to.not.throw();
34+
const normalized = normalize(test);
35+
expect(normalized).to.have.property('conversation');
36+
});
37+
it('should normalize when property "included" is undefined', () => {
38+
const test = {
39+
"data":[
40+
{
41+
"id":"3",
42+
"type":"conversation",
43+
"attributes":{
44+
"subject":"Test subject"
45+
},
46+
"relationships":{
47+
"last_message":{
48+
"data":{ type: "people", id: "9" }
49+
}
50+
}
51+
},
52+
],
53+
};
54+
expect(() => normalize(test)).to.not.throw();
55+
const normalized = normalize(test);
56+
expect(normalized).to.have.property('conversation');
57+
});
58+
it('should normalize when property "included" is undefined and relationship is null', () => {
59+
const test = {
60+
"data":[
61+
{
62+
"id":"3",
63+
"type":"conversation",
64+
"attributes":{
65+
"subject":"Test subject"
66+
},
67+
"relationships":{
68+
"last_message":{
69+
"data": null
70+
}
71+
}
72+
},
73+
],
74+
};
75+
expect(() => normalize(test)).to.not.throw();
76+
const normalized = normalize(test);
77+
expect(normalized).to.have.property('conversation');
78+
});
79+
});

src/test/validate.spec.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,10 @@ describe('Validation test', () => {
5151
validate(test);
5252
}).to.throw('[Normalize] [JSON:API Syntax Error] Not all data items are the same type!');
5353
});
54-
});
54+
it('should throw that not all data items are the same type', () => {
55+
const test = { data: [{ type: 'article' }, { type: 'person' }] };
56+
expect(() => {
57+
validate(test);
58+
}).to.throw('[Normalize] [JSON:API Syntax Error] Not all data items are the same type!');
59+
});
60+
});

0 commit comments

Comments
 (0)