-
Notifications
You must be signed in to change notification settings - Fork 18
/
JSONLDResolver-test.js
340 lines (275 loc) · 11 KB
/
JSONLDResolver-test.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import JSONLDResolver from '../../src/JSONLDResolver';
import context from '../context';
import { namedNode } from '@rdfjs/data-model';
describe('a JSONLDResolver instance', () => {
let resolver;
beforeAll(() => resolver = new JSONLDResolver());
it('supports strings', () => {
expect(resolver.supports('foo')).toBe(true);
});
it('does not support symbols', () => {
expect(resolver.supports(Symbol('foo'))).toBe(false);
});
});
describe('a JSONLDResolver instance with a context', () => {
let resolver;
beforeAll(() => resolver = new JSONLDResolver(context));
describe('expanding a property', () => {
it('expands knows to foaf:knows', async () => {
expect(await resolver.expandProperty('knows'))
.toEqual(namedNode('http://xmlns.com/foaf/0.1/knows'));
});
it('errors when expanding an unknown property', async () => {
await expect(resolver.expandProperty('other')).rejects
.toThrow(new Error("The JSON-LD context cannot expand the 'other' property"));
});
});
describe('resolving the knows property', () => {
const extendedPath = {};
const pathData = { extendPath: jest.fn(() => extendedPath) };
let result;
beforeEach(() => result = resolver.resolve('knows', pathData));
it('extends the path', () => {
expect(pathData.extendPath).toBeCalledTimes(1);
const args = pathData.extendPath.mock.calls[0];
expect(args).toHaveLength(1);
expect(args[0]).toBeInstanceOf(Object);
});
it('sets property to knows', () => {
const { property } = pathData.extendPath.mock.calls[0][0];
expect(property).toBe('knows');
});
it('sets predicate to a promise for foaf:knows', async () => {
const { predicate } = pathData.extendPath.mock.calls[0][0];
expect(await predicate).toEqual(namedNode('http://xmlns.com/foaf/0.1/knows'));
});
it('returns the extended path', () => {
expect(result).toBe(extendedPath);
});
});
describe('resolving properties as functions', () => {
const pathData = { extendPath: jest.fn(x => Object.assign(x, { proxy: x })) };
let result;
beforeEach(() => result = resolver.resolve('knows', pathData));
it('exists', () => {
result = resolver.resolve('knows', pathData);
expect(result.asFunction).toBeInstanceOf(Function);
});
it('errors if there are no arguments', () => {
expect(() => result.asFunction(result, [])).toThrowError();
});
it('errors if the input is not an RDF object', () => {
expect(() => result.asFunction(result, ['Ruben'])).toThrowError();
});
describe('with 2 values', () => {
const ruben = namedNode('Ruben');
const joachim = namedNode('Joachim');
let applied;
beforeEach(() => {
applied = result.asFunction(result, [ruben, joachim]);
});
it('stores the new values in the result', () => {
expect(applied.values).toEqual([ruben, joachim]);
});
it('deletes the asFunction property', () => {
expect(applied).not.toHaveProperty('asFunction');
});
});
});
describe('resolving the foaf:knows property', () => {
const extendedPath = {};
const pathData = { extendPath: jest.fn(() => extendedPath) };
let result;
beforeEach(() => result = resolver.resolve('foaf:knows', pathData));
it('extends the path', () => {
expect(pathData.extendPath).toBeCalledTimes(1);
const args = pathData.extendPath.mock.calls[0];
expect(args).toHaveLength(1);
expect(args[0]).toBeInstanceOf(Object);
});
it('sets property to foaf:knows', () => {
const { property } = pathData.extendPath.mock.calls[0][0];
expect(property).toBe('foaf:knows');
});
it('sets predicate to a promise for foaf:knows', async () => {
const { predicate } = pathData.extendPath.mock.calls[0][0];
expect(await predicate).toEqual(namedNode('http://xmlns.com/foaf/0.1/knows'));
});
it('returns the extended path', () => {
expect(result).toBe(extendedPath);
});
});
describe('resolving the foaf_knows property', () => {
const extendedPath = {};
const pathData = { extendPath: jest.fn(() => extendedPath) };
let result;
beforeEach(() => result = resolver.resolve('foaf_knows', pathData));
it('extends the path', () => {
expect(pathData.extendPath).toBeCalledTimes(1);
const args = pathData.extendPath.mock.calls[0];
expect(args).toHaveLength(1);
expect(args[0]).toBeInstanceOf(Object);
});
it('sets property to foaf_knows', () => {
const { property } = pathData.extendPath.mock.calls[0][0];
expect(property).toBe('foaf_knows');
});
it('sets predicate to a promise for foaf:knows', async () => {
const { predicate } = pathData.extendPath.mock.calls[0][0];
expect(await predicate).toEqual(namedNode('http://xmlns.com/foaf/0.1/knows'));
});
it('returns the extended path', () => {
expect(result).toBe(extendedPath);
});
});
describe('resolving the foaf$knows property', () => {
const extendedPath = {};
const pathData = { extendPath: jest.fn(() => extendedPath) };
let result;
beforeEach(() => result = resolver.resolve('foaf$knows', pathData));
it('extends the path', () => {
expect(pathData.extendPath).toBeCalledTimes(1);
const args = pathData.extendPath.mock.calls[0];
expect(args).toHaveLength(1);
expect(args[0]).toBeInstanceOf(Object);
});
it('sets property to foaf$knows', () => {
const { property } = pathData.extendPath.mock.calls[0][0];
expect(property).toBe('foaf$knows');
});
it('sets predicate to a promise for foaf:knows', async () => {
const { predicate } = pathData.extendPath.mock.calls[0][0];
expect(await predicate).toEqual(namedNode('http://xmlns.com/foaf/0.1/knows'));
});
it('returns the extended path', () => {
expect(result).toBe(extendedPath);
});
});
describe('resolving the foaf:topic_interest property', () => {
const extendedPath = {};
const pathData = { extendPath: jest.fn(() => extendedPath) };
let result;
beforeEach(() => result = resolver.resolve('foaf:topic_interest', pathData));
it('extends the path', () => {
expect(pathData.extendPath).toBeCalledTimes(1);
const args = pathData.extendPath.mock.calls[0];
expect(args).toHaveLength(1);
expect(args[0]).toBeInstanceOf(Object);
});
it('sets property to foaf:topic_interest', () => {
const { property } = pathData.extendPath.mock.calls[0][0];
expect(property).toBe('foaf:topic_interest');
});
it('sets predicate to a promise for foaf:topic_interest', async () => {
const { predicate } = pathData.extendPath.mock.calls[0][0];
expect(await predicate).toEqual(namedNode('http://xmlns.com/foaf/0.1/topic_interest'));
});
it('returns the extended path', () => {
expect(result).toBe(extendedPath);
});
});
describe('resolving the makerOf property', () => {
const extendedPath = {};
const pathData = { extendPath: jest.fn(() => extendedPath) };
let result;
beforeEach(() => result = resolver.resolve('makerOf', pathData));
it('extends the path', () => {
expect(pathData.extendPath).toBeCalledTimes(1);
const args = pathData.extendPath.mock.calls[0];
expect(args).toHaveLength(1);
expect(args[0]).toBeInstanceOf(Object);
});
it('sets property to makerOf', () => {
const { property } = pathData.extendPath.mock.calls[0][0];
expect(property).toBe('makerOf');
});
it('sets predicate to a promise for foaf:maker', async () => {
const { predicate } = pathData.extendPath.mock.calls[0][0];
await expect(predicate).resolves.toEqual(namedNode('http://xmlns.com/foaf/0.1/maker'));
});
it('sets the reverse property to a promise resolving to true', async () => {
const { reverse } = pathData.extendPath.mock.calls[0][0];
await expect(reverse).resolves.toBeTruthy();
});
it('returns the extended path', () => {
expect(result).toBe(extendedPath);
});
});
describe('when the context is extended', () => {
describe('before extending', () => {
it('does not resolve sameAs', async () => {
await expect(resolver.expandProperty('sameAs')).rejects
.toThrow(new Error("The JSON-LD context cannot expand the 'sameAs' property"));
});
});
describe('after extending', () => {
beforeAll(async () => {
await resolver.extendContext({
'@context': {
owl: 'http://www.w3.org/2002/07/owl#',
},
}, {
sameAs: 'owl:sameAs',
});
});
const pathData = { extendPath: jest.fn() };
beforeEach(() => resolver.resolve('sameAs', pathData));
it('sets predicate to a promise for sameAs', async () => {
const { predicate } = pathData.extendPath.mock.calls[0][0];
expect(await predicate).toEqual(namedNode('http://www.w3.org/2002/07/owl#sameAs'));
});
});
});
describe('when a propertyCache is present', () => {
const extendedPath = {};
const pathData = {
extendPath: jest.fn(() => extendedPath),
propertyCache: Promise.resolve({
'http://xmlns.com/foaf/0.1/knows': [
{ subject: namedNode('http://people.example/#Alice') },
{ subject: namedNode('http://people.example/#Bob') },
],
}),
};
describe('when accessing a non-cached property', () => {
let result;
beforeEach(() => result = resolver.resolve('foaf:givenName', pathData));
it('extends the path without a results cache', async () => {
expect(pathData.extendPath).toBeCalledTimes(1);
const args = pathData.extendPath.mock.calls[0];
expect(args).toHaveLength(1);
await expect(args[0].resultsCache).resolves.toBeUndefined();
});
it('returns the extended path', () => {
expect(result).toBe(extendedPath);
});
});
describe('when accessing a cached property', () => {
let result;
beforeEach(() => result = resolver.resolve('foaf:knows', pathData));
it('extends the path with a results cache', async () => {
expect(pathData.extendPath).toBeCalledTimes(1);
const args = pathData.extendPath.mock.calls[0];
expect(args).toHaveLength(1);
await expect(args[0].resultsCache).resolves.toBe(
(await pathData.propertyCache)['http://xmlns.com/foaf/0.1/knows']);
});
it('returns the extended path', () => {
expect(result).toBe(extendedPath);
});
});
describe('when accessing the reverse of a cached property', () => {
let result;
beforeEach(() => result = resolver.resolve('friendOf', pathData));
it('extends the path without a results cache', async () => {
expect(pathData.extendPath).toBeCalledTimes(1);
const args = pathData.extendPath.mock.calls[0];
expect(args).toHaveLength(1);
await expect(args[0].resultsCache).resolves.toBeFalsy();
});
it('returns the extended path', () => {
expect(result).toBe(extendedPath);
});
});
});
});