/
index.test.ts
265 lines (253 loc) · 8.21 KB
/
index.test.ts
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
import axios from "axios"
import { beforeAll, describe, expect, test } from "vitest"
import { from } from "./index.js"
const OCA_REPO: string | undefined = process.env.OCA_REPO
if (!OCA_REPO) {
throw new Error("OCA_REPO env variable is not set")
}
describe("#from", () => {
describe("when 'ns' is used", () => {
let bundleResp = null
const ocafile = `ADD ATTRIBUTE d=Text i=Text passed=Boolean nice_attr=Text
ADD META en PROPS name="Entrance credential" description="Entrance credential"
ADD META pl PROPS name="Kredenszjal wejściowy" description="Kredenszjal wejściowy"
ADD INFORMATION en ATTRS \
d="Schema digest" \
i="Credential Issuee" \
passed="Enables or disables passing"\
nice_attr="nice placeholder"
ADD LABEL en ATTRS\
d="Schema digest"\
i="Credential Issuee"\
passed="Passed"\
nice_attr="Nice attribute"
ADD LABEL pl ATTRS\
d="trawić schemat"\
i="Kredenszjal wystawiacz"\
passed="Zaliczony"\
nice_attr="Ładny atrybut"
`
beforeAll(async () => {
bundleResp = await axios.post(`${OCA_REPO}/api/oca-bundles`, ocafile)
expect(bundleResp.data).toHaveProperty("said")
})
test("creates object graph", async () => {
const bundle = await axios.get(`${OCA_REPO}/api/oca-bundles/${bundleResp.data.said}?w=true`)
const pres = {
v: "1.0.0",
bd: bundleResp.data.said,
l: ["eng", "ita"],
d: "EK0T5StXlcYwIhfp_wJxhIpYwYpEnMhHwnKbHnodhxFU",
p: [
{
ns: "page 2",
ao: ["i"],
},
{
ns: "page 1",
ao: ["d"],
},
],
po: ["page 1", "page 2"],
pl: {
eng: {
"page 1": "First page",
"page 2": "Second page",
},
ita: {
"page 1": "Prima pagina",
"page 2": "Seconda pagina",
},
},
i: [],
}
const result = await from(bundle.data, pres, {})
console.dir(result, { depth: null })
})
})
describe("with references", () => {
const ocafile2 = `ADD ATTRIBUTE street=Text zip=Text city=Text
ADD META en PROPS name="Address" description="Address schema"
ADD META pl PROPS name="Adres" description="Schema adresu"
ADD CONFORMANCE ATTRS city=M street=M
ADD LABEL en ATTRS\
street="Street"\
zip="Zip code"\
city="City"
ADD LABEL pl ATTRS\
street="Ulica"\
zip="Kod pocztowy"\
city="Miasto"
ADD ATTRIBUTE is_nice=Boolean
ADD LABEL en ATTRS is_nice="Is nice"
ADD LABEL pl ATTRS is_nice="Czy ładny"
ADD CONFORMANCE ATTRS is_nice=M
`
const ocafile1 = (
addrSAID: string,
) => `ADD ATTRIBUTE d=Text i=Text passed=Boolean nice_attr=Text
ADD META en PROPS name="Entrance credential" description="Entrance credential"
ADD META pl PROPS name="Kredenszjal wejściowy" description="Kredenszjal wejściowy"
ADD INFORMATION en ATTRS \
d="Schema digest" \
i="Credential Issuee" \
passed="Enables or disables passing"\
nice_attr="nice placeholder"
ADD LABEL en ATTRS\
d="Schema digest"\
i="Credential Issuee"\
passed="Passed"\
nice_attr="Nice attribute"
ADD LABEL pl ATTRS\
d="trawić schemat"\
i="Kredenszjal wystawiacz"\
passed="Zaliczony"\
nice_attr="Ładny atrybut"
ADD ATTRIBUTE address=refs:${addrSAID}
ADD LABEL en ATTRS address="Address"
ADD LABEL pl ATTRS address="Adres"
`
let bundleSAID = null
beforeAll(async () => {
let addrResp = await axios.post(`${OCA_REPO}/api/oca-bundles`, ocafile2)
expect(addrResp.data).toHaveProperty("said")
let bundleResp = await axios.post(`${OCA_REPO}/api/oca-bundles`, ocafile1(addrResp.data.said))
expect(bundleResp.data).toHaveProperty("said")
bundleSAID = bundleResp.data.said
})
describe("when deprecated 'n' is used", () => {
test("creates object graph", async () => {
const pres = {
v: "1.0.0",
bd: bundleSAID,
l: ["eng", "ita"],
d: "EK0T5StXlcYwIhfp_wJxhIpYwYpEnMhHwnKbHnodhxFU",
p: [
{
n: "page 2",
ao: ["i", { n: "address", ao: ["city", "street"] }],
},
{
n: "page 1",
ao: ["d"],
},
],
po: ["page 1", "page 2"],
pl: {
eng: {
"page 1": "First page",
"page 2": "Second page",
},
ita: {
"page 1": "Prima pagina",
"page 2": "Seconda pagina",
},
},
i: [],
}
const bundle = await axios.get(`${OCA_REPO}/api/oca-bundles/${bundleSAID}?w=true`)
const result = await from(bundle.data, pres, {})
expect(result.form.pages).toHaveLength(2)
expect(result.form.pages[0].title).toBe("page 1")
expect(result.form.pages[1].title).toBe("page 2")
expect(result.form.pages[1].fields.map((el) => el.name)).toEqual(["i", "address"])
expect(result.form.pages[1].fields[1].fields).toBeInstanceOf(Array)
expect(result.i18n.locales.eng.p["page.address.title"]).toBe("Address")
console.dir(result, { depth: null })
})
})
describe("when 'nr' and 'ns' are used", () => {
test("creates object graph", async () => {
const pres = {
v: "1.0.0",
bd: bundleSAID,
l: ["eng", "ita"],
d: "EK0T5StXlcYwIhfp_wJxhIpYwYpEnMhHwnKbHnodhxFU",
p: [
{
ns: "page 2",
ao: [
"i",
{ nr: "address", ao: ["city", "street"] },
{ ns: "nice embedding section", ao: ["nice_attr"] },
],
},
{
n: "page 1",
ao: ["d"],
},
],
po: ["page 1", "page 2"],
pl: {
eng: {
"page 1": "First page",
"page 2": "Second page",
"nice embedding section": "Nice embedding section",
},
ita: {
"page 1": "Prima pagina",
"page 2": "Seconda pagina",
"nice embedding section": "Sezione di incorporamento piacevole",
},
},
i: [],
}
const bundle = await axios.get(`${OCA_REPO}/api/oca-bundles/${bundleSAID}?w=true`)
const result = await from(bundle.data, pres, {})
expect(result.form.pages[1].title).toBe("page 2")
expect(result.form.pages[1].fields.map((el) => el.name)).toEqual([
"i",
"address",
"nice embedding section",
])
expect(result.form.pages[1].fields[1].fields).toBeInstanceOf(Array)
expect(result.i18n.locales.eng.p["page.address.title"]).toBe("Address")
console.dir(result, { depth: null })
})
})
})
describe("i a", () => {
describe("select variants", () => {
const ocafile = `ADD ATTRIBUTE multi=Array[Text]
ADD ENTRY_CODE ATTRS multi=["o1", "o2", "o3", "o4", "o5", "o6", "o7"]
ADD ENTRY en ATTRS multi={"o1": "One", "o2": "Two", "o3": "Three", "o4": "Four", "o5": "Five", "o6": "Six", "o7": "Seven"}
ADD LABEL en ATTRS multi="Multi"
`
let bundleSAID = null
beforeAll(async () => {
let bundleResp = await axios.post(`${OCA_REPO}/api/oca-bundles`, ocafile)
expect(bundleResp.data).toHaveProperty("said")
bundleSAID = bundleResp.data.said
})
test("creates object graph", async () => {
const bundle = await axios.get(`${OCA_REPO}/api/oca-bundles/${bundleSAID}?w=true`)
const pres = {
v: "1.0.0",
bd: bundleSAID,
l: ["eng"],
d: "EK0T5StXlcYwIhfp_wJxhIpYwYpEnMhHwnKbHnodhxFU",
p: [{ ns: "page 1", ao: ["multi"] }],
po: ["page 1"],
pl: {
eng: {
"page 1": "First page",
},
},
i: [
{
m: "web", // interaction method
c: "capture", // context
a: {
multi: { t: "select", va: "multiple" },
},
},
],
}
const result = await from(bundle.data, pres, {})
console.dir(result, { depth: null })
expect(result.form.pages[0].fields[0].field.type).toBe("choice")
expect(result.form.pages[0].fields[0].field.display.type).toBe("select-multiple")
})
})
})
})