-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_js.test.js
More file actions
178 lines (165 loc) · 6.12 KB
/
Copy pathapp_js.test.js
File metadata and controls
178 lines (165 loc) · 6.12 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
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
"use strict";
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const test = require("node:test");
const vm = require("node:vm");
test("map initialization creates the center, radius and OSM layer without console errors", () => {
const source = fs.readFileSync(
path.join(__dirname, "..", "hungrycall", "static", "app.js"),
"utf8"
);
const calls = [];
const map = {
setView(center, zoom) { calls.push(["setView", center, zoom]); return this; },
remove() { calls.push(["remove"]); }
};
const addable = (kind) => ({
addTo(target) { calls.push([kind, "addTo", target === map]); return this; },
bindPopup() { return this; }
});
const document = {
readyState: "complete",
documentElement: { dataset: {} },
getElementById(id) { return id === "map" ? {} : null; },
createElement() { return {}; },
addEventListener() {}
};
const context = {
window: {},
document,
localStorage: { setItem() {} },
console,
setTimeout,
clearTimeout,
L: {
map(id) { calls.push(["map", id]); return map; },
tileLayer(url, options) { calls.push(["tile", url, options.attribution]); return addable("tile"); },
circleMarker(center, options) { calls.push(["center", center, options.radius]); return addable("center"); },
circle(center, options) { calls.push(["circle", center, options.radius]); return addable("circle"); },
divIcon(options) { return options; },
marker() { return addable("marker"); }
}
};
context.window.document = document;
vm.runInNewContext(source, context, { filename: "app.js" });
context.window.HC.initMap(52.6908773, 13.5823608, 4.5, []);
const plainCalls = JSON.parse(JSON.stringify(calls));
assert.deepEqual(plainCalls.find((call) => call[0] === "setView"), [
"setView", [52.6908773, 13.5823608], 13
]);
assert.deepEqual(plainCalls.find((call) => call[0] === "circle"), [
"circle", [52.6908773, 13.5823608], 4500
]);
assert.ok(calls.some((call) => call[0] === "tile"));
});
test("an interactive document initializes visible order inputs before the add button is used", () => {
const source = fs.readFileSync(
path.join(__dirname, "..", "hungrycall", "static", "app.js"),
"utf8"
);
const byId = {};
function element(tag) {
let text = "";
const node = {
tag,
children: [],
style: {},
dataset: {},
appendChild(child) { this.children.push(child); return child; },
addEventListener() {},
setAttribute() {},
focus() { this.focused = true; }
};
Object.defineProperty(node, "textContent", {
get() { return text; },
set(value) { text = String(value); this.children = []; }
});
return node;
}
const root = element("div");
const walk = (node, found, className) => {
if (node.tag === "input" && node.type === "text" && (!className || node.className === className)) found.push(node);
(node.children || []).forEach((child) => walk(child, found, className));
return found;
};
byId["order-chain-builder"] = root;
byId.order_chain_json = element("input");
byId.food_prompt = element("input");
const document = {
readyState: "interactive",
documentElement: { dataset: {} },
getElementById(id) { return byId[id] || null; },
querySelector() { return null; },
querySelectorAll(selector) { return walk(root, [], selector === ".order-product-input" ? "order-product-input" : undefined); },
createElement(tag) { return element(tag); },
addEventListener() {}
};
const context = {
window: { HC: {
text: {
position: "Position", wish: "Wish", replacement: "Replacement", quantity: "Quantity",
product: "Product", tags: "Tags",
criteria: "Criteria", remove: "Remove", addReplacement: "Replacement",
ruleSkip: "Skip", ruleAbort: "Abort", budgetDelivery: "Budget", budgetPickup: "Budget",
addressDelivery: "Address", addressPickup: "Address"
},
orderChainInitial: {
version: 1,
posten: [{
zellen: [{ menge: 1, produkt: "Burger", art: "essen", kriterien: [] }],
tags: [], wenn_nichts_verfuegbar: "posten_weglassen"
}]
}
} }, document, localStorage: { setItem() {} }, console, setTimeout, clearTimeout
};
context.window.document = document;
vm.runInNewContext(source, context, { filename: "app.js" });
const initialProducts = walk(root, [], "order-product-input");
assert.equal(initialProducts.length, 1);
assert.equal(initialProducts[0].value, "Burger");
context.window.HC.addPosition();
assert.equal(context.window.HC.orderChain.posten.length, 2);
assert.equal(context.window.HC.orderChain.posten[1].zellen[0].produkt, "");
const products = walk(root, [], "order-product-input");
assert.equal(products.length, 2);
assert.equal(products[1].focused, true);
});
test("custom seating input is enabled and required only for the custom choice", () => {
const source = fs.readFileSync(
path.join(__dirname, "..", "hungrycall", "static", "app.js"),
"utf8"
);
const seating = { value: "any" };
const field = { hidden: false };
const input = { disabled: false, required: true, value: "stale text" };
const byId = {
seating,
"seating-custom-field": field,
seating_custom: input
};
const document = {
readyState: "complete",
documentElement: { dataset: {} },
getElementById(id) { return byId[id] || null; },
querySelector() { return null; },
querySelectorAll() { return []; },
createElement() { return {}; },
addEventListener() {}
};
const context = {
window: {}, document, localStorage: { setItem() {} }, console, setTimeout, clearTimeout
};
context.window.document = document;
vm.runInNewContext(source, context, { filename: "app.js" });
context.window.HC.onSeatingChange();
assert.equal(field.hidden, true);
assert.equal(input.disabled, true);
assert.equal(input.required, false);
assert.equal(input.value, "");
seating.value = "custom";
context.window.HC.onSeatingChange();
assert.equal(field.hidden, false);
assert.equal(input.disabled, false);
assert.equal(input.required, true);
});