Skip to content

Commit 6ec0ae7

Browse files
author
JF
committed
tests
1 parent ce87b51 commit 6ec0ae7

File tree

5 files changed

+108
-61
lines changed

5 files changed

+108
-61
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"eslint-plugin-import": "^2.2.0",
3030
"eslint-plugin-prettier": "^2.6.0",
3131
"flow-bin": "^0.42.0",
32-
"jest": "^20.0.0",
32+
"jest": "^23.0.0",
3333
"jest-fetch-mock": "^1.0.8",
3434
"prettier": "^1.12.1"
3535
},

src/hydra/addFilters.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Filter from "../Filter";
2+
import fetchResource from "./fetchResource";
3+
4+
export default api => {
5+
const promises = [];
6+
7+
for (const resource of api.resources) {
8+
let promise = fetchResource(resource.url).then(response => {
9+
const resourceFilters = [];
10+
11+
if (response.filters) {
12+
for (const filter of response.filters) {
13+
let property = filter.property;
14+
15+
// TODO : To prevent PropertyFilter, maybe should be handle specifically ?
16+
if (property === null) {
17+
continue;
18+
}
19+
20+
const resourceFilter = new Filter(property, filter.variable);
21+
22+
resourceFilters.push(resourceFilter);
23+
}
24+
}
25+
26+
return resourceFilters;
27+
});
28+
29+
promises.push(promise);
30+
}
31+
32+
return Promise.all(promises).then(values => {
33+
api.resources.map((resource, index) => {
34+
resource.filters = values[index];
35+
});
36+
37+
return api;
38+
});
39+
};

src/hydra/fetchResource.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import fetchJsonLd from "./fetchJsonLd";
2+
import get from "lodash.get";
3+
4+
export default async entrypointUrl => {
5+
return await fetchJsonLd(entrypointUrl, { items_per_page: 0 }).then(
6+
d => ({
7+
filters: get(d, "body.hydra:search.hydra:mapping")
8+
}),
9+
() => {
10+
throw new Error("Unreachable resource");
11+
}
12+
);
13+
};

src/hydra/parseHydraDocumentation.js

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { promises } from "jsonld";
22
import get from "lodash.get";
33
import Api from "../Api";
44
import Field from "../Field";
5-
import Filter from "../Filter";
65
import Resource from "../Resource";
76
import Operation from "../Operation";
87
import fetchJsonLd from "./fetchJsonLd";
8+
import addFilters from "./addFilters";
99

1010
/**
1111
* Extracts the short name of a resource.
@@ -118,14 +118,6 @@ function fetchEntrypointAndDocs(entrypointUrl, options = {}) {
118118
);
119119
}
120120

121-
function fetchResource(entrypointUrl) {
122-
return fetchJsonLd(entrypointUrl, { items_per_page: 0 }).then(d => {
123-
return {
124-
filters: get(d, "body.hydra:search.hydra:mapping")
125-
};
126-
});
127-
}
128-
129121
function removeTrailingSlash(url) {
130122
if (/\/$/.test(url)) {
131123
url = url.slice(0, -1);
@@ -189,43 +181,6 @@ function findRelatedClass(docs, property) {
189181
throw new Error(`Cannot find the class related to ${property["@id"]}.`);
190182
}
191183

192-
function addFilters(api) {
193-
const promises = [];
194-
195-
for (const resource of api.resources) {
196-
let promise = fetchResource(resource.url).then(response => {
197-
const resourceFilters = [];
198-
199-
if (response.filters) {
200-
for (const filter of response.filters) {
201-
let property = filter.property;
202-
203-
// TODO : To prevent PropertyFilter, maybe should be handle specifically ?
204-
if (property === null) {
205-
continue;
206-
}
207-
208-
const resourceFilter = new Filter(property, filter.variable);
209-
210-
resourceFilters.push(resourceFilter);
211-
}
212-
}
213-
214-
return resourceFilters;
215-
});
216-
217-
promises.push(promise);
218-
}
219-
220-
return Promise.all(promises).then(values => {
221-
api.resources.map((resource, index) => {
222-
resource.filters = values[index];
223-
});
224-
225-
return api;
226-
});
227-
}
228-
229184
/**
230185
* Parses a Hydra documentation and converts it to an intermediate representation.
231186
*
@@ -464,7 +419,8 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
464419
relatedClass,
465420
'["http://www.w3.org/2002/07/owl#deprecated"][0]["@value"]',
466421
false
467-
)
422+
),
423+
filters: []
468424
})
469425
);
470426
}

src/hydra/parseHydraDocumentation.test.js

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,24 @@ const docs = `{
552552
]
553553
}`;
554554

555+
const resourceCollection = `{
556+
"hydra:search": {
557+
"hydra:mapping": [
558+
{
559+
"property": "id",
560+
"variable": "id"
561+
}
562+
]
563+
}
564+
}`;
565+
566+
const filters = [
567+
{
568+
property: "id",
569+
variable: "id"
570+
}
571+
];
572+
555573
const book = {
556574
name: "books",
557575
url: "http://localhost/books",
@@ -743,7 +761,8 @@ const book = {
743761
deprecated: false
744762
}
745763
],
746-
deprecated: false
764+
deprecated: false,
765+
filters: filters
747766
};
748767

749768
const review = {
@@ -886,7 +905,8 @@ const review = {
886905
deprecated: false
887906
}
888907
],
889-
deprecated: false
908+
deprecated: false,
909+
filters: filters
890910
};
891911

892912
const customResource = {
@@ -1012,7 +1032,8 @@ const customResource = {
10121032
deprecated: false
10131033
}
10141034
],
1015-
deprecated: false
1035+
deprecated: false,
1036+
filters: filters
10161037
};
10171038

10181039
const deprecatedResource = {
@@ -1072,13 +1093,16 @@ const deprecatedResource = {
10721093
deprecated: true
10731094
}
10741095
],
1075-
deprecated: true
1096+
deprecated: true,
1097+
filters: filters
10761098
};
10771099

1100+
const resources = [book, review, customResource, deprecatedResource];
1101+
10781102
const expectedApi = {
10791103
entrypoint: "http://localhost",
10801104
title: "API Platform's demo",
1081-
resources: [book, review, customResource, deprecatedResource]
1105+
resources: resources
10821106
};
10831107

10841108
const init = {
@@ -1091,30 +1115,45 @@ const init = {
10911115
})
10921116
};
10931117

1094-
test("parse a Hydra documentation", () => {
1095-
fetch.mockResponses([entrypoint, init], [docs, init]);
1118+
test("parse a Hydra documentation", async () => {
1119+
fetch.mockResponses(
1120+
[entrypoint, init],
1121+
[docs, init],
1122+
[resourceCollection, init],
1123+
[resourceCollection, init],
1124+
[resourceCollection, init],
1125+
[resourceCollection, init]
1126+
);
10961127

10971128
const options = { headers: new Headers({ CustomHeader: "customValue" }) };
10981129

1099-
return parseHydraDocumentation("http://localhost", options).then(data => {
1130+
await parseHydraDocumentation("http://localhost", options).then(data => {
11001131
expect(JSON.stringify(data.api, null, 2)).toBe(
11011132
JSON.stringify(expectedApi, null, 2)
11021133
);
11031134
expect(data.response).toBeDefined();
11041135
expect(data.status).toBe(200);
11051136

1106-
expect(fetch).toHaveBeenCalledTimes(2);
1107-
expect(fetch).toHaveBeenLastCalledWith(
1137+
expect(fetch).toHaveBeenCalledTimes(2 + resources.length);
1138+
expect(fetch).toHaveBeenNthCalledWith(
1139+
2,
11081140
"http://localhost/docs.jsonld",
11091141
options
11101142
);
11111143
});
11121144
});
11131145

1114-
test("parse a Hydra documentation (http://localhost/)", () => {
1115-
fetch.mockResponses([entrypoint, init], [docs, init]);
1146+
test("parse a Hydra documentation (http://localhost/)", async () => {
1147+
fetch.mockResponses(
1148+
[entrypoint, init],
1149+
[docs, init],
1150+
[resourceCollection, init],
1151+
[resourceCollection, init],
1152+
[resourceCollection, init],
1153+
[resourceCollection, init]
1154+
);
11161155

1117-
return parseHydraDocumentation("http://localhost/").then(data => {
1156+
await parseHydraDocumentation("http://localhost/").then(data => {
11181157
expect(JSON.stringify(data.api, null, 2)).toBe(
11191158
JSON.stringify(expectedApi, null, 2)
11201159
);

0 commit comments

Comments
 (0)