@egen-civitas/esm-nextjs-utils 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +13 -0
- package/dist/client.d.ts +7 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +32 -1
- package/dist/host.d.ts +6 -0
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +131 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/navigation-sync.test.js +931 -0
- package/dist/navigation.d.ts +51 -0
- package/dist/navigation.d.ts.map +1 -0
- package/dist/navigation.js +153 -0
- package/dist/navigation.test.js +101 -0
- package/dist/nextjs-utils.test.js +5 -1
- package/dist/protocol.d.ts +25 -0
- package/dist/protocol.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +15 -1
- package/src/host.ts +72 -1
- package/src/index.ts +1 -0
- package/src/navigation.ts +163 -0
- package/src/protocol.ts +14 -3
|
@@ -0,0 +1,931 @@
|
|
|
1
|
+
function _array_like_to_array(arr, len) {
|
|
2
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
3
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
|
+
return arr2;
|
|
5
|
+
}
|
|
6
|
+
function _array_with_holes(arr) {
|
|
7
|
+
if (Array.isArray(arr)) return arr;
|
|
8
|
+
}
|
|
9
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
10
|
+
try {
|
|
11
|
+
var info = gen[key](arg);
|
|
12
|
+
var value = info.value;
|
|
13
|
+
} catch (error) {
|
|
14
|
+
reject(error);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (info.done) {
|
|
18
|
+
resolve(value);
|
|
19
|
+
} else {
|
|
20
|
+
Promise.resolve(value).then(_next, _throw);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function _async_to_generator(fn) {
|
|
24
|
+
return function() {
|
|
25
|
+
var self = this, args = arguments;
|
|
26
|
+
return new Promise(function(resolve, reject) {
|
|
27
|
+
var gen = fn.apply(self, args);
|
|
28
|
+
function _next(value) {
|
|
29
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
30
|
+
}
|
|
31
|
+
function _throw(err) {
|
|
32
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
33
|
+
}
|
|
34
|
+
_next(undefined);
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function _define_property(obj, key, value) {
|
|
39
|
+
if (key in obj) {
|
|
40
|
+
Object.defineProperty(obj, key, {
|
|
41
|
+
value: value,
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
writable: true
|
|
45
|
+
});
|
|
46
|
+
} else {
|
|
47
|
+
obj[key] = value;
|
|
48
|
+
}
|
|
49
|
+
return obj;
|
|
50
|
+
}
|
|
51
|
+
function _iterable_to_array_limit(arr, i) {
|
|
52
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
53
|
+
if (_i == null) return;
|
|
54
|
+
var _arr = [];
|
|
55
|
+
var _n = true;
|
|
56
|
+
var _d = false;
|
|
57
|
+
var _s, _e;
|
|
58
|
+
try {
|
|
59
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
60
|
+
_arr.push(_s.value);
|
|
61
|
+
if (i && _arr.length === i) break;
|
|
62
|
+
}
|
|
63
|
+
} catch (err) {
|
|
64
|
+
_d = true;
|
|
65
|
+
_e = err;
|
|
66
|
+
} finally{
|
|
67
|
+
try {
|
|
68
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
69
|
+
} finally{
|
|
70
|
+
if (_d) throw _e;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return _arr;
|
|
74
|
+
}
|
|
75
|
+
function _non_iterable_rest() {
|
|
76
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
77
|
+
}
|
|
78
|
+
function _object_spread(target) {
|
|
79
|
+
for(var i = 1; i < arguments.length; i++){
|
|
80
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
81
|
+
var ownKeys = Object.keys(source);
|
|
82
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
83
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
84
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
87
|
+
ownKeys.forEach(function(key) {
|
|
88
|
+
_define_property(target, key, source[key]);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return target;
|
|
92
|
+
}
|
|
93
|
+
function ownKeys(object, enumerableOnly) {
|
|
94
|
+
var keys = Object.keys(object);
|
|
95
|
+
if (Object.getOwnPropertySymbols) {
|
|
96
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
97
|
+
if (enumerableOnly) {
|
|
98
|
+
symbols = symbols.filter(function(sym) {
|
|
99
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
keys.push.apply(keys, symbols);
|
|
103
|
+
}
|
|
104
|
+
return keys;
|
|
105
|
+
}
|
|
106
|
+
function _object_spread_props(target, source) {
|
|
107
|
+
source = source != null ? source : {};
|
|
108
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
109
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
110
|
+
} else {
|
|
111
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
112
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return target;
|
|
116
|
+
}
|
|
117
|
+
function _sliced_to_array(arr, i) {
|
|
118
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
119
|
+
}
|
|
120
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
121
|
+
if (!o) return;
|
|
122
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
123
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
124
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
125
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
126
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
127
|
+
}
|
|
128
|
+
function _ts_generator(thisArg, body) {
|
|
129
|
+
var f, y, t, _ = {
|
|
130
|
+
label: 0,
|
|
131
|
+
sent: function() {
|
|
132
|
+
if (t[0] & 1) throw t[1];
|
|
133
|
+
return t[1];
|
|
134
|
+
},
|
|
135
|
+
trys: [],
|
|
136
|
+
ops: []
|
|
137
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
|
|
138
|
+
return d(g, "next", {
|
|
139
|
+
value: verb(0)
|
|
140
|
+
}), d(g, "throw", {
|
|
141
|
+
value: verb(1)
|
|
142
|
+
}), d(g, "return", {
|
|
143
|
+
value: verb(2)
|
|
144
|
+
}), typeof Symbol === "function" && d(g, Symbol.iterator, {
|
|
145
|
+
value: function() {
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
}), g;
|
|
149
|
+
function verb(n) {
|
|
150
|
+
return function(v) {
|
|
151
|
+
return step([
|
|
152
|
+
n,
|
|
153
|
+
v
|
|
154
|
+
]);
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function step(op) {
|
|
158
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
159
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
160
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
161
|
+
if (y = 0, t) op = [
|
|
162
|
+
op[0] & 2,
|
|
163
|
+
t.value
|
|
164
|
+
];
|
|
165
|
+
switch(op[0]){
|
|
166
|
+
case 0:
|
|
167
|
+
case 1:
|
|
168
|
+
t = op;
|
|
169
|
+
break;
|
|
170
|
+
case 4:
|
|
171
|
+
_.label++;
|
|
172
|
+
return {
|
|
173
|
+
value: op[1],
|
|
174
|
+
done: false
|
|
175
|
+
};
|
|
176
|
+
case 5:
|
|
177
|
+
_.label++;
|
|
178
|
+
y = op[1];
|
|
179
|
+
op = [
|
|
180
|
+
0
|
|
181
|
+
];
|
|
182
|
+
continue;
|
|
183
|
+
case 7:
|
|
184
|
+
op = _.ops.pop();
|
|
185
|
+
_.trys.pop();
|
|
186
|
+
continue;
|
|
187
|
+
default:
|
|
188
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
189
|
+
_ = 0;
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
193
|
+
_.label = op[1];
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
197
|
+
_.label = t[1];
|
|
198
|
+
t = op;
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
if (t && _.label < t[2]) {
|
|
202
|
+
_.label = t[2];
|
|
203
|
+
_.ops.push(op);
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
if (t[2]) _.ops.pop();
|
|
207
|
+
_.trys.pop();
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
op = body.call(thisArg, _);
|
|
211
|
+
} catch (e) {
|
|
212
|
+
op = [
|
|
213
|
+
6,
|
|
214
|
+
e
|
|
215
|
+
];
|
|
216
|
+
y = 0;
|
|
217
|
+
} finally{
|
|
218
|
+
f = t = 0;
|
|
219
|
+
}
|
|
220
|
+
if (op[0] & 5) throw op[1];
|
|
221
|
+
return {
|
|
222
|
+
value: op[0] ? op[1] : void 0,
|
|
223
|
+
done: true
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
import { readFileSync } from 'node:fs';
|
|
228
|
+
import { resolve } from 'node:path';
|
|
229
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
230
|
+
import { createStore } from 'zustand/vanilla';
|
|
231
|
+
var m = vi.hoisted(function() {
|
|
232
|
+
return {
|
|
233
|
+
session: undefined
|
|
234
|
+
};
|
|
235
|
+
});
|
|
236
|
+
vi.mock('@egen-civitas/esm-api', function() {
|
|
237
|
+
return {
|
|
238
|
+
getSessionStore: function getSessionStore() {
|
|
239
|
+
return m.session;
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
});
|
|
243
|
+
vi.mock('@egen-civitas/esm-config', function() {
|
|
244
|
+
return {};
|
|
245
|
+
});
|
|
246
|
+
vi.mock('@egen-civitas/esm-extensions', function() {
|
|
247
|
+
return {};
|
|
248
|
+
});
|
|
249
|
+
vi.mock('@egen-civitas/esm-feature-flags', function() {
|
|
250
|
+
return {};
|
|
251
|
+
});
|
|
252
|
+
import { getEmbedLifecycle } from './index.js';
|
|
253
|
+
var ORIGIN = 'https://next.exemple.org';
|
|
254
|
+
var base = {
|
|
255
|
+
moduleName: '@acme/next-app',
|
|
256
|
+
featureName: 'next',
|
|
257
|
+
url: "".concat(ORIGIN, "/app"),
|
|
258
|
+
minHeight: 300
|
|
259
|
+
};
|
|
260
|
+
var nav = {
|
|
261
|
+
route: 'next',
|
|
262
|
+
mode: 'history'
|
|
263
|
+
};
|
|
264
|
+
var sent = function sent(win) {
|
|
265
|
+
return win.postMessage.mock.calls.map(function(c) {
|
|
266
|
+
return c[0].message;
|
|
267
|
+
});
|
|
268
|
+
};
|
|
269
|
+
var post = function post(data, source) {
|
|
270
|
+
var origin = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : ORIGIN;
|
|
271
|
+
return window.dispatchEvent(new MessageEvent('message', {
|
|
272
|
+
data: data,
|
|
273
|
+
origin: origin,
|
|
274
|
+
source: source
|
|
275
|
+
}));
|
|
276
|
+
};
|
|
277
|
+
var env = function env(message) {
|
|
278
|
+
return {
|
|
279
|
+
egen: 1,
|
|
280
|
+
moduleName: base.moduleName,
|
|
281
|
+
message: message
|
|
282
|
+
};
|
|
283
|
+
};
|
|
284
|
+
var coreAt = function coreAt(url) {
|
|
285
|
+
return window.history.replaceState(null, '', url);
|
|
286
|
+
};
|
|
287
|
+
beforeEach(function() {
|
|
288
|
+
vi.spyOn(console, 'error').mockImplementation(function() {});
|
|
289
|
+
m.session = createStore(function() {
|
|
290
|
+
return {
|
|
291
|
+
loaded: false,
|
|
292
|
+
session: null
|
|
293
|
+
};
|
|
294
|
+
});
|
|
295
|
+
window.getEgenSpaBase = function() {
|
|
296
|
+
return '/egen/spa/';
|
|
297
|
+
};
|
|
298
|
+
window.i18next = {
|
|
299
|
+
language: 'fr',
|
|
300
|
+
on: vi.fn(),
|
|
301
|
+
off: vi.fn()
|
|
302
|
+
};
|
|
303
|
+
window.singleSpaNavigate = vi.fn();
|
|
304
|
+
coreAt('/egen/spa/next');
|
|
305
|
+
});
|
|
306
|
+
afterEach(function() {
|
|
307
|
+
document.body.innerHTML = '';
|
|
308
|
+
vi.restoreAllMocks();
|
|
309
|
+
});
|
|
310
|
+
var counter = 0;
|
|
311
|
+
function mount() {
|
|
312
|
+
return _async_to_generator(function() {
|
|
313
|
+
var extra, name, lc, el, props, iframe, win;
|
|
314
|
+
var _arguments = arguments;
|
|
315
|
+
return _ts_generator(this, function(_state) {
|
|
316
|
+
switch(_state.label){
|
|
317
|
+
case 0:
|
|
318
|
+
extra = _arguments.length > 0 && _arguments[0] !== void 0 ? _arguments[0] : {};
|
|
319
|
+
name = "n-page-".concat(counter++);
|
|
320
|
+
lc = getEmbedLifecycle(_object_spread({}, base, extra));
|
|
321
|
+
el = document.createElement('div');
|
|
322
|
+
el.id = "single-spa-application:".concat(name);
|
|
323
|
+
document.body.appendChild(el);
|
|
324
|
+
props = {
|
|
325
|
+
name: name
|
|
326
|
+
};
|
|
327
|
+
return [
|
|
328
|
+
4,
|
|
329
|
+
lc.bootstrap(props)
|
|
330
|
+
];
|
|
331
|
+
case 1:
|
|
332
|
+
_state.sent();
|
|
333
|
+
return [
|
|
334
|
+
4,
|
|
335
|
+
lc.mount(props)
|
|
336
|
+
];
|
|
337
|
+
case 2:
|
|
338
|
+
_state.sent();
|
|
339
|
+
iframe = el.querySelector('iframe');
|
|
340
|
+
win = {
|
|
341
|
+
postMessage: vi.fn()
|
|
342
|
+
};
|
|
343
|
+
Object.defineProperty(iframe, 'contentWindow', {
|
|
344
|
+
value: win,
|
|
345
|
+
configurable: true
|
|
346
|
+
});
|
|
347
|
+
return [
|
|
348
|
+
2,
|
|
349
|
+
{
|
|
350
|
+
lc: lc,
|
|
351
|
+
iframe: iframe,
|
|
352
|
+
win: win,
|
|
353
|
+
props: props
|
|
354
|
+
}
|
|
355
|
+
];
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
}).apply(this, arguments);
|
|
359
|
+
}
|
|
360
|
+
describe('Shell : navigation partagée', function() {
|
|
361
|
+
it('actualisation : le chemin du Core devient celui de l\'iframe (paramètres sensibles retirés)', function() {
|
|
362
|
+
return _async_to_generator(function() {
|
|
363
|
+
var iframe;
|
|
364
|
+
return _ts_generator(this, function(_state) {
|
|
365
|
+
switch(_state.label){
|
|
366
|
+
case 0:
|
|
367
|
+
coreAt('/egen/spa/next/dash/x?team=1&token=SECRET#frag');
|
|
368
|
+
return [
|
|
369
|
+
4,
|
|
370
|
+
mount({
|
|
371
|
+
navigation: nav
|
|
372
|
+
})
|
|
373
|
+
];
|
|
374
|
+
case 1:
|
|
375
|
+
iframe = _state.sent().iframe;
|
|
376
|
+
expect(iframe.src).toBe("".concat(ORIGIN, "/app/dash/x?team=1#frag"));
|
|
377
|
+
return [
|
|
378
|
+
2
|
|
379
|
+
];
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
})();
|
|
383
|
+
});
|
|
384
|
+
it('racine du Core → racine de l\'application ; sans option → comportement d\'origine', function() {
|
|
385
|
+
return _async_to_generator(function() {
|
|
386
|
+
return _ts_generator(this, function(_state) {
|
|
387
|
+
switch(_state.label){
|
|
388
|
+
case 0:
|
|
389
|
+
return [
|
|
390
|
+
4,
|
|
391
|
+
mount({
|
|
392
|
+
navigation: nav
|
|
393
|
+
})
|
|
394
|
+
];
|
|
395
|
+
case 1:
|
|
396
|
+
expect.apply(void 0, [
|
|
397
|
+
_state.sent().iframe.src
|
|
398
|
+
]).toBe("".concat(ORIGIN, "/app"));
|
|
399
|
+
coreAt('/egen/spa/next/dash');
|
|
400
|
+
return [
|
|
401
|
+
4,
|
|
402
|
+
mount()
|
|
403
|
+
];
|
|
404
|
+
case 2:
|
|
405
|
+
expect.apply(void 0, [
|
|
406
|
+
_state.sent().iframe.src
|
|
407
|
+
]).toBe("".concat(ORIGIN, "/app"));
|
|
408
|
+
return [
|
|
409
|
+
4,
|
|
410
|
+
mount({
|
|
411
|
+
navigation: _object_spread_props(_object_spread({}, nav), {
|
|
412
|
+
mode: 'none'
|
|
413
|
+
})
|
|
414
|
+
})
|
|
415
|
+
];
|
|
416
|
+
case 3:
|
|
417
|
+
expect.apply(void 0, [
|
|
418
|
+
_state.sent().iframe.src
|
|
419
|
+
]).toBe("".concat(ORIGIN, "/app"));
|
|
420
|
+
return [
|
|
421
|
+
2
|
|
422
|
+
];
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
})();
|
|
426
|
+
});
|
|
427
|
+
it('mode hash', function() {
|
|
428
|
+
return _async_to_generator(function() {
|
|
429
|
+
var iframe;
|
|
430
|
+
return _ts_generator(this, function(_state) {
|
|
431
|
+
switch(_state.label){
|
|
432
|
+
case 0:
|
|
433
|
+
coreAt('/egen/spa/next/dashboard/recent?team-id=1');
|
|
434
|
+
return [
|
|
435
|
+
4,
|
|
436
|
+
mount({
|
|
437
|
+
navigation: _object_spread_props(_object_spread({}, nav), {
|
|
438
|
+
mode: 'hash'
|
|
439
|
+
})
|
|
440
|
+
})
|
|
441
|
+
];
|
|
442
|
+
case 1:
|
|
443
|
+
iframe = _state.sent().iframe;
|
|
444
|
+
expect(iframe.src).toBe("".concat(ORIGIN, "/app#/dashboard/recent?team-id=1"));
|
|
445
|
+
return [
|
|
446
|
+
2
|
|
447
|
+
];
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
})();
|
|
451
|
+
});
|
|
452
|
+
it('location : met à jour l\'URL du Core (pushState, ou replaceState si demandé)', function() {
|
|
453
|
+
return _async_to_generator(function() {
|
|
454
|
+
var win, push, replace;
|
|
455
|
+
return _ts_generator(this, function(_state) {
|
|
456
|
+
switch(_state.label){
|
|
457
|
+
case 0:
|
|
458
|
+
return [
|
|
459
|
+
4,
|
|
460
|
+
mount({
|
|
461
|
+
navigation: nav
|
|
462
|
+
})
|
|
463
|
+
];
|
|
464
|
+
case 1:
|
|
465
|
+
win = _state.sent().win;
|
|
466
|
+
push = vi.spyOn(window.history, 'pushState');
|
|
467
|
+
replace = vi.spyOn(window.history, 'replaceState');
|
|
468
|
+
post(env({
|
|
469
|
+
kind: 'location',
|
|
470
|
+
path: '/dash/x?team=1&token=S'
|
|
471
|
+
}), win);
|
|
472
|
+
expect(window.location.pathname + window.location.search).toBe('/egen/spa/next/dash/x?team=1');
|
|
473
|
+
expect(push).toHaveBeenCalledTimes(1);
|
|
474
|
+
post(env({
|
|
475
|
+
kind: 'location',
|
|
476
|
+
path: '/dash/y',
|
|
477
|
+
replace: true
|
|
478
|
+
}), win);
|
|
479
|
+
expect(window.location.pathname).toBe('/egen/spa/next/dash/y');
|
|
480
|
+
expect(replace).toHaveBeenCalledTimes(1);
|
|
481
|
+
post(env({
|
|
482
|
+
kind: 'location',
|
|
483
|
+
path: '/'
|
|
484
|
+
}), win);
|
|
485
|
+
expect(window.location.pathname).toBe('/egen/spa/next');
|
|
486
|
+
return [
|
|
487
|
+
2
|
|
488
|
+
];
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
})();
|
|
492
|
+
});
|
|
493
|
+
it('location : refuse chemins dangereux, mauvaise origine/source, sans option de navigation', function() {
|
|
494
|
+
return _async_to_generator(function() {
|
|
495
|
+
var win, _i, _iter, path, plain;
|
|
496
|
+
return _ts_generator(this, function(_state) {
|
|
497
|
+
switch(_state.label){
|
|
498
|
+
case 0:
|
|
499
|
+
return [
|
|
500
|
+
4,
|
|
501
|
+
mount({
|
|
502
|
+
navigation: nav
|
|
503
|
+
})
|
|
504
|
+
];
|
|
505
|
+
case 1:
|
|
506
|
+
win = _state.sent().win;
|
|
507
|
+
for(_i = 0, _iter = [
|
|
508
|
+
'//evil.org',
|
|
509
|
+
'https://evil.org',
|
|
510
|
+
'x',
|
|
511
|
+
42
|
|
512
|
+
]; _i < _iter.length; _i++){
|
|
513
|
+
path = _iter[_i];
|
|
514
|
+
post(env({
|
|
515
|
+
kind: 'location',
|
|
516
|
+
path: path
|
|
517
|
+
}), win);
|
|
518
|
+
}
|
|
519
|
+
post(env({
|
|
520
|
+
kind: 'location',
|
|
521
|
+
path: '/ok'
|
|
522
|
+
}), win, 'https://evil.org');
|
|
523
|
+
post(env({
|
|
524
|
+
kind: 'location',
|
|
525
|
+
path: '/ok'
|
|
526
|
+
}), {
|
|
527
|
+
postMessage: vi.fn()
|
|
528
|
+
});
|
|
529
|
+
expect(window.location.pathname).toBe('/egen/spa/next');
|
|
530
|
+
return [
|
|
531
|
+
4,
|
|
532
|
+
mount()
|
|
533
|
+
];
|
|
534
|
+
case 2:
|
|
535
|
+
plain = _state.sent();
|
|
536
|
+
post(env({
|
|
537
|
+
kind: 'location',
|
|
538
|
+
path: '/ok'
|
|
539
|
+
}), plain.win);
|
|
540
|
+
expect(window.location.pathname).toBe('/egen/spa/next');
|
|
541
|
+
return [
|
|
542
|
+
2
|
|
543
|
+
];
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
})();
|
|
547
|
+
});
|
|
548
|
+
it('pathMap : traduit dans les deux sens', function() {
|
|
549
|
+
return _async_to_generator(function() {
|
|
550
|
+
var pathMap, _ref, iframe, win;
|
|
551
|
+
return _ts_generator(this, function(_state) {
|
|
552
|
+
switch(_state.label){
|
|
553
|
+
case 0:
|
|
554
|
+
pathMap = {
|
|
555
|
+
toApp: function toApp(p) {
|
|
556
|
+
return p.replace(/^\/studio/, '/workspace');
|
|
557
|
+
},
|
|
558
|
+
toCore: function toCore(p) {
|
|
559
|
+
return p.replace(/^\/workspace/, '/studio');
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
coreAt('/egen/spa/next/studio/1');
|
|
563
|
+
return [
|
|
564
|
+
4,
|
|
565
|
+
mount({
|
|
566
|
+
navigation: _object_spread_props(_object_spread({}, nav), {
|
|
567
|
+
pathMap: pathMap
|
|
568
|
+
})
|
|
569
|
+
})
|
|
570
|
+
];
|
|
571
|
+
case 1:
|
|
572
|
+
_ref = _state.sent(), iframe = _ref.iframe, win = _ref.win;
|
|
573
|
+
expect(iframe.src).toBe("".concat(ORIGIN, "/app/workspace/1"));
|
|
574
|
+
post(env({
|
|
575
|
+
kind: 'location',
|
|
576
|
+
path: '/workspace/2'
|
|
577
|
+
}), win);
|
|
578
|
+
expect(window.location.pathname).toBe('/egen/spa/next/studio/2');
|
|
579
|
+
return [
|
|
580
|
+
2
|
|
581
|
+
];
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
})();
|
|
585
|
+
});
|
|
586
|
+
it('retour/avance du Core → goto vers l\'application ; pas de boucle sur ses propres changements', function() {
|
|
587
|
+
return _async_to_generator(function() {
|
|
588
|
+
var win, gotos;
|
|
589
|
+
return _ts_generator(this, function(_state) {
|
|
590
|
+
switch(_state.label){
|
|
591
|
+
case 0:
|
|
592
|
+
return [
|
|
593
|
+
4,
|
|
594
|
+
mount({
|
|
595
|
+
navigation: nav
|
|
596
|
+
})
|
|
597
|
+
];
|
|
598
|
+
case 1:
|
|
599
|
+
win = _state.sent().win;
|
|
600
|
+
post(env({
|
|
601
|
+
kind: 'ready'
|
|
602
|
+
}), win);
|
|
603
|
+
win.postMessage.mockClear();
|
|
604
|
+
post(env({
|
|
605
|
+
kind: 'location',
|
|
606
|
+
path: '/a'
|
|
607
|
+
}), win); // l'app navigue : le Core suit
|
|
608
|
+
window.dispatchEvent(new Event('single-spa:routing-event'));
|
|
609
|
+
expect(sent(win).filter(function(x) {
|
|
610
|
+
return x.kind === 'goto';
|
|
611
|
+
})).toHaveLength(0);
|
|
612
|
+
coreAt('/egen/spa/next/b?q=1'); // l'utilisateur change l'URL du Core
|
|
613
|
+
window.dispatchEvent(new PopStateEvent('popstate'));
|
|
614
|
+
window.dispatchEvent(new Event('single-spa:routing-event')); // dédoublonné
|
|
615
|
+
gotos = sent(win).filter(function(x) {
|
|
616
|
+
return x.kind === 'goto';
|
|
617
|
+
});
|
|
618
|
+
expect(gotos).toEqual([
|
|
619
|
+
{
|
|
620
|
+
kind: 'goto',
|
|
621
|
+
path: '/b?q=1',
|
|
622
|
+
seq: 1
|
|
623
|
+
}
|
|
624
|
+
]);
|
|
625
|
+
expect(win.postMessage.mock.calls.at(-1)[1]).toBe(ORIGIN);
|
|
626
|
+
return [
|
|
627
|
+
2
|
|
628
|
+
];
|
|
629
|
+
}
|
|
630
|
+
});
|
|
631
|
+
})();
|
|
632
|
+
});
|
|
633
|
+
it('application sans pont : le changement d\'URL du Core recharge l\'iframe au bon endroit', function() {
|
|
634
|
+
return _async_to_generator(function() {
|
|
635
|
+
var iframe;
|
|
636
|
+
return _ts_generator(this, function(_state) {
|
|
637
|
+
switch(_state.label){
|
|
638
|
+
case 0:
|
|
639
|
+
return [
|
|
640
|
+
4,
|
|
641
|
+
mount({
|
|
642
|
+
navigation: nav
|
|
643
|
+
})
|
|
644
|
+
];
|
|
645
|
+
case 1:
|
|
646
|
+
iframe = _state.sent().iframe;
|
|
647
|
+
coreAt('/egen/spa/next/c');
|
|
648
|
+
window.dispatchEvent(new Event('single-spa:routing-event'));
|
|
649
|
+
expect(iframe.src).toBe("".concat(ORIGIN, "/app/c"));
|
|
650
|
+
return [
|
|
651
|
+
2
|
|
652
|
+
];
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
})();
|
|
656
|
+
});
|
|
657
|
+
it('poignée de main : hello à chaque chargement ; init annonce la navigation ; démontage nettoie', function() {
|
|
658
|
+
return _async_to_generator(function() {
|
|
659
|
+
var _ref, iframe, win, lc, props;
|
|
660
|
+
return _ts_generator(this, function(_state) {
|
|
661
|
+
switch(_state.label){
|
|
662
|
+
case 0:
|
|
663
|
+
return [
|
|
664
|
+
4,
|
|
665
|
+
mount({
|
|
666
|
+
navigation: nav
|
|
667
|
+
})
|
|
668
|
+
];
|
|
669
|
+
case 1:
|
|
670
|
+
_ref = _state.sent(), iframe = _ref.iframe, win = _ref.win, lc = _ref.lc, props = _ref.props;
|
|
671
|
+
iframe.dispatchEvent(new Event('load'));
|
|
672
|
+
expect(sent(win)).toContainEqual({
|
|
673
|
+
kind: 'hello'
|
|
674
|
+
});
|
|
675
|
+
post(env({
|
|
676
|
+
kind: 'ready',
|
|
677
|
+
caps: [
|
|
678
|
+
'location',
|
|
679
|
+
'goto'
|
|
680
|
+
]
|
|
681
|
+
}), win);
|
|
682
|
+
expect(sent(win).find(function(x) {
|
|
683
|
+
return x.kind === 'init';
|
|
684
|
+
}).payload.navigation).toEqual({
|
|
685
|
+
mode: 'history',
|
|
686
|
+
route: 'next'
|
|
687
|
+
});
|
|
688
|
+
return [
|
|
689
|
+
4,
|
|
690
|
+
lc.unmount(props)
|
|
691
|
+
];
|
|
692
|
+
case 2:
|
|
693
|
+
_state.sent();
|
|
694
|
+
win.postMessage.mockClear();
|
|
695
|
+
coreAt('/egen/spa/next/zz');
|
|
696
|
+
window.dispatchEvent(new PopStateEvent('popstate'));
|
|
697
|
+
expect(win.postMessage).not.toHaveBeenCalled();
|
|
698
|
+
return [
|
|
699
|
+
2
|
|
700
|
+
];
|
|
701
|
+
}
|
|
702
|
+
});
|
|
703
|
+
})();
|
|
704
|
+
});
|
|
705
|
+
it('titre annoncé avec la position → onTitle (fil d\'Ariane)', function() {
|
|
706
|
+
return _async_to_generator(function() {
|
|
707
|
+
var onTitle, win;
|
|
708
|
+
return _ts_generator(this, function(_state) {
|
|
709
|
+
switch(_state.label){
|
|
710
|
+
case 0:
|
|
711
|
+
onTitle = vi.fn();
|
|
712
|
+
return [
|
|
713
|
+
4,
|
|
714
|
+
mount({
|
|
715
|
+
navigation: _object_spread_props(_object_spread({}, nav), {
|
|
716
|
+
onTitle: onTitle
|
|
717
|
+
})
|
|
718
|
+
})
|
|
719
|
+
];
|
|
720
|
+
case 1:
|
|
721
|
+
win = _state.sent().win;
|
|
722
|
+
post(env({
|
|
723
|
+
kind: 'location',
|
|
724
|
+
path: '/t',
|
|
725
|
+
title: 'Mes thèmes'
|
|
726
|
+
}), win);
|
|
727
|
+
expect(onTitle).toHaveBeenCalledWith('Mes thèmes');
|
|
728
|
+
return [
|
|
729
|
+
2
|
|
730
|
+
];
|
|
731
|
+
}
|
|
732
|
+
});
|
|
733
|
+
})();
|
|
734
|
+
});
|
|
735
|
+
});
|
|
736
|
+
describe('script injectable egen-bridge.js', function() {
|
|
737
|
+
var load = function load() {
|
|
738
|
+
var attrs = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
739
|
+
delete window.EgenBridge;
|
|
740
|
+
var el = document.createElement('script');
|
|
741
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
742
|
+
try {
|
|
743
|
+
for(var _iterator = Object.entries(attrs)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
744
|
+
var _step_value = _sliced_to_array(_step.value, 2), k = _step_value[0], v = _step_value[1];
|
|
745
|
+
el.setAttribute("data-".concat(k), v);
|
|
746
|
+
}
|
|
747
|
+
} catch (err) {
|
|
748
|
+
_didIteratorError = true;
|
|
749
|
+
_iteratorError = err;
|
|
750
|
+
} finally{
|
|
751
|
+
try {
|
|
752
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
753
|
+
_iterator.return();
|
|
754
|
+
}
|
|
755
|
+
} finally{
|
|
756
|
+
if (_didIteratorError) {
|
|
757
|
+
throw _iteratorError;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
Object.defineProperty(document, 'currentScript', {
|
|
762
|
+
value: el,
|
|
763
|
+
configurable: true
|
|
764
|
+
});
|
|
765
|
+
new Function(code).call(window);
|
|
766
|
+
};
|
|
767
|
+
var code = readFileSync(resolve(process.cwd(), 'assets/egen-bridge.js'), 'utf8');
|
|
768
|
+
var HOST = 'https://core.exemple.org';
|
|
769
|
+
var parent;
|
|
770
|
+
var origPush = window.history.pushState;
|
|
771
|
+
var origReplace = window.history.replaceState;
|
|
772
|
+
var fromHost = function fromHost(message) {
|
|
773
|
+
var origin = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : HOST;
|
|
774
|
+
return window.dispatchEvent(new MessageEvent('message', {
|
|
775
|
+
data: {
|
|
776
|
+
egen: 1,
|
|
777
|
+
moduleName: 'mod',
|
|
778
|
+
message: message
|
|
779
|
+
},
|
|
780
|
+
origin: origin,
|
|
781
|
+
source: parent
|
|
782
|
+
}));
|
|
783
|
+
};
|
|
784
|
+
var out = function out() {
|
|
785
|
+
return parent.postMessage.mock.calls.map(function(c) {
|
|
786
|
+
return c[0].message;
|
|
787
|
+
});
|
|
788
|
+
};
|
|
789
|
+
beforeEach(function() {
|
|
790
|
+
parent = {
|
|
791
|
+
postMessage: vi.fn()
|
|
792
|
+
};
|
|
793
|
+
Object.defineProperty(window, 'parent', {
|
|
794
|
+
value: parent,
|
|
795
|
+
configurable: true
|
|
796
|
+
});
|
|
797
|
+
window.history.replaceState(null, '', '/dash');
|
|
798
|
+
});
|
|
799
|
+
afterEach(function() {
|
|
800
|
+
window.history.pushState = origPush;
|
|
801
|
+
window.history.replaceState = origReplace;
|
|
802
|
+
delete window.EgenBridge;
|
|
803
|
+
Object.defineProperty(window, 'parent', {
|
|
804
|
+
value: window,
|
|
805
|
+
configurable: true
|
|
806
|
+
});
|
|
807
|
+
});
|
|
808
|
+
it('silencieux tant que le Shell n\'a pas parlé ; hello → ready + position (replace)', function() {
|
|
809
|
+
load();
|
|
810
|
+
window.history.pushState(null, '', '/avant');
|
|
811
|
+
expect(parent.postMessage).not.toHaveBeenCalled();
|
|
812
|
+
fromHost({
|
|
813
|
+
kind: 'hello'
|
|
814
|
+
});
|
|
815
|
+
expect(out()).toEqual([
|
|
816
|
+
{
|
|
817
|
+
kind: 'ready',
|
|
818
|
+
caps: [
|
|
819
|
+
'location',
|
|
820
|
+
'goto'
|
|
821
|
+
]
|
|
822
|
+
},
|
|
823
|
+
{
|
|
824
|
+
kind: 'location',
|
|
825
|
+
path: '/avant',
|
|
826
|
+
replace: true,
|
|
827
|
+
title: document.title
|
|
828
|
+
}
|
|
829
|
+
]);
|
|
830
|
+
expect(parent.postMessage.mock.calls[0][1]).toBe(HOST);
|
|
831
|
+
});
|
|
832
|
+
it('annonce pushState / replaceState / popstate, sans doublon', function() {
|
|
833
|
+
load();
|
|
834
|
+
fromHost({
|
|
835
|
+
kind: 'hello'
|
|
836
|
+
});
|
|
837
|
+
parent.postMessage.mockClear();
|
|
838
|
+
window.history.pushState(null, '', '/a?x=1');
|
|
839
|
+
window.history.pushState(null, '', '/a?x=1');
|
|
840
|
+
window.history.replaceState(null, '', '/b');
|
|
841
|
+
expect(out().map(function(x) {
|
|
842
|
+
return [
|
|
843
|
+
x.path,
|
|
844
|
+
x.replace
|
|
845
|
+
];
|
|
846
|
+
})).toEqual([
|
|
847
|
+
[
|
|
848
|
+
'/a?x=1',
|
|
849
|
+
false
|
|
850
|
+
],
|
|
851
|
+
[
|
|
852
|
+
'/b',
|
|
853
|
+
true
|
|
854
|
+
]
|
|
855
|
+
]);
|
|
856
|
+
});
|
|
857
|
+
it('retire le préfixe data-base-path ; mode hash lit #/chemin', function() {
|
|
858
|
+
load({
|
|
859
|
+
'base-path': '/studio'
|
|
860
|
+
});
|
|
861
|
+
window.history.replaceState(null, '', '/studio/editor/x');
|
|
862
|
+
fromHost({
|
|
863
|
+
kind: 'hello'
|
|
864
|
+
});
|
|
865
|
+
expect(out().at(-1).path).toBe('/editor/x');
|
|
866
|
+
window.history.pushState = origPush;
|
|
867
|
+
window.history.replaceState = origReplace;
|
|
868
|
+
parent.postMessage.mockClear();
|
|
869
|
+
load({
|
|
870
|
+
mode: 'hash'
|
|
871
|
+
});
|
|
872
|
+
window.history.replaceState(null, '', '/#/dashboard/recent?team-id=1');
|
|
873
|
+
fromHost({
|
|
874
|
+
kind: 'hello'
|
|
875
|
+
});
|
|
876
|
+
expect(out().at(-1).path).toBe('/dashboard/recent?team-id=1');
|
|
877
|
+
});
|
|
878
|
+
it('goto : délègue à onGoto, ne renvoie pas l\'écho', function() {
|
|
879
|
+
load();
|
|
880
|
+
fromHost({
|
|
881
|
+
kind: 'hello'
|
|
882
|
+
});
|
|
883
|
+
parent.postMessage.mockClear();
|
|
884
|
+
var handler = vi.fn(function() {
|
|
885
|
+
return true;
|
|
886
|
+
});
|
|
887
|
+
window.EgenBridge.onGoto(handler);
|
|
888
|
+
fromHost({
|
|
889
|
+
kind: 'goto',
|
|
890
|
+
path: '/zz',
|
|
891
|
+
seq: 1
|
|
892
|
+
});
|
|
893
|
+
expect(handler).toHaveBeenCalledWith('/zz');
|
|
894
|
+
window.history.replaceState(null, '', '/zz'); // l'app a suivi : aucune annonce en retour
|
|
895
|
+
expect(out().filter(function(x) {
|
|
896
|
+
return x.kind === 'location';
|
|
897
|
+
})).toHaveLength(0);
|
|
898
|
+
});
|
|
899
|
+
it('data-hosts : messages d\'une autre origine ignorés ; une origine unique ensuite', function() {
|
|
900
|
+
load({
|
|
901
|
+
hosts: HOST
|
|
902
|
+
});
|
|
903
|
+
fromHost({
|
|
904
|
+
kind: 'hello'
|
|
905
|
+
}, 'https://evil.org');
|
|
906
|
+
expect(parent.postMessage).not.toHaveBeenCalled();
|
|
907
|
+
fromHost({
|
|
908
|
+
kind: 'hello'
|
|
909
|
+
});
|
|
910
|
+
expect(out().length).toBeGreaterThan(0);
|
|
911
|
+
});
|
|
912
|
+
it('ignore une source qui n\'est pas le parent, et mode none', function() {
|
|
913
|
+
load();
|
|
914
|
+
window.dispatchEvent(new MessageEvent('message', {
|
|
915
|
+
data: {
|
|
916
|
+
egen: 1,
|
|
917
|
+
moduleName: 'm',
|
|
918
|
+
message: {
|
|
919
|
+
kind: 'hello'
|
|
920
|
+
}
|
|
921
|
+
},
|
|
922
|
+
origin: HOST,
|
|
923
|
+
source: {}
|
|
924
|
+
}));
|
|
925
|
+
expect(parent.postMessage).not.toHaveBeenCalled();
|
|
926
|
+
load({
|
|
927
|
+
mode: 'none'
|
|
928
|
+
});
|
|
929
|
+
expect(window.EgenBridge).toBeUndefined();
|
|
930
|
+
});
|
|
931
|
+
});
|