@egen-civitas/esm-nextjs-utils 0.0.0-stage → 0.2.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.
@@ -0,0 +1,952 @@
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 _class_call_check(instance, Constructor) {
39
+ if (!(instance instanceof Constructor)) {
40
+ throw new TypeError("Cannot call a class as a function");
41
+ }
42
+ }
43
+ function _defineProperties(target, props) {
44
+ for(var i = 0; i < props.length; i++){
45
+ var descriptor = props[i];
46
+ descriptor.enumerable = descriptor.enumerable || false;
47
+ descriptor.configurable = true;
48
+ if ("value" in descriptor) descriptor.writable = true;
49
+ Object.defineProperty(target, descriptor.key, descriptor);
50
+ }
51
+ }
52
+ function _create_class(Constructor, protoProps, staticProps) {
53
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
54
+ if (staticProps) _defineProperties(Constructor, staticProps);
55
+ return Constructor;
56
+ }
57
+ function _define_property(obj, key, value) {
58
+ if (key in obj) {
59
+ Object.defineProperty(obj, key, {
60
+ value: value,
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true
64
+ });
65
+ } else {
66
+ obj[key] = value;
67
+ }
68
+ return obj;
69
+ }
70
+ function _iterable_to_array_limit(arr, i) {
71
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
72
+ if (_i == null) return;
73
+ var _arr = [];
74
+ var _n = true;
75
+ var _d = false;
76
+ var _s, _e;
77
+ try {
78
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
79
+ _arr.push(_s.value);
80
+ if (i && _arr.length === i) break;
81
+ }
82
+ } catch (err) {
83
+ _d = true;
84
+ _e = err;
85
+ } finally{
86
+ try {
87
+ if (!_n && _i["return"] != null) _i["return"]();
88
+ } finally{
89
+ if (_d) throw _e;
90
+ }
91
+ }
92
+ return _arr;
93
+ }
94
+ function _non_iterable_rest() {
95
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
96
+ }
97
+ function _object_spread(target) {
98
+ for(var i = 1; i < arguments.length; i++){
99
+ var source = arguments[i] != null ? arguments[i] : {};
100
+ var ownKeys = Object.keys(source);
101
+ if (typeof Object.getOwnPropertySymbols === "function") {
102
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
103
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
104
+ }));
105
+ }
106
+ ownKeys.forEach(function(key) {
107
+ _define_property(target, key, source[key]);
108
+ });
109
+ }
110
+ return target;
111
+ }
112
+ function ownKeys(object, enumerableOnly) {
113
+ var keys = Object.keys(object);
114
+ if (Object.getOwnPropertySymbols) {
115
+ var symbols = Object.getOwnPropertySymbols(object);
116
+ if (enumerableOnly) {
117
+ symbols = symbols.filter(function(sym) {
118
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
119
+ });
120
+ }
121
+ keys.push.apply(keys, symbols);
122
+ }
123
+ return keys;
124
+ }
125
+ function _object_spread_props(target, source) {
126
+ source = source != null ? source : {};
127
+ if (Object.getOwnPropertyDescriptors) {
128
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
129
+ } else {
130
+ ownKeys(Object(source)).forEach(function(key) {
131
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
132
+ });
133
+ }
134
+ return target;
135
+ }
136
+ function _sliced_to_array(arr, i) {
137
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
138
+ }
139
+ function _type_of(obj) {
140
+ "@swc/helpers - typeof";
141
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
142
+ }
143
+ function _unsupported_iterable_to_array(o, minLen) {
144
+ if (!o) return;
145
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
146
+ var n = Object.prototype.toString.call(o).slice(8, -1);
147
+ if (n === "Object" && o.constructor) n = o.constructor.name;
148
+ if (n === "Map" || n === "Set") return Array.from(n);
149
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
150
+ }
151
+ function _ts_generator(thisArg, body) {
152
+ var f, y, t, _ = {
153
+ label: 0,
154
+ sent: function() {
155
+ if (t[0] & 1) throw t[1];
156
+ return t[1];
157
+ },
158
+ trys: [],
159
+ ops: []
160
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
161
+ return d(g, "next", {
162
+ value: verb(0)
163
+ }), d(g, "throw", {
164
+ value: verb(1)
165
+ }), d(g, "return", {
166
+ value: verb(2)
167
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
168
+ value: function() {
169
+ return this;
170
+ }
171
+ }), g;
172
+ function verb(n) {
173
+ return function(v) {
174
+ return step([
175
+ n,
176
+ v
177
+ ]);
178
+ };
179
+ }
180
+ function step(op) {
181
+ if (f) throw new TypeError("Generator is already executing.");
182
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
183
+ 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;
184
+ if (y = 0, t) op = [
185
+ op[0] & 2,
186
+ t.value
187
+ ];
188
+ switch(op[0]){
189
+ case 0:
190
+ case 1:
191
+ t = op;
192
+ break;
193
+ case 4:
194
+ _.label++;
195
+ return {
196
+ value: op[1],
197
+ done: false
198
+ };
199
+ case 5:
200
+ _.label++;
201
+ y = op[1];
202
+ op = [
203
+ 0
204
+ ];
205
+ continue;
206
+ case 7:
207
+ op = _.ops.pop();
208
+ _.trys.pop();
209
+ continue;
210
+ default:
211
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
212
+ _ = 0;
213
+ continue;
214
+ }
215
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
216
+ _.label = op[1];
217
+ break;
218
+ }
219
+ if (op[0] === 6 && _.label < t[1]) {
220
+ _.label = t[1];
221
+ t = op;
222
+ break;
223
+ }
224
+ if (t && _.label < t[2]) {
225
+ _.label = t[2];
226
+ _.ops.push(op);
227
+ break;
228
+ }
229
+ if (t[2]) _.ops.pop();
230
+ _.trys.pop();
231
+ continue;
232
+ }
233
+ op = body.call(thisArg, _);
234
+ } catch (e) {
235
+ op = [
236
+ 6,
237
+ e
238
+ ];
239
+ y = 0;
240
+ } finally{
241
+ f = t = 0;
242
+ }
243
+ if (op[0] & 5) throw op[1];
244
+ return {
245
+ value: op[0] ? op[1] : void 0,
246
+ done: true
247
+ };
248
+ }
249
+ }
250
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
251
+ import { createStore } from 'zustand/vanilla';
252
+ var m = vi.hoisted(function() {
253
+ return {
254
+ session: undefined
255
+ };
256
+ });
257
+ vi.mock('@egen-civitas/esm-api', function() {
258
+ return {
259
+ getSessionStore: function getSessionStore() {
260
+ return m.session;
261
+ }
262
+ };
263
+ });
264
+ vi.mock('@egen-civitas/esm-config', function() {
265
+ return {};
266
+ });
267
+ vi.mock('@egen-civitas/esm-extensions', function() {
268
+ return {};
269
+ });
270
+ vi.mock('@egen-civitas/esm-feature-flags', function() {
271
+ return {};
272
+ });
273
+ import { getEmbedLifecycle, getSyncLifecycle, isSafeInternalPath, pickFields, DEFAULT_SESSION_FIELDS, wrap } from './index.js';
274
+ import { createEgenBridge } from './client.js';
275
+ var ORIGIN = 'https://next.exemple.org';
276
+ var opts = {
277
+ moduleName: '@acme/next-app',
278
+ featureName: 'next',
279
+ url: "".concat(ORIGIN, "/app"),
280
+ minHeight: 300
281
+ };
282
+ var fakeWin = function fakeWin() {
283
+ return {
284
+ postMessage: vi.fn()
285
+ };
286
+ };
287
+ function post(data, source) {
288
+ var origin = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : ORIGIN;
289
+ window.dispatchEvent(new MessageEvent('message', {
290
+ data: data,
291
+ origin: origin,
292
+ source: source
293
+ }));
294
+ }
295
+ beforeEach(function() {
296
+ // happy-dom signale « chargement d'iframe désactivé » : voulu ici (la fenêtre de l'iframe est simulée)
297
+ vi.spyOn(console, 'error').mockImplementation(function() {});
298
+ m.session = createStore(function() {
299
+ return {
300
+ loaded: false,
301
+ session: null
302
+ };
303
+ });
304
+ window.getEgenSpaBase = function() {
305
+ return '/egen/spa/';
306
+ };
307
+ window.i18next = {
308
+ language: 'fr',
309
+ on: vi.fn(),
310
+ off: vi.fn()
311
+ };
312
+ window.singleSpaNavigate = vi.fn();
313
+ });
314
+ afterEach(function() {
315
+ document.body.innerHTML = '';
316
+ vi.restoreAllMocks();
317
+ });
318
+ describe('utilitaires du protocole', function() {
319
+ it('pickFields : chemins pointés, jamais de sessionId par défaut', function() {
320
+ var s = {
321
+ authenticated: true,
322
+ sessionId: 'SECRET',
323
+ locale: 'fr',
324
+ user: {
325
+ uuid: 'u',
326
+ display: 'Ada',
327
+ username: 'ada',
328
+ userProperties: {
329
+ x: 1
330
+ }
331
+ }
332
+ };
333
+ expect(pickFields(s, DEFAULT_SESSION_FIELDS)).toEqual({
334
+ authenticated: true,
335
+ locale: 'fr',
336
+ user: {
337
+ uuid: 'u',
338
+ display: 'Ada',
339
+ username: 'ada'
340
+ }
341
+ });
342
+ expect(JSON.stringify(pickFields(s, DEFAULT_SESSION_FIELDS))).not.toContain('SECRET');
343
+ });
344
+ it('isSafeInternalPath', function() {
345
+ for(var _i = 0, _iter = [
346
+ '/accueil',
347
+ '/a/b?x=1',
348
+ '/'
349
+ ]; _i < _iter.length; _i++){
350
+ var ok = _iter[_i];
351
+ expect(isSafeInternalPath(ok)).toBe(true);
352
+ }
353
+ for(var _i1 = 0, _iter1 = [
354
+ '//evil.com',
355
+ 'https://evil.com',
356
+ 'javascript:alert(1)',
357
+ 'accueil',
358
+ '/a b',
359
+ '/\\evil',
360
+ 42,
361
+ undefined
362
+ ]; _i1 < _iter1.length; _i1++){
363
+ var ko = _iter1[_i1];
364
+ expect(isSafeInternalPath(ko)).toBe(false);
365
+ }
366
+ });
367
+ });
368
+ describe('côté Shell : getEmbedLifecycle', function() {
369
+ var mount = function mount() {
370
+ return _async_to_generator(function() {
371
+ var extra, lc, el, props, iframe, win;
372
+ var _arguments = arguments;
373
+ return _ts_generator(this, function(_state) {
374
+ switch(_state.label){
375
+ case 0:
376
+ extra = _arguments.length > 0 && _arguments[0] !== void 0 ? _arguments[0] : {};
377
+ lc = getEmbedLifecycle(_object_spread({}, opts, extra));
378
+ el = document.createElement('div');
379
+ el.id = 'single-spa-application:n-page-0';
380
+ document.body.appendChild(el);
381
+ props = {
382
+ name: 'n-page-0'
383
+ };
384
+ return [
385
+ 4,
386
+ lc.bootstrap(props)
387
+ ];
388
+ case 1:
389
+ _state.sent();
390
+ return [
391
+ 4,
392
+ lc.mount(props)
393
+ ];
394
+ case 2:
395
+ _state.sent();
396
+ iframe = el.querySelector('iframe');
397
+ win = fakeWin();
398
+ Object.defineProperty(iframe, 'contentWindow', {
399
+ value: win,
400
+ configurable: true
401
+ });
402
+ return [
403
+ 2,
404
+ {
405
+ lc: lc,
406
+ el: el,
407
+ iframe: iframe,
408
+ win: win,
409
+ props: props
410
+ }
411
+ ];
412
+ }
413
+ });
414
+ }).apply(this, arguments);
415
+ };
416
+ it('monte une iframe sandboxée, titrée, avec hauteur minimale', function() {
417
+ return _async_to_generator(function() {
418
+ var iframe;
419
+ return _ts_generator(this, function(_state) {
420
+ switch(_state.label){
421
+ case 0:
422
+ return [
423
+ 4,
424
+ mount()
425
+ ];
426
+ case 1:
427
+ iframe = _state.sent().iframe;
428
+ expect(iframe.getAttribute('src')).toBe("".concat(ORIGIN, "/app"));
429
+ expect(iframe.getAttribute('sandbox')).toContain('allow-scripts');
430
+ expect(iframe.title).toBe('next');
431
+ expect(iframe.style.minHeight).toBe('300px');
432
+ return [
433
+ 2
434
+ ];
435
+ }
436
+ });
437
+ })();
438
+ });
439
+ it('prêt → envoie init à l’origine exacte, avec session filtrée', function() {
440
+ return _async_to_generator(function() {
441
+ var win, _win_postMessage_mock_calls_, data, target;
442
+ return _ts_generator(this, function(_state) {
443
+ switch(_state.label){
444
+ case 0:
445
+ return [
446
+ 4,
447
+ mount()
448
+ ];
449
+ case 1:
450
+ win = _state.sent().win;
451
+ m.session.setState({
452
+ loaded: true,
453
+ session: {
454
+ authenticated: true,
455
+ sessionId: 'SECRET',
456
+ user: {
457
+ uuid: 'u',
458
+ display: 'Ada',
459
+ username: 'ada',
460
+ secret: 1
461
+ }
462
+ }
463
+ });
464
+ post(wrap('@acme/next-app', {
465
+ kind: 'ready'
466
+ }), win);
467
+ expect(win.postMessage).toHaveBeenCalledTimes(1);
468
+ _win_postMessage_mock_calls_ = _sliced_to_array(win.postMessage.mock.calls[0], 2), data = _win_postMessage_mock_calls_[0], target = _win_postMessage_mock_calls_[1];
469
+ expect(target).toBe(ORIGIN);
470
+ expect(data.message.kind).toBe('init');
471
+ expect(data.message.payload).toMatchObject({
472
+ moduleName: '@acme/next-app',
473
+ locale: 'fr',
474
+ spaBase: '/egen/spa/'
475
+ });
476
+ expect(data.message.payload.session).toEqual({
477
+ authenticated: true,
478
+ user: {
479
+ uuid: 'u',
480
+ display: 'Ada',
481
+ username: 'ada'
482
+ }
483
+ });
484
+ expect(JSON.stringify(data)).not.toContain('SECRET');
485
+ return [
486
+ 2
487
+ ];
488
+ }
489
+ });
490
+ })();
491
+ });
492
+ it('relaie les changements de session après « prêt »', function() {
493
+ return _async_to_generator(function() {
494
+ var win;
495
+ return _ts_generator(this, function(_state) {
496
+ switch(_state.label){
497
+ case 0:
498
+ return [
499
+ 4,
500
+ mount()
501
+ ];
502
+ case 1:
503
+ win = _state.sent().win;
504
+ post(wrap('@acme/next-app', {
505
+ kind: 'ready'
506
+ }), win);
507
+ win.postMessage.mockClear();
508
+ m.session.setState({
509
+ loaded: true,
510
+ session: {
511
+ authenticated: true,
512
+ user: {
513
+ uuid: 'u2',
514
+ display: 'Bob',
515
+ username: 'bob'
516
+ }
517
+ }
518
+ });
519
+ expect(win.postMessage).toHaveBeenCalledOnce();
520
+ expect(win.postMessage.mock.calls[0][0].message).toMatchObject({
521
+ kind: 'session',
522
+ session: {
523
+ user: {
524
+ display: 'Bob'
525
+ }
526
+ }
527
+ });
528
+ return [
529
+ 2
530
+ ];
531
+ }
532
+ });
533
+ })();
534
+ });
535
+ it('ignore les messages d’une autre origine, d’une autre fenêtre, d’un autre module ou mal formés', function() {
536
+ return _async_to_generator(function() {
537
+ var onNavigate, win, nav;
538
+ return _ts_generator(this, function(_state) {
539
+ switch(_state.label){
540
+ case 0:
541
+ onNavigate = vi.fn();
542
+ return [
543
+ 4,
544
+ mount({
545
+ onNavigate: onNavigate
546
+ })
547
+ ];
548
+ case 1:
549
+ win = _state.sent().win;
550
+ nav = wrap('@acme/next-app', {
551
+ kind: 'navigate',
552
+ to: '/x'
553
+ });
554
+ post(nav, win, 'https://evil.example');
555
+ post(nav, fakeWin());
556
+ post(wrap('@autre/module', {
557
+ kind: 'navigate',
558
+ to: '/x'
559
+ }), win);
560
+ post({
561
+ egen: 2,
562
+ moduleName: '@acme/next-app',
563
+ message: {
564
+ kind: 'navigate',
565
+ to: '/x'
566
+ }
567
+ }, win);
568
+ post('texte', win);
569
+ post(null, win);
570
+ expect(onNavigate).not.toHaveBeenCalled();
571
+ post(nav, win);
572
+ expect(onNavigate).toHaveBeenCalledWith('/x');
573
+ return [
574
+ 2
575
+ ];
576
+ }
577
+ });
578
+ })();
579
+ });
580
+ it('navigation : chemins internes seulement ; défaut = singleSpaNavigate(spaBase + chemin)', function() {
581
+ return _async_to_generator(function() {
582
+ var win;
583
+ return _ts_generator(this, function(_state) {
584
+ switch(_state.label){
585
+ case 0:
586
+ return [
587
+ 4,
588
+ mount()
589
+ ];
590
+ case 1:
591
+ win = _state.sent().win;
592
+ post(wrap('@acme/next-app', {
593
+ kind: 'navigate',
594
+ to: '//evil.com'
595
+ }), win);
596
+ post(wrap('@acme/next-app', {
597
+ kind: 'navigate',
598
+ to: 'https://evil.com'
599
+ }), win);
600
+ expect(window.singleSpaNavigate).not.toHaveBeenCalled();
601
+ post(wrap('@acme/next-app', {
602
+ kind: 'navigate',
603
+ to: '/accueil'
604
+ }), win);
605
+ expect(window.singleSpaNavigate).toHaveBeenCalledWith('/egen/spa/accueil');
606
+ return [
607
+ 2
608
+ ];
609
+ }
610
+ });
611
+ })();
612
+ });
613
+ it('resize et titre', function() {
614
+ return _async_to_generator(function() {
615
+ var _ref, win, iframe;
616
+ return _ts_generator(this, function(_state) {
617
+ switch(_state.label){
618
+ case 0:
619
+ return [
620
+ 4,
621
+ mount()
622
+ ];
623
+ case 1:
624
+ _ref = _state.sent(), win = _ref.win, iframe = _ref.iframe;
625
+ post(wrap('@acme/next-app', {
626
+ kind: 'resize',
627
+ height: 812.4
628
+ }), win);
629
+ expect(iframe.style.height).toBe('813px');
630
+ post(wrap('@acme/next-app', {
631
+ kind: 'resize',
632
+ height: -5
633
+ }), win);
634
+ post(wrap('@acme/next-app', {
635
+ kind: 'resize',
636
+ height: Number.NaN
637
+ }), win);
638
+ expect(iframe.style.height).toBe('813px');
639
+ post(wrap('@acme/next-app', {
640
+ kind: 'title',
641
+ title: 'Tableau de bord'
642
+ }), win);
643
+ expect(iframe.title).toBe('Tableau de bord');
644
+ return [
645
+ 2
646
+ ];
647
+ }
648
+ });
649
+ })();
650
+ });
651
+ it('démontage : iframe retirée, plus aucun message traité', function() {
652
+ return _async_to_generator(function() {
653
+ var onNavigate, _ref, lc, el, win, props;
654
+ return _ts_generator(this, function(_state) {
655
+ switch(_state.label){
656
+ case 0:
657
+ onNavigate = vi.fn();
658
+ return [
659
+ 4,
660
+ mount({
661
+ onNavigate: onNavigate
662
+ })
663
+ ];
664
+ case 1:
665
+ _ref = _state.sent(), lc = _ref.lc, el = _ref.el, win = _ref.win, props = _ref.props;
666
+ return [
667
+ 4,
668
+ lc.unmount(props)
669
+ ];
670
+ case 2:
671
+ _state.sent();
672
+ expect(el.querySelector('iframe')).toBeNull();
673
+ post(wrap('@acme/next-app', {
674
+ kind: 'navigate',
675
+ to: '/x'
676
+ }), win);
677
+ expect(onNavigate).not.toHaveBeenCalled();
678
+ return [
679
+ 2
680
+ ];
681
+ }
682
+ });
683
+ })();
684
+ });
685
+ it('refuse une origine « * » et des options invalides', function() {
686
+ return _async_to_generator(function() {
687
+ var lc, el;
688
+ return _ts_generator(this, function(_state) {
689
+ lc = getEmbedLifecycle(_object_spread_props(_object_spread({}, opts), {
690
+ allowedOrigin: '*'
691
+ }));
692
+ el = document.createElement('div');
693
+ el.id = 'single-spa-application:x-0';
694
+ document.body.appendChild(el);
695
+ expect(function() {
696
+ return lc.mount({
697
+ name: 'x-0'
698
+ });
699
+ }).toThrow(/origine explicite/);
700
+ expect(function() {
701
+ return getEmbedLifecycle({
702
+ moduleName: 'x'
703
+ });
704
+ }).toThrow('Invalid options');
705
+ return [
706
+ 2
707
+ ];
708
+ });
709
+ })();
710
+ });
711
+ it('avertit si l’application embarquée a la même origine que le Shell avec scripts + même origine', function() {
712
+ return _async_to_generator(function() {
713
+ var warn;
714
+ return _ts_generator(this, function(_state) {
715
+ switch(_state.label){
716
+ case 0:
717
+ warn = vi.spyOn(console, 'warn').mockImplementation(function() {});
718
+ return [
719
+ 4,
720
+ mount({
721
+ url: "".concat(window.location.origin, "/app")
722
+ })
723
+ ];
724
+ case 1:
725
+ _state.sent();
726
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('bac à sable est sans effet'));
727
+ return [
728
+ 2
729
+ ];
730
+ }
731
+ });
732
+ })();
733
+ });
734
+ it('getSyncLifecycle : fonction → promesse de lifecycles', function() {
735
+ return _async_to_generator(function() {
736
+ var lc;
737
+ return _ts_generator(this, function(_state) {
738
+ switch(_state.label){
739
+ case 0:
740
+ return [
741
+ 4,
742
+ getSyncLifecycle(opts)()
743
+ ];
744
+ case 1:
745
+ lc = _state.sent();
746
+ expect(_type_of(lc.mount)).toBe('function');
747
+ return [
748
+ 2
749
+ ];
750
+ }
751
+ });
752
+ })();
753
+ });
754
+ });
755
+ describe('côté application : createEgenBridge', function() {
756
+ var HOST = 'https://shell.exemple.org';
757
+ var host;
758
+ beforeEach(function() {
759
+ host = fakeWin();
760
+ Object.defineProperty(window, 'parent', {
761
+ value: host,
762
+ configurable: true
763
+ });
764
+ });
765
+ afterEach(function() {
766
+ Object.defineProperty(window, 'parent', {
767
+ value: window,
768
+ configurable: true
769
+ });
770
+ });
771
+ var make = function make() {
772
+ return createEgenBridge({
773
+ moduleName: '@acme/next-app',
774
+ allowedHostOrigins: [
775
+ HOST
776
+ ]
777
+ });
778
+ };
779
+ it('annonce « prêt » à l’origine autorisée, reçoit init puis les mises à jour', function() {
780
+ return _async_to_generator(function() {
781
+ var b, states;
782
+ return _ts_generator(this, function(_state) {
783
+ switch(_state.label){
784
+ case 0:
785
+ b = make();
786
+ expect(host.postMessage).toHaveBeenCalledWith(wrap('@acme/next-app', {
787
+ kind: 'ready'
788
+ }), HOST);
789
+ states = [];
790
+ b.subscribe(function(s) {
791
+ return states.push(s);
792
+ });
793
+ post(wrap('@acme/next-app', {
794
+ kind: 'init',
795
+ payload: {
796
+ moduleName: '@acme/next-app',
797
+ locale: 'fr',
798
+ spaBase: '/s/',
799
+ session: {
800
+ authenticated: true
801
+ }
802
+ }
803
+ }), host, HOST);
804
+ return [
805
+ 4,
806
+ expect(b.ready).resolves.toMatchObject({
807
+ locale: 'fr'
808
+ })
809
+ ];
810
+ case 1:
811
+ _state.sent();
812
+ post(wrap('@acme/next-app', {
813
+ kind: 'session',
814
+ session: {
815
+ authenticated: false
816
+ }
817
+ }), host, HOST);
818
+ post(wrap('@acme/next-app', {
819
+ kind: 'locale',
820
+ locale: 'en'
821
+ }), host, HOST);
822
+ expect(b.getState()).toMatchObject({
823
+ locale: 'en',
824
+ session: {
825
+ authenticated: false
826
+ },
827
+ spaBase: '/s/'
828
+ });
829
+ expect(states).toHaveLength(3);
830
+ b.destroy();
831
+ return [
832
+ 2
833
+ ];
834
+ }
835
+ });
836
+ })();
837
+ });
838
+ it('ignore une autre origine, une autre fenêtre, un autre module', function() {
839
+ var b = make();
840
+ post(wrap('@acme/next-app', {
841
+ kind: 'locale',
842
+ locale: 'en'
843
+ }), host, 'https://evil.example');
844
+ post(wrap('@acme/next-app', {
845
+ kind: 'locale',
846
+ locale: 'en'
847
+ }), fakeWin(), HOST);
848
+ post(wrap('@autre', {
849
+ kind: 'locale',
850
+ locale: 'en'
851
+ }), host, HOST);
852
+ expect(b.getState()).toEqual({});
853
+ b.destroy();
854
+ });
855
+ it('navigate / resize / title : enveloppe + origine du Shell', function() {
856
+ var b = make();
857
+ host.postMessage.mockClear();
858
+ b.navigate('/accueil');
859
+ b.setHeight(500);
860
+ b.setTitle('Titre');
861
+ expect(host.postMessage.mock.calls.map(function(param) {
862
+ var _param = _sliced_to_array(param, 2), d = _param[0], o = _param[1];
863
+ return [
864
+ d.message,
865
+ o
866
+ ];
867
+ })).toEqual([
868
+ [
869
+ {
870
+ kind: 'navigate',
871
+ to: '/accueil'
872
+ },
873
+ HOST
874
+ ],
875
+ [
876
+ {
877
+ kind: 'resize',
878
+ height: 500
879
+ },
880
+ HOST
881
+ ],
882
+ [
883
+ {
884
+ kind: 'title',
885
+ title: 'Titre'
886
+ },
887
+ HOST
888
+ ]
889
+ ]);
890
+ b.destroy();
891
+ });
892
+ it('après destroy : plus d’écoute', function() {
893
+ var b = make();
894
+ b.destroy();
895
+ post(wrap('@acme/next-app', {
896
+ kind: 'locale',
897
+ locale: 'en'
898
+ }), host, HOST);
899
+ expect(b.getState()).toEqual({});
900
+ });
901
+ it('refuse « * », une liste vide, et l’exécution hors iframe', function() {
902
+ expect(function() {
903
+ return createEgenBridge({
904
+ moduleName: 'a',
905
+ allowedHostOrigins: [
906
+ '*'
907
+ ]
908
+ });
909
+ }).toThrow(/jamais/);
910
+ expect(function() {
911
+ return createEgenBridge({
912
+ moduleName: 'a',
913
+ allowedHostOrigins: []
914
+ });
915
+ }).toThrow(/au moins une origine/);
916
+ Object.defineProperty(window, 'parent', {
917
+ value: window,
918
+ configurable: true
919
+ });
920
+ expect(function() {
921
+ return make();
922
+ }).toThrow(/dans une iframe/);
923
+ });
924
+ it('autoResize annonce la hauteur du document', function() {
925
+ var cb = function cb() {};
926
+ globalThis.ResizeObserver = /*#__PURE__*/ function() {
927
+ "use strict";
928
+ function _class(f) {
929
+ _class_call_check(this, _class);
930
+ cb = f;
931
+ }
932
+ _create_class(_class, [
933
+ {
934
+ key: "observe",
935
+ value: function observe() {}
936
+ },
937
+ {
938
+ key: "disconnect",
939
+ value: function disconnect() {}
940
+ }
941
+ ]);
942
+ return _class;
943
+ }();
944
+ var b = make();
945
+ host.postMessage.mockClear();
946
+ var stop = b.autoResize();
947
+ cb();
948
+ expect(host.postMessage.mock.calls[0][0].message.kind).toBe('resize');
949
+ stop();
950
+ b.destroy();
951
+ });
952
+ });