-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.spec.js
162 lines (148 loc) · 5.24 KB
/
utils.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const { getCacheableNodes, getDefaultIdFromNode, getHash, getHashForQuery } = require('../src/utils');
describe('Utils', () => {
describe('getHash', () => {
it('should return the same hash for the same input', () => {
expect(getHash('asd')).toBe('34403090');
expect(getHash('asd')).toBe('34403090');
});
it('should return a hash for complex inputs', () => {
expect(getHash({ a: 123 })).toBe('1364385693');
expect(getHash(['asd', 'dsa', { foo: 'bar' }])).toBe('-128420855');
});
it('should return a hash for falsy inputs', () => {
expect(getHash()).toBe('0');
expect(getHash('')).toBe('1088');
expect(getHash({})).toBe('3938');
expect(getHash([])).toBe('2914');
});
});
describe('getHashForQuery', () => {
it('should return a hash for a query with variables', () => {
const query = {
query: {
loc: {
source: {
body: 'query body'
}
}
},
variables: {
a: 1,
b: 'foo'
}
};
expect(getHashForQuery(query)).toBe('1650944006___409938244');
});
it('should return a hash for a query without variables', () => {
const query = {
query: {
loc: {
source: {
body: 'query body'
}
}
}
};
expect(getHashForQuery(query)).toBe('1650944006___3938');
});
});
describe('getDefaultIdFromNode', () => {
it('should return undefined when no id / _id is present', () => {
expect(getDefaultIdFromNode({})).toBe(undefined);
});
it('should return string if id / _id is present', () => {
expect(getDefaultIdFromNode({ id: 'a' })).toBe('a');
expect(getDefaultIdFromNode({ _id: 'b' })).toBe('b');
expect(getDefaultIdFromNode({ id: 123 })).toBe(123);
});
});
describe('getCacheableNodes', () => {
it('should extract cacheable nodes from object', () => {
const obj = {
__typename: 'Book',
id: '111',
name: 'Test Book',
author: {
__typename: 'Author',
_id: '222',
name: 'Mr Foo Bar'
},
cover: {
url: 'http://foobar.com/cover.jpg'
}
};
expect(getCacheableNodes(obj, getDefaultIdFromNode)).toEqual({
Book: {
'111': {
__typename: 'Book',
id: '111',
name: 'Test Book',
author: {
__typename: 'Author',
_id: '222',
name: 'Mr Foo Bar'
},
cover: {
url: 'http://foobar.com/cover.jpg'
}
}
},
Author: {
'222': {
__typename: 'Author',
_id: '222',
name: 'Mr Foo Bar'
}
}
});
});
it('should generate IDs using the provided function', () => {
const idGeneratorFn = obj => {
if (obj.__typename === 'Book') {
return `${obj.id}::${obj.name}`;
} else if (obj.__typename === 'Author') {
return `${obj._id}__${obj.name.split('').reverse().join('')}`;
} else {
return String(obj.id || obj._id);
}
};
const obj = {
__typename: 'Book',
id: '111',
name: 'Test Book',
author: {
__typename: 'Author',
_id: '222',
name: 'Mr Foo Bar'
},
cover: {
url: 'http://foobar.com/cover.jpg'
}
};
expect(getCacheableNodes(obj, idGeneratorFn)).toEqual({
Book: {
'111::Test Book': {
__typename: 'Book',
id: '111',
name: 'Test Book',
author: {
__typename: 'Author',
_id: '222',
name: 'Mr Foo Bar'
},
cover: {
url: 'http://foobar.com/cover.jpg'
}
}
},
Author: {
'222__raB ooF rM': {
__typename: 'Author',
_id: '222',
name: 'Mr Foo Bar'
}
}
});
});
});
});