-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathselection_input_component.test.ts
More file actions
804 lines (737 loc) · 29.7 KB
/
selection_input_component.test.ts
File metadata and controls
804 lines (737 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
import { App, Component, useSubEnv, xml } from "@odoo/owl";
import { Model } from "../../src";
import { OPEN_CF_SIDEPANEL_ACTION } from "../../src/actions/menu_items_actions";
import { SelectionInput } from "../../src/components/selection_input/selection_input";
import { ColorGenerator, toCartesian, toZone } from "../../src/helpers";
import { useStoreProvider } from "../../src/store_engine";
import { ModelStore } from "../../src/stores";
import { HighlightStore } from "../../src/stores/highlight_store";
import { Color, SpreadsheetChildEnv } from "../../src/types";
import {
activateSheet,
addCellToSelection,
createSheet,
createSheetWithName,
merge,
selectCell,
undo,
} from "../test_helpers/commands_helpers";
import {
clickCell,
keyDown,
keyUp,
selectColumnByClicking,
setInputValueAndTrigger,
simulateClick,
} from "../test_helpers/dom_helper";
import {
getChildFromComponent,
mountComponent,
mountSpreadsheet,
nextTick,
} from "../test_helpers/helpers";
let model: Model;
let fixture: HTMLElement;
function focus(index = 0) {
fixture.querySelectorAll("input")[index].focus();
}
async function writeInput(index: number, text: string) {
focus(index);
await nextTick();
const input = fixture.querySelectorAll("input")[index];
input.value = text;
input.dispatchEvent(new Event("input"));
await nextTick();
}
interface SelectionInputTestConfig {
initialRanges?: string[];
hasSingleRange?: boolean;
onChanged?: jest.Mock<void, [any]>;
onConfirmed?: jest.Mock<void, []>;
colors?: Color[];
}
class Parent extends Component<any> {
static template = xml/* xml */ `
<SelectionInput
ranges="initialRanges || []"
hasSingleRange="hasSingleRange"
onSelectionChanged="(ranges) => this.onChanged(ranges)"
onSelectionConfirmed="onConfirmed"
colors="colors || []"
/>
`;
static components = { SelectionInput };
static props = { model: Object, config: Object };
model!: Model;
initialRanges: string[] | undefined;
hasSingleRange: boolean | undefined;
onChanged!: jest.Mock<void, [any]>;
onConfirmed!: jest.Mock<void, []>;
colors: Color[] | undefined;
get id(): string {
const selectionInput = getChildFromComponent(this, SelectionInput);
return selectionInput["id"];
}
setup() {
useSubEnv({
model: this.props.model,
});
const stores = useStoreProvider();
stores.inject(ModelStore, this.props.model);
this.initialRanges = this.props.config.initialRanges;
this.hasSingleRange = this.props.config.hasSingleRange;
this.colors = this.props.config.colors;
this.model = model;
this.onChanged = this.props.config.onChanged || jest.fn();
this.onConfirmed = this.props.config.onConfirmed || jest.fn();
}
}
class MultiParent extends Component<any> {
static template = xml/* xml */ `
<div>
<div class="input-1">
<SelectionInput ranges="[]"/>
</div>
<div class="input-2">
<SelectionInput ranges="[]"/>
</div>
</div>
`;
static components = { SelectionInput };
static props = { model: Object };
setup() {
useSubEnv({
model: this.props.model,
});
const stores = useStoreProvider();
stores.inject(ModelStore, this.props.model);
}
}
async function createSelectionInput(
config: SelectionInputTestConfig = {},
fixtureEl?: HTMLElement
) {
model = new Model();
let parent: Component;
let app: App;
({ fixture, parent, app } = await mountComponent(Parent, {
model,
fixture: fixtureEl,
props: { model, config },
}));
await nextTick();
const id = (parent as Parent).id;
return {
parent: parent as Parent,
env: parent.env as SpreadsheetChildEnv,
model,
id,
app,
fixture,
};
}
describe("Selection Input", () => {
test("empty input is not colored", async () => {
await createSelectionInput();
expect(fixture.querySelectorAll("input")[0].getAttribute("style")).toBe("");
});
test("remove button is not displayed with a single input", async () => {
await createSelectionInput();
expect(fixture.querySelectorAll(".o-remove-selection").length).toBeFalsy();
});
test("remove button is displayed with more than one input", async () => {
await createSelectionInput();
await simulateClick(".o-add-selection");
expect(fixture.querySelectorAll(".o-remove-selection").length).toBe(2);
});
test("update with invalid input hide confirm button", async () => {
await createSelectionInput();
await simulateClick(fixture.querySelector("input")!);
expect(fixture.querySelectorAll(".o-selection-ok").length).toBe(1);
expect(fixture.querySelectorAll(".o-selection-ko").length).toBe(0);
await setInputValueAndTrigger(fixture.querySelector("input")!, "A1");
expect(fixture.querySelectorAll(".o-selection-ok").length).toBe(1);
expect(fixture.querySelectorAll(".o-selection-ko").length).toBe(1);
await simulateClick(".o-selection-ok");
await nextTick();
expect(fixture.querySelectorAll(".o-selection-ok").length).toBe(0);
expect(fixture.querySelectorAll(".o-selection-ko").length).toBe(0);
await simulateClick("input");
await setInputValueAndTrigger(fixture.querySelector("input")!, "this is not valid");
expect(fixture.querySelectorAll(".o-selection-ok[disabled]").length).toBe(1);
expect(fixture.querySelectorAll(".o-selection-ko").length).toBe(1);
});
test("hitting enter key acts the same as clicking confirm button", async () => {
let isConfirmed = false;
const onConfirmed = jest.fn(() => {
isConfirmed = true;
});
await createSelectionInput({ onConfirmed });
expect(fixture.querySelector(".o-focused")).toBeTruthy();
expect(isConfirmed).toBeFalsy();
await keyDown({ key: "Enter" });
expect(fixture.querySelector(".o-focused")).toBeFalsy();
expect(onConfirmed).toHaveBeenCalled();
expect(isConfirmed).toBeTruthy();
});
test("input is filled when new cells are selected", async () => {
const { model } = await createSelectionInput();
selectCell(model, "B4");
await nextTick();
expect(fixture.querySelector("input")!.value).toBe("B4");
const colorGenerator = new ColorGenerator(2);
const color = colorGenerator.next();
expect(fixture.querySelector("input")!.getAttribute("style")).toBe(`color:${color}; `);
simulateClick(".o-add-selection");
selectCell(model, "B5");
await nextTick();
const color2 = colorGenerator.next();
expect(fixture.querySelectorAll("input")[0].value).toBe("B4");
expect(fixture.querySelectorAll("input")[0].getAttribute("style")).toBe(`color:${color}; `);
expect(fixture.querySelectorAll("input")[1].value).toBe("B5");
expect(fixture.querySelectorAll("input")[1].getAttribute("style")).toBe(`color:${color2}; `);
});
test("colors passed as props are taken into account and completed by a color", async () => {
const { model } = await createSelectionInput({ colors: ["#FF0000"] });
selectCell(model, "B4");
await nextTick();
expect(fixture.querySelector("input")!.value).toBe("B4");
expect(fixture.querySelector("input")!.getAttribute("style")).toBe("color:#FF0000; ");
simulateClick(".o-add-selection");
selectCell(model, "B5");
await nextTick();
const colorGenerator = new ColorGenerator(2);
colorGenerator.next(); //the first generated color is skipped in favor of the props color
const secondColor = colorGenerator.next();
expect(fixture.querySelectorAll("input")[0].value).toBe("B4");
expect(fixture.querySelectorAll("input")[0].getAttribute("style")).toBe("color:#FF0000; ");
expect(fixture.querySelectorAll("input")[1].value).toBe("B5");
expect(fixture.querySelectorAll("input")[1].getAttribute("style")).toBe(
`color:${secondColor}; `
);
});
test("update of colors are taken into account", async () => {
// This test aims to check if the colors are updated when the colors function is updated.
// This kind of update can comes when removing a range from the selectionInput for the
// data series of a chart, as the color of the removed range won't be passed anymore to
// the colors function in the chart's side panel's props.
let colors = ["#FF0000", "#00FF00"];
const { parent, env } = await createSelectionInput({
initialRanges: ["A1", "B1"],
colors: colors,
});
const highlightStore = env.getStore(HighlightStore);
await simulateClick(fixture.querySelectorAll("input")[0]);
expect(fixture.querySelectorAll("input")[0].getAttribute("style")).toBe("color:#FF0000; ");
expect(fixture.querySelectorAll("input")[1].getAttribute("style")).toBe("color:#00FF00; ");
expect(highlightStore.highlights).toMatchObject([
{ color: "#FF0000", zone: toZone("A1") },
{ color: "#00FF00", zone: toZone("B1") },
]);
parent.colors = ["#0000FF", "#FF00FF"];
await simulateClick(fixture.querySelectorAll("input")[1]);
expect(fixture.querySelectorAll("input")[0].getAttribute("style")).toBe("color:#0000FF; ");
expect(fixture.querySelectorAll("input")[1].getAttribute("style")).toBe("color:#FF00FF; ");
expect(highlightStore.highlights).toMatchObject([
{ color: "#0000FF", zone: toZone("A1") },
{ color: "#FF00FF", zone: toZone("B1") },
]);
});
test("can select full column as unbounded zone by clicking on header", async () => {
const { model } = await createSelectionInput();
model.selection.selectColumn(3, "overrideSelection");
await nextTick();
expect(fixture.querySelector("input")!.value).toBe("D:D");
});
test("can select full row as unbounded zone by clicking on header", async () => {
const { model } = await createSelectionInput();
model.selection.selectRow(2, "overrideSelection");
await nextTick();
expect(fixture.querySelector("input")!.value).toBe("3:3");
});
test("can correctly select a merged zone", async () => {
const { model } = await createSelectionInput();
merge(model, "A1:B2");
model.selection.selectCell(0, 0);
await nextTick();
expect(fixture.querySelector("input")!.value).toBe("A1");
model.selection.selectZone({
cell: { col: 0, row: 0 },
zone: { top: 0, left: 0, bottom: 1, right: 1 },
});
await nextTick();
expect(fixture.querySelector("input")!.value).toBe("A1");
});
test("ctrl + select cell --> add new input", async () => {
const { env, model, fixture } = await mountSpreadsheet();
OPEN_CF_SIDEPANEL_ACTION(env);
await nextTick();
await simulateClick(".o-cf-add");
await nextTick();
const input = fixture.querySelector(".o-selection-input input") as HTMLInputElement;
await simulateClick(input);
clickCell(model, "B4");
await nextTick();
clickCell(model, "B5", { ctrlKey: true });
await nextTick();
const inputs = fixture.querySelectorAll(
".o-selection-input input"
) as unknown as HTMLInputElement[];
expect(inputs.length).toBe(2);
expect(inputs[0].value).toBe("B4");
expect(inputs[1].value).toBe("B5");
});
test("input is not filled with highlight when maximum ranges reached", async () => {
const { model } = await createSelectionInput({ hasSingleRange: true });
expect(fixture.querySelectorAll("input")).toHaveLength(1);
addCellToSelection(model, "B2");
await nextTick();
expect(fixture.querySelectorAll("input")).toHaveLength(1);
expect(fixture.querySelector("input")!.value).toBe("B2");
expect(fixture.querySelector(".o-add-selection")).toBeNull();
});
test("new range is added when button clicked", async () => {
await createSelectionInput();
expect(fixture.querySelectorAll("input").length).toBe(1);
await simulateClick(".o-add-selection");
expect(fixture.querySelectorAll("input").length).toBe(2);
});
test("can set initial ranges", async () => {
await createSelectionInput({ initialRanges: ["C4", "A1"] });
expect(fixture.querySelectorAll("input").length).toBe(2);
expect(fixture.querySelectorAll("input")[0].value).toBe("C4");
expect(fixture.querySelectorAll("input")[1].value).toBe("A1");
});
test("can focus a range", async () => {
await createSelectionInput();
await simulateClick(".o-add-selection"); // last input is now focused
expect(fixture.querySelectorAll("input")[0].classList).not.toContain("o-focused");
expect(fixture.querySelectorAll("input")[1].classList).toContain("o-focused");
await simulateClick("input"); // focus the first input
expect(fixture.querySelectorAll("input")[0].classList).toContain("o-focused");
expect(fixture.querySelectorAll("input")[1].classList).not.toContain("o-focused");
});
test("can unfocus all inputs with the OK button", async () => {
await createSelectionInput();
expect(fixture.querySelector(".o-focused")).toBeTruthy();
await simulateClick(".o-selection-ok");
expect(fixture.querySelector(".o-focused")).toBeFalsy();
});
test("manually input a single cell", async () => {
await createSelectionInput();
await writeInput(0, "C2");
expect(fixture.querySelectorAll("input")[0].value).toBe("C2");
});
test("manually input multiple cells", async () => {
await createSelectionInput();
await writeInput(0, "C2,A1");
expect(fixture.querySelectorAll("input")[0].value).toBe("C2");
expect(fixture.querySelectorAll("input")[1].value).toBe("A1");
});
test("manually add another range via trailing comma", async () => {
await createSelectionInput({ initialRanges: ["C2"] });
await writeInput(0, "C2,");
expect(fixture.querySelectorAll("input")[0].value).toBe("C2");
expect(fixture.querySelectorAll("input")[1].value).toBe("");
});
test.each([
[",", ""],
[",C1", "C1"],
])(
"leading comma will not split the input into multiple ranges",
async (inputString, rangeValue) => {
await createSelectionInput();
await writeInput(0, inputString);
expect(fixture.querySelectorAll("input").length).toEqual(1);
expect(fixture.querySelectorAll("input")[0].value).toBe(rangeValue);
}
);
test("F2 alters edition mode", async () => {
await createSelectionInput({ initialRanges: ["C2"] });
const selectionInputEl: HTMLInputElement = fixture.querySelector(".o-selection-input input")!;
focus(0);
await nextTick();
expect(document.activeElement).toBe(selectionInputEl);
await keyDown({ key: "ArrowLeft" });
expect(document.activeElement).toBe(selectionInputEl);
expect(selectionInputEl?.value).toEqual("B2");
keyDown({ key: "F2" });
await keyDown({ key: "ArrowLeft" });
expect(document.activeElement).toBe(selectionInputEl);
expect(selectionInputEl?.value).toEqual("B2");
});
test("changed event is triggered when input changed", async () => {
let newRanges;
const onChanged = jest.fn((ranges) => {
newRanges = ranges;
});
await createSelectionInput({ onChanged });
await writeInput(0, "C2");
expect(onChanged).toHaveBeenCalled();
expect(newRanges).toStrictEqual(["C2"]);
});
test("changed event is triggered when cell is selected", async () => {
let newRanges;
const onChanged = jest.fn((ranges) => {
newRanges = ranges;
});
const { model } = await createSelectionInput({ onChanged });
selectCell(model, "B4");
await nextTick();
expect(onChanged).toHaveBeenCalled();
expect(newRanges).toStrictEqual(["B4"]);
});
test("can select full col/row grid selection as selection input data series range", async () => {
const { env, model, fixture } = await mountSpreadsheet();
await selectColumnByClicking(model, "B");
OPEN_CF_SIDEPANEL_ACTION(env);
await nextTick();
await simulateClick(".o-cf-add");
await nextTick();
let input = fixture.querySelector(".o-selection-input input") as HTMLInputElement;
expect(input.value).toBe("B:B");
await simulateClick(input);
await selectColumnByClicking(model, "C");
input = fixture.querySelector(".o-selection-input input") as HTMLInputElement;
expect(input.value).toBe("C:C");
await selectColumnByClicking(model, "B");
input = fixture.querySelector(".o-selection-input input") as HTMLInputElement;
expect(input.value).toBe("B:B");
});
test("focus is transferred from one input to another", async () => {
model = new Model();
({ fixture } = await mountComponent(MultiParent, { props: { model }, model }));
await nextTick();
expect(fixture.querySelector(".input-1 .o-focused")).toBeFalsy();
expect(fixture.querySelector(".input-2 .o-focused")).toBeTruthy();
await simulateClick(".input-1 input");
expect(fixture.querySelector(".input-1 .o-focused")).toBeTruthy();
expect(fixture.querySelector(".input-2 .o-focused")).toBeFalsy();
});
test("focus is transferred into the newly added input automatically when typing comma at the end", async () => {
await createSelectionInput({ initialRanges: ["C2"] });
await simulateClick("input");
expect(fixture.querySelectorAll("input")[0].classList).toContain("o-focused");
await writeInput(0, "C2,");
expect(fixture.querySelectorAll("input")[0].classList).not.toContain("o-focused");
expect(fixture.querySelectorAll("input")[1].classList).toContain("o-focused");
expect(document.activeElement).toBe(fixture.querySelectorAll("input")[1]);
});
test("go back to initial sheet when selection is finished", async () => {
const { model, fixture } = await createSelectionInput();
const sheet1Id = model.getters.getActiveSheetId();
createSheet(model, { sheetId: "42", activate: true });
await createSelectionInput({}, fixture);
activateSheet(model, "42");
selectCell(model, "B4");
await nextTick();
expect(fixture.querySelector("input")!.value).toBe("Sheet2!B4");
await simulateClick(".o-selection-ok");
expect(model.getters.getActiveSheetId()).toBe(sheet1Id);
});
test("undo after selection won't change active sheet", async () => {
const { model } = await createSelectionInput();
const sheet1Id = model.getters.getActiveSheetId();
createSheet(model, { sheetId: "42" });
await createSelectionInput();
activateSheet(model, "42");
selectCell(model, "B4");
await nextTick();
await simulateClick(".o-selection-ok");
expect(model.getters.getActiveSheetId()).toBe(sheet1Id);
undo(model);
expect(model.getters.getActiveSheetId()).toBe(sheet1Id);
});
test("show red border if and only if invalid range", async () => {
await createSelectionInput();
await writeInput(0, "A1");
expect(fixture.querySelectorAll("input")[0].value).toBe("A1");
expect(fixture.querySelectorAll("input")[0].getAttribute("style")).not.toBe("");
expect(fixture.querySelectorAll("input")[0].classList).not.toContain("o-invalid");
await writeInput(0, "aaaaa");
expect(fixture.querySelectorAll("input")[0].value).toBe("aaaaa");
expect(fixture.querySelectorAll("input")[0].getAttribute("style")).toBe("");
expect(fixture.querySelectorAll("input")[0].classList).toContain("o-invalid");
await writeInput(0, "B1");
expect(fixture.querySelectorAll("input")[0].value).toBe("B1");
expect(fixture.querySelectorAll("input")[0].getAttribute("style")).not.toBe("");
expect(fixture.querySelectorAll("input")[0].classList).not.toContain("o-invalid");
});
test("don't show red border initially", async () => {
await createSelectionInput();
expect(fixture.querySelectorAll("input")[0].classList).not.toContain("o-invalid");
});
test("pressing and releasing control has no effect on future clicks", async () => {
const { env, model, fixture } = await mountSpreadsheet();
OPEN_CF_SIDEPANEL_ACTION(env);
await nextTick();
await simulateClick(".o-cf-add");
await nextTick();
let input = fixture.querySelector(".o-selection-input input") as HTMLInputElement;
await simulateClick(input);
expect(input.value).toBe("A1");
await keyDown({ key: "Control" });
await keyUp({ key: "Control" });
await clickCell(model, "A2");
expect(fixture.querySelectorAll(".o-selection-input input")).toHaveLength(1);
input = fixture.querySelector(".o-selection-input input") as HTMLInputElement;
expect(input.value).toBe("A2");
});
test("In 'isSingleRange' mode, capture the first part of a multi range input", async () => {
await createSelectionInput({ hasSingleRange: true });
await writeInput(0, "C2,A1");
expect(fixture.querySelector("input")?.value).toBe("C2");
});
describe("change highlight position in the grid", () => {
test("change the associated range in the composer ", async () => {
const { model, fixture } = await createSelectionInput({ initialRanges: ["B2"] });
focus(0);
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B2"),
});
model.selection.selectZone(
{ cell: toCartesian("C3"), zone: toZone("C3") },
{ unbounded: true }
);
await nextTick();
expect(fixture.querySelectorAll("input")[0].value).toBe("C3");
});
test("highlights change handle unbounded ranges ", async () => {
const { model, fixture } = await createSelectionInput({ initialRanges: ["B2"] });
focus(0);
await nextTick();
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B1:B100"),
});
model.selection.selectZone(
{ cell: toCartesian("C1"), zone: toZone("C1:C100") },
{ unbounded: true }
);
await nextTick();
expect(fixture.querySelectorAll("input")[0].value).toBe("C:C");
});
test("change the first associated range in the composer when ranges are the same", async () => {
const { model, fixture } = await createSelectionInput({ initialRanges: ["B2", "B2"] });
focus(0);
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B2"),
});
model.selection.selectZone(
{ cell: toCartesian("C3"), zone: toZone("C3") },
{ unbounded: true }
);
await nextTick();
const inputs = fixture.querySelectorAll("input");
expect(inputs[0].value).toBe("C3");
expect(inputs[1].value).toBe("B2");
});
test("the first range doesn't change if other highlight transit by the first range state ", async () => {
const { model, fixture } = await createSelectionInput({ initialRanges: ["B2", "B1"] });
focus(0);
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B1"),
});
model.selection.selectZone(
{ cell: toCartesian("B2"), zone: toZone("B2") },
{ unbounded: true }
);
model.selection.selectZone(
{ cell: toCartesian("B3"), zone: toZone("B3") },
{ unbounded: true }
);
await nextTick();
const inputs = fixture.querySelectorAll("input");
expect(inputs[0].value).toBe("B2");
expect(inputs[1].value).toBe("B3");
});
test("can change references of different length", async () => {
const { model, fixture } = await createSelectionInput({ initialRanges: ["B1"] });
focus(0);
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B1"),
});
model.selection.selectZone(
{ cell: toCartesian("B1"), zone: toZone("B1:B2") },
{ unbounded: true }
);
await nextTick();
expect(fixture.querySelectorAll("input")[0].value).toBe("B1:B2");
});
test("can change references with sheetname", async () => {
const { model, fixture } = await createSelectionInput({ initialRanges: ["Sheet42!B1"] });
createSheetWithName(model, { sheetId: "42", activate: true }, "Sheet42");
await nextTick();
focus(0);
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B1"),
});
model.selection.selectZone(
{ cell: toCartesian("B2"), zone: toZone("B2") },
{ unbounded: true }
);
await nextTick();
expect(fixture.querySelectorAll("input")[0].value).toBe("Sheet42!B2");
});
test("change references of the current sheet", async () => {
const { model, fixture } = await createSelectionInput({
initialRanges: ["B1", "Sheet42!B1"],
});
createSheetWithName(model, { sheetId: "42", activate: true }, "Sheet42");
await nextTick();
focus(0);
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B1"),
});
model.selection.selectZone(
{ cell: toCartesian("B2"), zone: toZone("B2") },
{ unbounded: true }
);
await nextTick();
const inputs = fixture.querySelectorAll("input");
expect(inputs[0].value).toBe("B1");
expect(inputs[1].value).toBe("Sheet42!B2");
});
test.each([
["b$1", "C$1"],
["$b1", "$C1"],
])("can change cells reference with index fixed", async (ref, resultRef) => {
const { model, fixture } = await createSelectionInput({ initialRanges: [ref] });
focus(0);
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B1"),
});
model.selection.selectZone(
{ cell: toCartesian("C1"), zone: toZone("C1") },
{ unbounded: true }
);
await nextTick();
expect(fixture.querySelectorAll("input")[0].value).toBe(resultRef);
});
test.each([
["B1:B$2", "C1:C$2"],
["B1:$B$2", "C1:$C$2"],
["B1:$B2", "C1:$C2"],
["$B1:B2", "$C1:C2"],
["$B$1:B2", "$C$1:C2"],
["B$1:B2", "C$1:C2"],
["$B1:$B2", "$C1:$C2"],
["B$1:B$2", "C$1:C$2"],
["$B$1:$B$2", "$C$1:$C$2"],
])("can change ranges reference with index fixed", async (ref, resultRef) => {
const { model, fixture } = await createSelectionInput({ initialRanges: [ref] });
focus(0);
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B1:B2"),
});
model.selection.selectZone(
{ cell: toCartesian("C1"), zone: toZone("C1:C2") },
{ unbounded: true }
);
await nextTick();
expect(fixture.querySelectorAll("input")[0].value).toBe(resultRef);
});
test("can change cells merged reference", async () => {
const { model, fixture } = await createSelectionInput({ initialRanges: ["B1"] });
merge(model, "B1:B2");
await nextTick();
focus(0);
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B1:B2"),
});
model.selection.selectZone(
{ cell: toCartesian("C1"), zone: toZone("C1") },
{ unbounded: true }
);
await nextTick();
expect(fixture.querySelectorAll("input")[0].value).toBe("C1");
await simulateClick(".o-add-selection");
await writeInput(1, "B2");
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B1:B2"),
});
model.selection.selectZone(
{ cell: toCartesian("C2"), zone: toZone("C2") },
{ unbounded: true }
);
await nextTick();
const inputs = fixture.querySelectorAll("input");
expect(inputs[0].value).toBe("C1");
expect(inputs[1].value).toBe("C2");
});
test("can change cells merged reference with index fixed", async () => {
const { model, fixture } = await createSelectionInput({ initialRanges: ["B$2"] });
merge(model, "B1:B2");
await nextTick();
focus(0);
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B1:B2"),
});
model.selection.selectZone(
{ cell: toCartesian("C1"), zone: toZone("C1:C2") },
{ unbounded: true }
);
await nextTick();
expect(fixture.querySelectorAll("input")[0].value).toBe("C$1:C$2");
});
test("references are expanded to include merges", async () => {
const { model, fixture } = await createSelectionInput({ initialRanges: ["A1:B1"] });
merge(model, "C1:D1");
await nextTick();
focus(0);
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("A1:B1"),
});
model.selection.selectZone(
{ cell: toCartesian("B1"), zone: toZone("B1:C1") },
{ unbounded: true }
);
await nextTick();
expect(fixture.querySelectorAll("input")[0].value).toBe("B1:D1");
});
test("can change references of different length with index fixed", async () => {
const { model, fixture } = await createSelectionInput({ initialRanges: ["$B$1"] });
focus(0);
model.dispatch("START_CHANGE_HIGHLIGHT", {
zone: toZone("B1"),
});
model.selection.selectZone(
{ cell: toCartesian("B1"), zone: toZone("B1:B2") },
{ unbounded: true }
);
await nextTick();
expect(fixture.querySelectorAll("input")[0].value).toBe("$B$1:$B$2");
});
});
test("After confirming an empty selection input, the confirm button should become visible.", async () => {
await createSelectionInput();
await simulateClick(fixture.querySelector("input")!);
await simulateClick(fixture.querySelector(".o-selection-ok"));
await nextTick();
expect(fixture.querySelectorAll(".o-selection-ok").length).toBe(0);
await simulateClick(fixture.querySelector("input")!);
await nextTick();
expect(fixture.querySelectorAll(".o-selection-ok").length).toBe(1);
});
test("After confirming two empty selection inputs, the confirm button should become visible.", async () => {
await createSelectionInput();
await simulateClick(fixture.querySelector("input")!);
await simulateClick(".o-add-selection");
await simulateClick(fixture.querySelector(".o-selection-ok"));
await nextTick();
expect(fixture.querySelectorAll(".o-selection-ok").length).toBe(0);
await simulateClick(fixture.querySelector("input")!);
await nextTick();
expect(fixture.querySelectorAll(".o-selection-ok").length).toBe(1);
});
test("Reset selection button reset the selection input and remove focus", async () => {
const { model } = await createSelectionInput({ initialRanges: ["C4", "A1"] });
const input = fixture.querySelector("input")!;
await simulateClick(input);
setInputValueAndTrigger(input, "C5:D9");
await nextTick();
expect(input.value).toBe("C5:D9");
await simulateClick(".o-selection-ko");
expect(input.value).toBe("C4");
expect(model.getters.isGridSelectionActive()).toBeTruthy();
});
});