-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathApiSerializer.test.js
More file actions
144 lines (137 loc) · 4.3 KB
/
ApiSerializer.test.js
File metadata and controls
144 lines (137 loc) · 4.3 KB
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
import Api from "./Api";
import ApiSerializer from "./ApiSerializer";
import Resource from "./Resource";
import Field from "./Field";
import Operation from "./Operation";
import Parameter from "./Parameter";
test("can serialize and deserialize Api", () => {
const book = new Resource("Books", "http://localhost/books", {
id: "http://schema.org/Book",
title: "Book",
readableFields: [
new Field("isbn", {
id: "http://schema.org/isbn",
range: "http://www.w3.org/2001/XMLSchema#string",
reference: null,
required: true,
description: "The ISBN of the book",
maxCardinality: null,
deprecated: false
}),
new Field("name", {
id: "http://schema.org/name",
range: "http://www.w3.org/2001/XMLSchema#string",
reference: null,
required: true,
description: "The name of the item",
maxCardinality: null,
deprecated: false
})
],
writableFields: [
new Field("isbn", {
id: "http://schema.org/isbn",
range: "http://www.w3.org/2001/XMLSchema#string",
reference: null,
required: true,
description: "The ISBN of the book",
maxCardinality: null,
deprecated: false
}),
new Field("name", {
id: "http://schema.org/name",
range: "http://www.w3.org/2001/XMLSchema#string",
reference: null,
required: true,
description: "The name of the item",
maxCardinality: null,
deprecated: false
})
],
operations: [
new Operation("Retrieves Book resource.", {
method: "GET",
returns: "http://schema.org/Book",
types: ["http://www.w3.org/ns/hydra/core#Operation"],
deprecated: false
}),
new Operation("Replaces the Book resource.", {
method: "PUT",
expects: "http://schema.org/Book",
returns: "http://schema.org/Book",
types: ["http://www.w3.org/ns/hydra/core#ReplaceResourceOperation"],
deprecated: false
}),
new Operation("Deletes the Book resource.", {
method: "DELETE",
returns: "http://www.w3.org/2002/07/owl#Nothing",
types: ["http://www.w3.org/ns/hydra/core#Operation"],
deprecated: false
})
],
parameters: [
new Parameter(
"isbn",
"http://www.w3.org/2001/XMLSchema#string",
false,
""
)
]
});
const review = new Resource("Reviews", "http://localhost/reviews", {
id: "http://schema.org/Review",
title: "Book",
readableFields: [
new Field("reviewBody", {
id: "http://schema.org/reviewBody",
range: "http://www.w3.org/2001/XMLSchema#string",
reference: null,
required: false,
description: "The actual body of the review",
maxCardinality: null,
deprecated: false
}),
new Field("itemReviewed", {
id: "http://schema.org/itemReviewed",
range: "http://schema.org/Book",
reference: book,
required: true,
description: "The name of the item",
maxCardinality: null,
deprecated: false
})
],
writableFields: [
new Field("reviewBody", {
id: "http://schema.org/reviewBody",
range: "http://www.w3.org/2001/XMLSchema#string",
reference: null,
required: true,
description: "The actual body of the review",
maxCardinality: null,
deprecated: false
}),
new Field("itemReviewed", {
id: "http://schema.org/itemReviewed",
range: "http://schema.org/Book",
reference: book,
required: true,
description: "The item that is being reviewed/rated",
maxCardinality: null,
deprecated: false
})
]
});
const api = new Api("http://localhost", {
title: "API Platform's demo",
resources: [book, review]
});
const serializer = new ApiSerializer();
const serialized = serializer.serialize(api);
// verify that the serialized and stringified versions don't differ
// this ensures that the serializer does not return stuff that cannot be
// expressed in json-string notation properly
expect(JSON.parse(JSON.stringify(serialized))).toEqual(serialized);
const deserialized = serializer.deserialize(serialized);
expect(deserialized).toEqual(api);
});