newspack-components 4.10.0 → 4.11.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,1224 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
+ function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
4
+ function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
5
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
6
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
7
+ /**
8
+ * External dependencies
9
+ */
10
+ import { createEvent, fireEvent, render, screen } from '@testing-library/react';
11
+
12
+ /**
13
+ * WordPress dependencies
14
+ */
15
+ import { speak } from '@wordpress/a11y';
16
+
17
+ /**
18
+ * Internal dependencies
19
+ */
20
+ import ReorderModal, { findOverlay, moveItem } from '.';
21
+ import { jsx as _jsx } from "react/jsx-runtime";
22
+ jest.mock('@wordpress/a11y', function () {
23
+ return {
24
+ speak: jest.fn()
25
+ };
26
+ });
27
+ beforeEach(function () {
28
+ speak.mockClear();
29
+ });
30
+ describe('moveItem', function () {
31
+ it('moves an item down', function () {
32
+ expect(moveItem(['a', 'b', 'c'], 0, 2)).toEqual(['b', 'c', 'a']);
33
+ });
34
+ it('moves an item up', function () {
35
+ expect(moveItem(['a', 'b', 'c'], 2, 0)).toEqual(['c', 'a', 'b']);
36
+ });
37
+ it('returns the original array when the indices match', function () {
38
+ var items = ['a', 'b'];
39
+ expect(moveItem(items, 1, 1)).toBe(items);
40
+ });
41
+ it('returns the original array when an index is out of range', function () {
42
+ var items = ['a', 'b'];
43
+ expect(moveItem(items, 0, 5)).toBe(items);
44
+ expect(moveItem(items, -1, 0)).toBe(items);
45
+ });
46
+ });
47
+ var ITEMS = [{
48
+ value: 11,
49
+ label: 'Alpha'
50
+ }, {
51
+ value: 22,
52
+ label: 'Beta'
53
+ }, {
54
+ value: 33,
55
+ label: 'Gamma'
56
+ }];
57
+ var renderModal = function renderModal() {
58
+ var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
59
+ var onSave = jest.fn();
60
+ var onClose = jest.fn();
61
+ render(/*#__PURE__*/_jsx(ReorderModal, _objectSpread({
62
+ title: "Reorder Content",
63
+ ids: [11, 22, 33],
64
+ fetchItems: function fetchItems() {
65
+ return Promise.resolve(ITEMS);
66
+ },
67
+ onSave: onSave,
68
+ onClose: onClose
69
+ }, props)));
70
+ return {
71
+ onSave: onSave,
72
+ onClose: onClose
73
+ };
74
+ };
75
+ var titles = function titles() {
76
+ return Array.from(document.querySelectorAll('.newspack-reorder-modal__title')).map(function (el) {
77
+ return el.textContent;
78
+ });
79
+ };
80
+ var overlay = function overlay() {
81
+ return document.querySelector('.components-modal__screen-overlay');
82
+ };
83
+ var discardPrompt = function discardPrompt() {
84
+ return screen.queryByRole('dialog', {
85
+ name: 'Discard the new order?'
86
+ });
87
+ };
88
+
89
+ // jsdom has no `PointerEvent`, and `fireEvent.pointerDown` would fall back to a
90
+ // plain `Event` and drop `button`.
91
+ var press = function press(from, to) {
92
+ var button = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
93
+ fireEvent(from, new MouseEvent('pointerdown', {
94
+ bubbles: true,
95
+ button: button
96
+ }));
97
+ fireEvent(to, new MouseEvent('pointerup', {
98
+ bubbles: true,
99
+ button: button
100
+ }));
101
+ };
102
+ describe('ReorderModal', function () {
103
+ it('lists the items in the order the IDs were given', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
104
+ var _t;
105
+ return _regenerator().w(function (_context) {
106
+ while (1) switch (_context.n) {
107
+ case 0:
108
+ renderModal();
109
+ _t = expect;
110
+ _context.n = 1;
111
+ return screen.findByText('Alpha');
112
+ case 1:
113
+ _t(_context.v).toBeInTheDocument();
114
+ expect(titles()).toEqual(['Alpha', 'Beta', 'Gamma']);
115
+ case 2:
116
+ return _context.a(2);
117
+ }
118
+ }, _callee);
119
+ })));
120
+ it('falls back to (no title) for IDs the fetch does not resolve', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
121
+ var _t2;
122
+ return _regenerator().w(function (_context2) {
123
+ while (1) switch (_context2.n) {
124
+ case 0:
125
+ renderModal({
126
+ ids: [11, 99],
127
+ fetchItems: function fetchItems() {
128
+ return Promise.resolve([ITEMS[0]]);
129
+ }
130
+ });
131
+ _t2 = expect;
132
+ _context2.n = 1;
133
+ return screen.findByText('Alpha');
134
+ case 1:
135
+ _t2(_context2.v).toBeInTheDocument();
136
+ expect(titles()).toEqual(['Alpha', '(no title)']);
137
+ case 2:
138
+ return _context2.a(2);
139
+ }
140
+ }, _callee2);
141
+ })));
142
+ it('moves an item up with the chevron', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
143
+ var _t3;
144
+ return _regenerator().w(function (_context3) {
145
+ while (1) switch (_context3.n) {
146
+ case 0:
147
+ renderModal();
148
+ _t3 = fireEvent;
149
+ _context3.n = 1;
150
+ return screen.findByLabelText('Move Up: Gamma');
151
+ case 1:
152
+ _t3.click.call(_t3, _context3.v);
153
+ expect(titles()).toEqual(['Alpha', 'Gamma', 'Beta']);
154
+ case 2:
155
+ return _context3.a(2);
156
+ }
157
+ }, _callee3);
158
+ })));
159
+ it('moves an item down with the chevron', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() {
160
+ var _t4;
161
+ return _regenerator().w(function (_context4) {
162
+ while (1) switch (_context4.n) {
163
+ case 0:
164
+ renderModal();
165
+ _t4 = fireEvent;
166
+ _context4.n = 1;
167
+ return screen.findByLabelText('Move Down: Alpha');
168
+ case 1:
169
+ _t4.click.call(_t4, _context4.v);
170
+ expect(titles()).toEqual(['Beta', 'Alpha', 'Gamma']);
171
+ case 2:
172
+ return _context4.a(2);
173
+ }
174
+ }, _callee4);
175
+ })));
176
+ it('disables the chevrons at the ends of the list', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() {
177
+ var _t5;
178
+ return _regenerator().w(function (_context5) {
179
+ while (1) switch (_context5.n) {
180
+ case 0:
181
+ renderModal();
182
+ _t5 = expect;
183
+ _context5.n = 1;
184
+ return screen.findByLabelText('Move Up: Alpha');
185
+ case 1:
186
+ _t5(_context5.v).toHaveAttribute('aria-disabled', 'true');
187
+ expect(screen.getByLabelText('Move Down: Gamma')).toHaveAttribute('aria-disabled', 'true');
188
+ expect(screen.getByLabelText('Move Down: Alpha')).not.toHaveAttribute('aria-disabled');
189
+ case 2:
190
+ return _context5.a(2);
191
+ }
192
+ }, _callee5);
193
+ })));
194
+ it('keeps a disabled chevron focusable but inert', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() {
195
+ var chevron;
196
+ return _regenerator().w(function (_context6) {
197
+ while (1) switch (_context6.n) {
198
+ case 0:
199
+ renderModal();
200
+ _context6.n = 1;
201
+ return screen.findByLabelText('Move Up: Alpha');
202
+ case 1:
203
+ chevron = _context6.v;
204
+ chevron.focus();
205
+ expect(document.activeElement).toBe(chevron);
206
+ fireEvent.click(chevron);
207
+ expect(titles()).toEqual(['Alpha', 'Beta', 'Gamma']);
208
+ case 2:
209
+ return _context6.a(2);
210
+ }
211
+ }, _callee6);
212
+ })));
213
+ it('keeps the disabled Save button focusable but inert', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() {
214
+ var _renderModal, onSave, save;
215
+ return _regenerator().w(function (_context7) {
216
+ while (1) switch (_context7.n) {
217
+ case 0:
218
+ _renderModal = renderModal(), onSave = _renderModal.onSave;
219
+ _context7.n = 1;
220
+ return screen.findByRole('button', {
221
+ name: 'Save'
222
+ });
223
+ case 1:
224
+ save = _context7.v;
225
+ save.focus();
226
+ expect(document.activeElement).toBe(save);
227
+ fireEvent.click(save);
228
+ expect(onSave).not.toHaveBeenCalled();
229
+ case 2:
230
+ return _context7.a(2);
231
+ }
232
+ }, _callee7);
233
+ })));
234
+ it('keeps the item title in the accessible name and out of the tooltip', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() {
235
+ var chevron, tooltip;
236
+ return _regenerator().w(function (_context8) {
237
+ while (1) switch (_context8.n) {
238
+ case 0:
239
+ renderModal();
240
+ _context8.n = 1;
241
+ return screen.findByLabelText('Move Up: Gamma');
242
+ case 1:
243
+ chevron = _context8.v;
244
+ expect(screen.queryAllByLabelText('Move Up')).toHaveLength(0);
245
+ chevron.focus();
246
+ _context8.n = 2;
247
+ return screen.findByRole('tooltip');
248
+ case 2:
249
+ tooltip = _context8.v;
250
+ expect(tooltip).toHaveTextContent('Move Up');
251
+ expect(tooltip).not.toHaveTextContent('Gamma');
252
+ case 3:
253
+ return _context8.a(2);
254
+ }
255
+ }, _callee8);
256
+ })));
257
+ it('starts the accessible name with the visible label', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9() {
258
+ var chevron;
259
+ return _regenerator().w(function (_context9) {
260
+ while (1) switch (_context9.n) {
261
+ case 0:
262
+ renderModal();
263
+ _context9.n = 1;
264
+ return screen.findByLabelText('Move Up: Gamma');
265
+ case 1:
266
+ chevron = _context9.v;
267
+ expect(chevron.getAttribute('aria-label')).toContain('Move Up');
268
+ expect(screen.getByLabelText('Move Down: Alpha').getAttribute('aria-label')).toContain('Move Down');
269
+ case 2:
270
+ return _context9.a(2);
271
+ }
272
+ }, _callee9);
273
+ })));
274
+ it('keeps focus on the pressed chevron while it stays enabled', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0() {
275
+ var _t6;
276
+ return _regenerator().w(function (_context0) {
277
+ while (1) switch (_context0.n) {
278
+ case 0:
279
+ renderModal();
280
+ _t6 = fireEvent;
281
+ _context0.n = 1;
282
+ return screen.findByLabelText('Move Up: Gamma');
283
+ case 1:
284
+ _t6.click.call(_t6, _context0.v);
285
+ expect(document.activeElement).toBe(screen.getByLabelText('Move Up: Gamma'));
286
+ case 2:
287
+ return _context0.a(2);
288
+ }
289
+ }, _callee0);
290
+ })));
291
+ it('moves focus to the other chevron when the pressed one disables', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1() {
292
+ var _t7;
293
+ return _regenerator().w(function (_context1) {
294
+ while (1) switch (_context1.n) {
295
+ case 0:
296
+ renderModal();
297
+ _t7 = fireEvent;
298
+ _context1.n = 1;
299
+ return screen.findByLabelText('Move Up: Beta');
300
+ case 1:
301
+ _t7.click.call(_t7, _context1.v);
302
+ expect(screen.getByLabelText('Move Up: Beta')).toHaveAttribute('aria-disabled', 'true');
303
+ expect(document.activeElement).toBe(screen.getByLabelText('Move Down: Beta'));
304
+ case 2:
305
+ return _context1.a(2);
306
+ }
307
+ }, _callee1);
308
+ })));
309
+ it('names the loading state and announces the list once it arrives', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10() {
310
+ return _regenerator().w(function (_context10) {
311
+ while (1) switch (_context10.n) {
312
+ case 0:
313
+ renderModal();
314
+ expect(screen.getByText('Loading content…')).toBeInTheDocument();
315
+ _context10.n = 1;
316
+ return screen.findByText('Alpha');
317
+ case 1:
318
+ expect(screen.queryByText('Loading content…')).not.toBeInTheDocument();
319
+ expect(speak).toHaveBeenCalledWith('3 items ready to reorder.');
320
+ case 2:
321
+ return _context10.a(2);
322
+ }
323
+ }, _callee10);
324
+ })));
325
+ it('names the row it moved, not just the position', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee11() {
326
+ var _t8;
327
+ return _regenerator().w(function (_context11) {
328
+ while (1) switch (_context11.n) {
329
+ case 0:
330
+ renderModal();
331
+ _t8 = fireEvent;
332
+ _context11.n = 1;
333
+ return screen.findByLabelText('Move Up: Gamma');
334
+ case 1:
335
+ _t8.click.call(_t8, _context11.v);
336
+ expect(speak).toHaveBeenCalledWith('Gamma moved to position 2 of 3.');
337
+ case 2:
338
+ return _context11.a(2);
339
+ }
340
+ }, _callee11);
341
+ })));
342
+ it('saves the reordered IDs', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee12() {
343
+ var _renderModal2, onSave, _t9;
344
+ return _regenerator().w(function (_context12) {
345
+ while (1) switch (_context12.n) {
346
+ case 0:
347
+ _renderModal2 = renderModal(), onSave = _renderModal2.onSave;
348
+ _t9 = fireEvent;
349
+ _context12.n = 1;
350
+ return screen.findByLabelText('Move Up: Gamma');
351
+ case 1:
352
+ _t9.click.call(_t9, _context12.v);
353
+ fireEvent.click(screen.getByRole('button', {
354
+ name: 'Save'
355
+ }));
356
+ expect(onSave).toHaveBeenCalledWith([11, 33, 22]);
357
+ case 2:
358
+ return _context12.a(2);
359
+ }
360
+ }, _callee12);
361
+ })));
362
+ it('discards the new order once the discard is confirmed', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee13() {
363
+ var _renderModal3, onSave, onClose, _t0;
364
+ return _regenerator().w(function (_context13) {
365
+ while (1) switch (_context13.n) {
366
+ case 0:
367
+ _renderModal3 = renderModal(), onSave = _renderModal3.onSave, onClose = _renderModal3.onClose;
368
+ _t0 = fireEvent;
369
+ _context13.n = 1;
370
+ return screen.findByLabelText('Move Up: Gamma');
371
+ case 1:
372
+ _t0.click.call(_t0, _context13.v);
373
+ fireEvent.click(screen.getByRole('button', {
374
+ name: 'Cancel'
375
+ }));
376
+ fireEvent.click(screen.getByRole('button', {
377
+ name: 'Discard'
378
+ }));
379
+ expect(onSave).not.toHaveBeenCalled();
380
+ expect(onClose).toHaveBeenCalled();
381
+ case 2:
382
+ return _context13.a(2);
383
+ }
384
+ }, _callee13);
385
+ })));
386
+ it('disables saving until the order changes', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee14() {
387
+ var _t1;
388
+ return _regenerator().w(function (_context14) {
389
+ while (1) switch (_context14.n) {
390
+ case 0:
391
+ renderModal();
392
+ _t1 = expect;
393
+ _context14.n = 1;
394
+ return screen.findByRole('button', {
395
+ name: 'Save'
396
+ });
397
+ case 1:
398
+ _t1(_context14.v).toHaveAttribute('aria-disabled', 'true');
399
+ fireEvent.click(screen.getByLabelText('Move Up: Gamma'));
400
+ expect(screen.getByRole('button', {
401
+ name: 'Save'
402
+ })).not.toHaveAttribute('aria-disabled');
403
+ case 2:
404
+ return _context14.a(2);
405
+ }
406
+ }, _callee14);
407
+ })));
408
+ it('disables saving again when the order is moved back', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee15() {
409
+ var _t10;
410
+ return _regenerator().w(function (_context15) {
411
+ while (1) switch (_context15.n) {
412
+ case 0:
413
+ renderModal();
414
+ _t10 = fireEvent;
415
+ _context15.n = 1;
416
+ return screen.findByLabelText('Move Up: Gamma');
417
+ case 1:
418
+ _t10.click.call(_t10, _context15.v);
419
+ expect(screen.getByRole('button', {
420
+ name: 'Save'
421
+ })).not.toHaveAttribute('aria-disabled');
422
+ fireEvent.click(screen.getByLabelText('Move Down: Gamma'));
423
+ expect(titles()).toEqual(['Alpha', 'Beta', 'Gamma']);
424
+ expect(screen.getByRole('button', {
425
+ name: 'Save'
426
+ })).toHaveAttribute('aria-disabled', 'true');
427
+ case 2:
428
+ return _context15.a(2);
429
+ }
430
+ }, _callee15);
431
+ })));
432
+ it('closes without confirming when nothing was reordered', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee16() {
433
+ var _renderModal4, onClose, _t11;
434
+ return _regenerator().w(function (_context16) {
435
+ while (1) switch (_context16.n) {
436
+ case 0:
437
+ _renderModal4 = renderModal(), onClose = _renderModal4.onClose;
438
+ _t11 = fireEvent;
439
+ _context16.n = 1;
440
+ return screen.findByRole('button', {
441
+ name: 'Cancel'
442
+ });
443
+ case 1:
444
+ _t11.click.call(_t11, _context16.v);
445
+ expect(onClose).toHaveBeenCalled();
446
+ expect(discardPrompt()).not.toBeInTheDocument();
447
+ case 2:
448
+ return _context16.a(2);
449
+ }
450
+ }, _callee16);
451
+ })));
452
+ it('asks before discarding and holds the modal open until answered', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee17() {
453
+ var _renderModal5, onSave, onClose, _t12;
454
+ return _regenerator().w(function (_context17) {
455
+ while (1) switch (_context17.n) {
456
+ case 0:
457
+ _renderModal5 = renderModal(), onSave = _renderModal5.onSave, onClose = _renderModal5.onClose;
458
+ _t12 = fireEvent;
459
+ _context17.n = 1;
460
+ return screen.findByLabelText('Move Up: Gamma');
461
+ case 1:
462
+ _t12.click.call(_t12, _context17.v);
463
+ fireEvent.click(screen.getByRole('button', {
464
+ name: 'Cancel'
465
+ }));
466
+ expect(discardPrompt()).toBeInTheDocument();
467
+ expect(onClose).not.toHaveBeenCalled();
468
+ expect(onSave).not.toHaveBeenCalled();
469
+ case 2:
470
+ return _context17.a(2);
471
+ }
472
+ }, _callee17);
473
+ })));
474
+
475
+ // The modal takes Escape over from `Modal`, whose own handler defers the close
476
+ // request behind an exit animation, so the confirmation is up synchronously.
477
+ it('asks before discarding when Escape dismisses the modal', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee18() {
478
+ var _renderModal6, onClose, _t13;
479
+ return _regenerator().w(function (_context18) {
480
+ while (1) switch (_context18.n) {
481
+ case 0:
482
+ _renderModal6 = renderModal(), onClose = _renderModal6.onClose;
483
+ _t13 = fireEvent;
484
+ _context18.n = 1;
485
+ return screen.findByLabelText('Move Up: Gamma');
486
+ case 1:
487
+ _t13.click.call(_t13, _context18.v);
488
+ fireEvent.keyDown(document.activeElement, {
489
+ key: 'Escape'
490
+ });
491
+ expect(discardPrompt()).toBeInTheDocument();
492
+ expect(onClose).not.toHaveBeenCalled();
493
+ case 2:
494
+ return _context18.a(2);
495
+ }
496
+ }, _callee18);
497
+ })));
498
+ it('closes on Escape without confirming when nothing was reordered', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee19() {
499
+ var _renderModal7, onClose;
500
+ return _regenerator().w(function (_context19) {
501
+ while (1) switch (_context19.n) {
502
+ case 0:
503
+ _renderModal7 = renderModal(), onClose = _renderModal7.onClose;
504
+ _context19.n = 1;
505
+ return screen.findByText('Alpha');
506
+ case 1:
507
+ fireEvent.keyDown(screen.getByRole('dialog'), {
508
+ key: 'Escape'
509
+ });
510
+ expect(onClose).toHaveBeenCalled();
511
+ expect(discardPrompt()).not.toBeInTheDocument();
512
+ case 2:
513
+ return _context19.a(2);
514
+ }
515
+ }, _callee19);
516
+ })));
517
+ it('asks before discarding when the header close button dismisses the modal', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee20() {
518
+ var _renderModal8, onClose, _t14;
519
+ return _regenerator().w(function (_context20) {
520
+ while (1) switch (_context20.n) {
521
+ case 0:
522
+ _renderModal8 = renderModal(), onClose = _renderModal8.onClose;
523
+ _t14 = fireEvent;
524
+ _context20.n = 1;
525
+ return screen.findByLabelText('Move Up: Gamma');
526
+ case 1:
527
+ _t14.click.call(_t14, _context20.v);
528
+ fireEvent.click(screen.getByRole('button', {
529
+ name: 'Close'
530
+ }));
531
+ expect(discardPrompt()).toBeInTheDocument();
532
+ expect(onClose).not.toHaveBeenCalled();
533
+ case 2:
534
+ return _context20.a(2);
535
+ }
536
+ }, _callee20);
537
+ })));
538
+ it('closes when a press starts and ends on the overlay', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee21() {
539
+ var _renderModal9, onClose;
540
+ return _regenerator().w(function (_context21) {
541
+ while (1) switch (_context21.n) {
542
+ case 0:
543
+ _renderModal9 = renderModal(), onClose = _renderModal9.onClose;
544
+ _context21.n = 1;
545
+ return screen.findByText('Alpha');
546
+ case 1:
547
+ press(overlay(), overlay());
548
+ expect(onClose).toHaveBeenCalled();
549
+ case 2:
550
+ return _context21.a(2);
551
+ }
552
+ }, _callee21);
553
+ })));
554
+ it('asks before discarding when an overlay press dismisses a reordered list', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee22() {
555
+ var _renderModal0, onClose, _t15;
556
+ return _regenerator().w(function (_context22) {
557
+ while (1) switch (_context22.n) {
558
+ case 0:
559
+ _renderModal0 = renderModal(), onClose = _renderModal0.onClose;
560
+ _t15 = fireEvent;
561
+ _context22.n = 1;
562
+ return screen.findByLabelText('Move Up: Gamma');
563
+ case 1:
564
+ _t15.click.call(_t15, _context22.v);
565
+ press(overlay(), overlay());
566
+ expect(discardPrompt()).toBeInTheDocument();
567
+ expect(onClose).not.toHaveBeenCalled();
568
+ case 2:
569
+ return _context22.a(2);
570
+ }
571
+ }, _callee22);
572
+ })));
573
+ it('ignores a press that starts on a row and ends on the overlay', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee23() {
574
+ var _renderModal1, onClose;
575
+ return _regenerator().w(function (_context23) {
576
+ while (1) switch (_context23.n) {
577
+ case 0:
578
+ _renderModal1 = renderModal(), onClose = _renderModal1.onClose;
579
+ _context23.n = 1;
580
+ return screen.findByText('Alpha');
581
+ case 1:
582
+ press(document.querySelector('.newspack-reorder-modal__item'), overlay());
583
+ expect(onClose).not.toHaveBeenCalled();
584
+ expect(discardPrompt()).not.toBeInTheDocument();
585
+ case 2:
586
+ return _context23.a(2);
587
+ }
588
+ }, _callee23);
589
+ })));
590
+ it('ignores a secondary-button press on the overlay', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee24() {
591
+ var _renderModal10, onClose;
592
+ return _regenerator().w(function (_context24) {
593
+ while (1) switch (_context24.n) {
594
+ case 0:
595
+ _renderModal10 = renderModal(), onClose = _renderModal10.onClose;
596
+ _context24.n = 1;
597
+ return screen.findByText('Alpha');
598
+ case 1:
599
+ press(overlay(), overlay(), 2);
600
+ expect(onClose).not.toHaveBeenCalled();
601
+ case 2:
602
+ return _context24.a(2);
603
+ }
604
+ }, _callee24);
605
+ })));
606
+
607
+ // `ConfirmDialog` announces `contentLabel` as the dialog's name and hides its
608
+ // header, so repeating it in the body would have it read out twice.
609
+ it('states the consequence in the discard prompt rather than repeating its name', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee25() {
610
+ var _t16;
611
+ return _regenerator().w(function (_context25) {
612
+ while (1) switch (_context25.n) {
613
+ case 0:
614
+ renderModal();
615
+ _t16 = fireEvent;
616
+ _context25.n = 1;
617
+ return screen.findByLabelText('Move Up: Gamma');
618
+ case 1:
619
+ _t16.click.call(_t16, _context25.v);
620
+ fireEvent.click(screen.getByRole('button', {
621
+ name: 'Cancel'
622
+ }));
623
+ expect(discardPrompt()).toHaveTextContent('The order you set will be lost.');
624
+ expect(discardPrompt()).not.toHaveTextContent('Discard the new order?');
625
+ case 2:
626
+ return _context25.a(2);
627
+ }
628
+ }, _callee25);
629
+ })));
630
+ it('keeps the reordered list when the discard is dismissed', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee26() {
631
+ var _renderModal11, onSave, onClose, _t17;
632
+ return _regenerator().w(function (_context26) {
633
+ while (1) switch (_context26.n) {
634
+ case 0:
635
+ _renderModal11 = renderModal(), onSave = _renderModal11.onSave, onClose = _renderModal11.onClose;
636
+ _t17 = fireEvent;
637
+ _context26.n = 1;
638
+ return screen.findByLabelText('Move Up: Gamma');
639
+ case 1:
640
+ _t17.click.call(_t17, _context26.v);
641
+ fireEvent.click(screen.getByRole('button', {
642
+ name: 'Cancel'
643
+ }));
644
+ fireEvent.click(screen.getByRole('button', {
645
+ name: 'Keep editing'
646
+ }));
647
+ expect(discardPrompt()).not.toBeInTheDocument();
648
+ expect(titles()).toEqual(['Alpha', 'Gamma', 'Beta']);
649
+ expect(onClose).not.toHaveBeenCalled();
650
+ expect(onSave).not.toHaveBeenCalled();
651
+ case 2:
652
+ return _context26.a(2);
653
+ }
654
+ }, _callee26);
655
+ })));
656
+ it('exposes the rows as a list', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee27() {
657
+ return _regenerator().w(function (_context27) {
658
+ while (1) switch (_context27.n) {
659
+ case 0:
660
+ renderModal();
661
+ _context27.n = 1;
662
+ return screen.findByText('Alpha');
663
+ case 1:
664
+ expect(screen.getByRole('list')).toBeInTheDocument();
665
+ expect(screen.getAllByRole('listitem')).toHaveLength(3);
666
+ case 2:
667
+ return _context27.a(2);
668
+ }
669
+ }, _callee27);
670
+ })));
671
+ it('carries the full title on a row whose text is clipped', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee28() {
672
+ return _regenerator().w(function (_context28) {
673
+ while (1) switch (_context28.n) {
674
+ case 0:
675
+ renderModal();
676
+ _context28.n = 1;
677
+ return screen.findByText('Alpha');
678
+ case 1:
679
+ expect(document.querySelector('.newspack-reorder-modal__title')).toHaveAttribute('title', 'Alpha');
680
+ case 2:
681
+ return _context28.a(2);
682
+ }
683
+ }, _callee28);
684
+ })));
685
+ });
686
+
687
+ // jsdom ships no `DataTransfer`, so the drag payload is stubbed.
688
+ var dataTransfer = function dataTransfer() {
689
+ return {
690
+ data: {},
691
+ dropEffect: 'none',
692
+ effectAllowed: 'none',
693
+ setData: function setData(type, value) {
694
+ this.data[type] = value;
695
+ },
696
+ getData: function getData(type) {
697
+ return this.data[type];
698
+ }
699
+ };
700
+ };
701
+ var rows = function rows() {
702
+ return Array.from(document.querySelectorAll('.newspack-reorder-modal__item'));
703
+ };
704
+ describe('ReorderModal toggles and badges', function () {
705
+ it('has no toggles unless enabled IDs are given', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee29() {
706
+ return _regenerator().w(function (_context29) {
707
+ while (1) switch (_context29.n) {
708
+ case 0:
709
+ renderModal();
710
+ _context29.n = 1;
711
+ return screen.findByText('Alpha');
712
+ case 1:
713
+ expect(screen.queryByRole('checkbox')).not.toBeInTheDocument();
714
+ case 2:
715
+ return _context29.a(2);
716
+ }
717
+ }, _callee29);
718
+ })));
719
+ it('saves the order and the enabled IDs in that order', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee30() {
720
+ var _renderModal12, onSave, _t18;
721
+ return _regenerator().w(function (_context30) {
722
+ while (1) switch (_context30.n) {
723
+ case 0:
724
+ _renderModal12 = renderModal({
725
+ enabledIds: [11, 33]
726
+ }), onSave = _renderModal12.onSave;
727
+ _t18 = fireEvent;
728
+ _context30.n = 1;
729
+ return screen.findByRole('checkbox', {
730
+ name: 'Beta'
731
+ });
732
+ case 1:
733
+ _t18.click.call(_t18, _context30.v);
734
+ fireEvent.click(screen.getByRole('checkbox', {
735
+ name: 'Alpha'
736
+ }));
737
+ fireEvent.click(screen.getByLabelText('Move Up: Gamma'));
738
+ fireEvent.click(screen.getByRole('button', {
739
+ name: 'Save'
740
+ }));
741
+ expect(onSave).toHaveBeenCalledWith([11, 33, 22], [33, 22]);
742
+ case 2:
743
+ return _context30.a(2);
744
+ }
745
+ }, _callee30);
746
+ })));
747
+ it('treats a toggle change as unsaved', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee31() {
748
+ var _t19;
749
+ return _regenerator().w(function (_context31) {
750
+ while (1) switch (_context31.n) {
751
+ case 0:
752
+ renderModal({
753
+ enabledIds: [11]
754
+ });
755
+ _t19 = fireEvent;
756
+ _context31.n = 1;
757
+ return screen.findByRole('checkbox', {
758
+ name: 'Beta'
759
+ });
760
+ case 1:
761
+ _t19.click.call(_t19, _context31.v);
762
+ expect(screen.getByRole('button', {
763
+ name: 'Save'
764
+ })).not.toHaveAttribute('aria-disabled');
765
+ fireEvent.click(screen.getByRole('button', {
766
+ name: 'Cancel'
767
+ }));
768
+ expect(screen.getByRole('dialog', {
769
+ name: 'Discard the changes?'
770
+ })).toBeInTheDocument();
771
+ case 2:
772
+ return _context31.a(2);
773
+ }
774
+ }, _callee31);
775
+ })));
776
+ it('disables saving again when a toggle is set back', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee32() {
777
+ var toggle;
778
+ return _regenerator().w(function (_context32) {
779
+ while (1) switch (_context32.n) {
780
+ case 0:
781
+ renderModal({
782
+ enabledIds: [11]
783
+ });
784
+ _context32.n = 1;
785
+ return screen.findByRole('checkbox', {
786
+ name: 'Beta'
787
+ });
788
+ case 1:
789
+ toggle = _context32.v;
790
+ fireEvent.click(toggle);
791
+ fireEvent.click(toggle);
792
+ expect(screen.getByRole('button', {
793
+ name: 'Save'
794
+ })).toHaveAttribute('aria-disabled', 'true');
795
+ case 2:
796
+ return _context32.a(2);
797
+ }
798
+ }, _callee32);
799
+ })));
800
+ var withBadge = function withBadge() {
801
+ return Promise.resolve([ITEMS[0], _objectSpread(_objectSpread({}, ITEMS[1]), {}, {
802
+ badge: {
803
+ label: 'Premium',
804
+ tooltip: 'Gated by Members'
805
+ }
806
+ }), ITEMS[2]]);
807
+ };
808
+ it('shows the badge an item carries', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee33() {
809
+ var badges;
810
+ return _regenerator().w(function (_context33) {
811
+ while (1) switch (_context33.n) {
812
+ case 0:
813
+ renderModal({
814
+ fetchItems: withBadge
815
+ });
816
+ _context33.n = 1;
817
+ return screen.findByText('Alpha');
818
+ case 1:
819
+ badges = document.querySelectorAll('.newspack-reorder-modal__badge');
820
+ expect(badges).toHaveLength(1);
821
+ expect(badges[0].closest('[data-item-id]')).toHaveAttribute('data-item-id', '22');
822
+ case 2:
823
+ return _context33.a(2);
824
+ }
825
+ }, _callee33);
826
+ })));
827
+ it('describes the toggle with the badge and its tooltip', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee34() {
828
+ var _t20;
829
+ return _regenerator().w(function (_context34) {
830
+ while (1) switch (_context34.n) {
831
+ case 0:
832
+ renderModal({
833
+ fetchItems: withBadge,
834
+ enabledIds: [11]
835
+ });
836
+ _t20 = expect;
837
+ _context34.n = 1;
838
+ return screen.findByRole('checkbox', {
839
+ name: 'Beta'
840
+ });
841
+ case 1:
842
+ _t20(_context34.v).toHaveAccessibleDescription('Premium: Gated by Members');
843
+ expect(screen.getByRole('checkbox', {
844
+ name: 'Alpha'
845
+ })).not.toHaveAttribute('aria-describedby');
846
+ case 2:
847
+ return _context34.a(2);
848
+ }
849
+ }, _callee34);
850
+ })));
851
+ it('describes the dialog with the description', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee35() {
852
+ return _regenerator().w(function (_context35) {
853
+ while (1) switch (_context35.n) {
854
+ case 0:
855
+ renderModal({
856
+ description: 'Turn on the items to include.'
857
+ });
858
+ _context35.n = 1;
859
+ return screen.findByText('Alpha');
860
+ case 1:
861
+ expect(screen.getByRole('dialog', {
862
+ name: 'Reorder Content'
863
+ })).toHaveAccessibleDescription('Turn on the items to include.');
864
+ case 2:
865
+ return _context35.a(2);
866
+ }
867
+ }, _callee35);
868
+ })));
869
+ it('holds Save while every toggle is off', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee36() {
870
+ var _t21;
871
+ return _regenerator().w(function (_context36) {
872
+ while (1) switch (_context36.n) {
873
+ case 0:
874
+ renderModal({
875
+ enabledIds: [11]
876
+ });
877
+ _t21 = fireEvent;
878
+ _context36.n = 1;
879
+ return screen.findByRole('checkbox', {
880
+ name: 'Alpha'
881
+ });
882
+ case 1:
883
+ _t21.click.call(_t21, _context36.v);
884
+ expect(screen.getByRole('button', {
885
+ name: 'Save'
886
+ })).toHaveAttribute('aria-disabled', 'true');
887
+ expect(screen.getByRole('button', {
888
+ name: 'Save'
889
+ })).toHaveAccessibleDescription('Turn on at least one item to save.');
890
+ case 2:
891
+ return _context36.a(2);
892
+ }
893
+ }, _callee36);
894
+ })));
895
+ it('keeps enabled IDs that have no row', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee37() {
896
+ var _renderModal13, onSave, _t22;
897
+ return _regenerator().w(function (_context37) {
898
+ while (1) switch (_context37.n) {
899
+ case 0:
900
+ _renderModal13 = renderModal({
901
+ enabledIds: [99, 11]
902
+ }), onSave = _renderModal13.onSave;
903
+ _t22 = fireEvent;
904
+ _context37.n = 1;
905
+ return screen.findByRole('checkbox', {
906
+ name: 'Alpha'
907
+ });
908
+ case 1:
909
+ _t22.click.call(_t22, _context37.v);
910
+ fireEvent.click(screen.getByRole('button', {
911
+ name: 'Save'
912
+ }));
913
+ expect(onSave).toHaveBeenCalledWith([11, 22, 33], [99]);
914
+ case 2:
915
+ return _context37.a(2);
916
+ }
917
+ }, _callee37);
918
+ })));
919
+ it('does not start a drag that began on a toggle', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee38() {
920
+ var toggle, dt;
921
+ return _regenerator().w(function (_context38) {
922
+ while (1) switch (_context38.n) {
923
+ case 0:
924
+ renderModal({
925
+ enabledIds: [11]
926
+ });
927
+ _context38.n = 1;
928
+ return screen.findByRole('checkbox', {
929
+ name: 'Gamma'
930
+ });
931
+ case 1:
932
+ toggle = _context38.v;
933
+ fireEvent(toggle, new MouseEvent('pointerdown', {
934
+ bubbles: true
935
+ }));
936
+ dt = dataTransfer();
937
+ fireEvent.dragStart(rows()[2], {
938
+ dataTransfer: dt
939
+ });
940
+ expect(dt.getData('text/plain')).toBeUndefined();
941
+ case 2:
942
+ return _context38.a(2);
943
+ }
944
+ }, _callee38);
945
+ })));
946
+ });
947
+ describe('ReorderModal drag and drop', function () {
948
+ it('reorders as a row is dragged over another', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee39() {
949
+ var dt;
950
+ return _regenerator().w(function (_context39) {
951
+ while (1) switch (_context39.n) {
952
+ case 0:
953
+ renderModal();
954
+ _context39.n = 1;
955
+ return screen.findByText('Alpha');
956
+ case 1:
957
+ dt = dataTransfer();
958
+ fireEvent.dragStart(rows()[2], {
959
+ dataTransfer: dt
960
+ });
961
+ expect(dt.getData('text/plain')).toBe('33');
962
+ fireEvent.dragOver(rows()[0], {
963
+ dataTransfer: dt
964
+ });
965
+ expect(titles()).toEqual(['Gamma', 'Alpha', 'Beta']);
966
+ case 2:
967
+ return _context39.a(2);
968
+ }
969
+ }, _callee39);
970
+ })));
971
+ it('keeps the new order when the drag is dropped', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee40() {
972
+ var dt;
973
+ return _regenerator().w(function (_context40) {
974
+ while (1) switch (_context40.n) {
975
+ case 0:
976
+ renderModal();
977
+ _context40.n = 1;
978
+ return screen.findByText('Alpha');
979
+ case 1:
980
+ dt = dataTransfer();
981
+ fireEvent.dragStart(rows()[2], {
982
+ dataTransfer: dt
983
+ });
984
+ fireEvent.dragOver(rows()[0], {
985
+ dataTransfer: dt
986
+ });
987
+ dt.dropEffect = 'move';
988
+ fireEvent.dragEnd(rows()[0], {
989
+ dataTransfer: dt
990
+ });
991
+ expect(titles()).toEqual(['Gamma', 'Alpha', 'Beta']);
992
+ case 2:
993
+ return _context40.a(2);
994
+ }
995
+ }, _callee40);
996
+ })));
997
+ it('puts the order back when the drag is cancelled', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee41() {
998
+ var dt;
999
+ return _regenerator().w(function (_context41) {
1000
+ while (1) switch (_context41.n) {
1001
+ case 0:
1002
+ renderModal();
1003
+ _context41.n = 1;
1004
+ return screen.findByText('Alpha');
1005
+ case 1:
1006
+ dt = dataTransfer();
1007
+ fireEvent.dragStart(rows()[2], {
1008
+ dataTransfer: dt
1009
+ });
1010
+ fireEvent.dragOver(rows()[0], {
1011
+ dataTransfer: dt
1012
+ });
1013
+ expect(titles()).toEqual(['Gamma', 'Alpha', 'Beta']);
1014
+ dt.dropEffect = 'none';
1015
+ fireEvent.dragEnd(rows()[0], {
1016
+ dataTransfer: dt
1017
+ });
1018
+ expect(titles()).toEqual(['Alpha', 'Beta', 'Gamma']);
1019
+ case 2:
1020
+ return _context41.a(2);
1021
+ }
1022
+ }, _callee41);
1023
+ })));
1024
+ it('reorders as a row is dragged down past the rows below it', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee42() {
1025
+ var dt;
1026
+ return _regenerator().w(function (_context42) {
1027
+ while (1) switch (_context42.n) {
1028
+ case 0:
1029
+ renderModal();
1030
+ _context42.n = 1;
1031
+ return screen.findByText('Alpha');
1032
+ case 1:
1033
+ dt = dataTransfer();
1034
+ fireEvent.dragStart(rows()[0], {
1035
+ dataTransfer: dt
1036
+ });
1037
+ fireEvent.dragOver(rows()[1], {
1038
+ dataTransfer: dt
1039
+ });
1040
+ expect(titles()).toEqual(['Beta', 'Alpha', 'Gamma']);
1041
+
1042
+ // The dragged row now sits under the cursor, so the next `dragover` reaches
1043
+ // it rather than the row it swapped with: it has to leave the order alone.
1044
+ fireEvent.dragOver(rows()[1], {
1045
+ dataTransfer: dt
1046
+ });
1047
+ expect(titles()).toEqual(['Beta', 'Alpha', 'Gamma']);
1048
+ fireEvent.dragOver(rows()[2], {
1049
+ dataTransfer: dt
1050
+ });
1051
+ expect(titles()).toEqual(['Beta', 'Gamma', 'Alpha']);
1052
+ case 2:
1053
+ return _context42.a(2);
1054
+ }
1055
+ }, _callee42);
1056
+ })));
1057
+
1058
+ // `dragstart` fires at the draggable row even when the gesture began on a
1059
+ // chevron inside it, so the guard keys off where the pointer went down.
1060
+ it('does not start a drag that began on a chevron', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee43() {
1061
+ var chevron, dt;
1062
+ return _regenerator().w(function (_context43) {
1063
+ while (1) switch (_context43.n) {
1064
+ case 0:
1065
+ renderModal();
1066
+ _context43.n = 1;
1067
+ return screen.findByLabelText('Move Up: Gamma');
1068
+ case 1:
1069
+ chevron = _context43.v;
1070
+ fireEvent(chevron, new MouseEvent('pointerdown', {
1071
+ bubbles: true
1072
+ }));
1073
+ dt = dataTransfer();
1074
+ fireEvent.dragStart(rows()[2], {
1075
+ dataTransfer: dt
1076
+ });
1077
+ expect(dt.getData('text/plain')).toBeUndefined();
1078
+ fireEvent.dragOver(rows()[0], {
1079
+ dataTransfer: dt
1080
+ });
1081
+ expect(titles()).toEqual(['Alpha', 'Beta', 'Gamma']);
1082
+ case 2:
1083
+ return _context43.a(2);
1084
+ }
1085
+ }, _callee43);
1086
+ })));
1087
+ it('starts a drag that began on the row itself', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee44() {
1088
+ var dt;
1089
+ return _regenerator().w(function (_context44) {
1090
+ while (1) switch (_context44.n) {
1091
+ case 0:
1092
+ renderModal();
1093
+ _context44.n = 1;
1094
+ return screen.findByText('Alpha');
1095
+ case 1:
1096
+ fireEvent(rows()[2], new MouseEvent('pointerdown', {
1097
+ bubbles: true
1098
+ }));
1099
+ dt = dataTransfer();
1100
+ fireEvent.dragStart(rows()[2], {
1101
+ dataTransfer: dt
1102
+ });
1103
+ expect(dt.getData('text/plain')).toBe('33');
1104
+ case 2:
1105
+ return _context44.a(2);
1106
+ }
1107
+ }, _callee44);
1108
+ })));
1109
+
1110
+ // Rows are the only drop targets, so the dialog has to accept the drop as well:
1111
+ // otherwise the gaps between rows, and the frame's own padding, read as a
1112
+ // cancelled drag and undo the reorder.
1113
+ it.each([['frame', '.components-modal__frame'], ['body', '.newspack-reorder-modal__body']])('accepts a drop released on the %s', /*#__PURE__*/function () {
1114
+ var _ref45 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee45(_name, selector) {
1115
+ var target, over, drop;
1116
+ return _regenerator().w(function (_context45) {
1117
+ while (1) switch (_context45.n) {
1118
+ case 0:
1119
+ renderModal();
1120
+ _context45.n = 1;
1121
+ return screen.findByText('Alpha');
1122
+ case 1:
1123
+ target = document.querySelector(selector);
1124
+ over = createEvent.dragOver(target, {
1125
+ dataTransfer: dataTransfer()
1126
+ });
1127
+ fireEvent(target, over);
1128
+ expect(over.defaultPrevented).toBe(true);
1129
+ drop = createEvent.drop(target, {
1130
+ dataTransfer: dataTransfer()
1131
+ });
1132
+ fireEvent(target, drop);
1133
+ expect(drop.defaultPrevented).toBe(true);
1134
+ case 2:
1135
+ return _context45.a(2);
1136
+ }
1137
+ }, _callee45);
1138
+ }));
1139
+ return function (_x, _x2) {
1140
+ return _ref45.apply(this, arguments);
1141
+ };
1142
+ }());
1143
+ it('leaves a drop released on the overlay cancelling the drag', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee46() {
1144
+ var over;
1145
+ return _regenerator().w(function (_context46) {
1146
+ while (1) switch (_context46.n) {
1147
+ case 0:
1148
+ renderModal();
1149
+ _context46.n = 1;
1150
+ return screen.findByText('Alpha');
1151
+ case 1:
1152
+ over = createEvent.dragOver(overlay(), {
1153
+ dataTransfer: dataTransfer()
1154
+ });
1155
+ fireEvent(overlay(), over);
1156
+ expect(over.defaultPrevented).toBe(false);
1157
+ case 2:
1158
+ return _context46.a(2);
1159
+ }
1160
+ }, _callee46);
1161
+ })));
1162
+ });
1163
+ describe('ReorderModal when the titles cannot be loaded', function () {
1164
+ var errorSpy;
1165
+ beforeEach(function () {
1166
+ errorSpy = jest.spyOn(console, 'error').mockImplementation(function () {});
1167
+ });
1168
+ afterEach(function () {
1169
+ errorSpy.mockRestore();
1170
+ });
1171
+ it('says so instead of offering rows nobody can tell apart', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee47() {
1172
+ var _renderModal14, onClose, _t23;
1173
+ return _regenerator().w(function (_context47) {
1174
+ while (1) switch (_context47.n) {
1175
+ case 0:
1176
+ _renderModal14 = renderModal({
1177
+ fetchItems: function fetchItems() {
1178
+ return Promise.reject(new Error('unreachable'));
1179
+ }
1180
+ }), onClose = _renderModal14.onClose; // The same wording also lands in the live region core announces it through.
1181
+ _t23 = expect;
1182
+ _context47.n = 1;
1183
+ return screen.findByText('The content could not be loaded, so the order cannot be saved.', {
1184
+ selector: '.components-notice__content'
1185
+ });
1186
+ case 1:
1187
+ _t23(_context47.v).toBeInTheDocument();
1188
+ expect(titles()).toEqual([]);
1189
+ expect(screen.queryByRole('list')).not.toBeInTheDocument();
1190
+ expect(screen.queryByRole('button', {
1191
+ name: 'Save'
1192
+ })).not.toBeInTheDocument();
1193
+ fireEvent.click(screen.getByRole('button', {
1194
+ name: 'Cancel'
1195
+ }));
1196
+ expect(onClose).toHaveBeenCalled();
1197
+ case 2:
1198
+ return _context47.a(2);
1199
+ }
1200
+ }, _callee47);
1201
+ })));
1202
+ });
1203
+ describe('findOverlay', function () {
1204
+ var markup = function markup(html) {
1205
+ var root = document.createElement('div');
1206
+ root.innerHTML = html;
1207
+ return root;
1208
+ };
1209
+ it('takes the node itself when it is the overlay', function () {
1210
+ var overlayNode = markup('<div class="components-modal__screen-overlay"></div>').firstChild;
1211
+ expect(findOverlay(overlayNode)).toBe(overlayNode);
1212
+ });
1213
+ it('finds the overlay from a node inside it', function () {
1214
+ var root = markup('<div class="components-modal__screen-overlay"><div class="components-modal__frame"></div></div>');
1215
+ expect(findOverlay(root.querySelector('.components-modal__frame'))).toBe(root.firstChild);
1216
+ });
1217
+ it('finds the overlay from a node wrapping it', function () {
1218
+ var root = markup('<div class="components-modal__screen-overlay"></div>');
1219
+ expect(findOverlay(root)).toBe(root.firstChild);
1220
+ });
1221
+ it('reports nothing rather than guessing', function () {
1222
+ expect(findOverlay(null)).toBe(null);
1223
+ });
1224
+ });