-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdata_validation_clipboard_plugin.test.ts
223 lines (183 loc) · 8.12 KB
/
data_validation_clipboard_plugin.test.ts
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
import { Model, UIPlugin } from "../../src";
import { featurePluginRegistry } from "../../src/plugins";
import { Command, DataValidationCriterion, UID } from "../../src/types";
import {
activateSheet,
addDataValidation,
copy,
createSheet,
cut,
deleteSheet,
paste,
removeDataValidation,
} from "../test_helpers/commands_helpers";
import { addTestPlugin, getDataValidationRules } from "../test_helpers/helpers";
describe("Data validation", () => {
let model: Model;
let sheetId: UID;
beforeEach(() => {
model = new Model();
sheetId = model.getters.getActiveSheetId();
});
test("Can copy data validation rule on same sheet", () => {
const criterion: DataValidationCriterion = { type: "textContains", values: ["1"] };
addDataValidation(model, "A1:A5", "id", criterion);
copy(model, "A1:A5");
paste(model, "C1");
expect(getDataValidationRules(model, sheetId)).toMatchObject([
{ id: "id", criterion, ranges: ["A1:A5", "C1:C5"] },
]);
});
test("Can copy data validation rule on another sheet", () => {
const criterion: DataValidationCriterion = { type: "textContains", values: ["1"] };
addDataValidation(model, "A1:A5", "id", criterion);
copy(model, "A1:A5");
createSheet(model, { sheetId: "sheet2", activate: true });
paste(model, "C1");
expect(getDataValidationRules(model, "sheet2")).toMatchObject([
{ id: expect.any(String), criterion, ranges: ["C1:C5"] },
]);
paste(model, "E1");
expect(getDataValidationRules(model, "sheet2")).toMatchObject([
{ id: expect.any(String), criterion, ranges: ["C1:C5", "E1:E5"] },
]);
});
test("Can cut/paste part of data validation rule on same sheet", () => {
const criterion: DataValidationCriterion = { type: "textContains", values: ["1"] };
addDataValidation(model, "A1:A5", "id", criterion);
cut(model, "A4");
paste(model, "C1");
expect(getDataValidationRules(model, sheetId)).toMatchObject([
{ id: "id", criterion, ranges: ["A1:A3", "A5", "C1"] },
]);
});
test("Can cut/paste whole data validation rule on same sheet", () => {
const criterion: DataValidationCriterion = { type: "textContains", values: ["1"] };
addDataValidation(model, "A1:A5", "id", criterion);
cut(model, "A1:A5");
paste(model, "C1");
expect(getDataValidationRules(model, sheetId)).toMatchObject([
{ id: "id", criterion, ranges: ["C1:C5"] },
]);
});
test("Can cut/paste part of data validation rule on another sheet", () => {
const criterion: DataValidationCriterion = { type: "textContains", values: ["1"] };
addDataValidation(model, "A1:A5", "id", criterion);
cut(model, "A4");
createSheet(model, { sheetId: "sheet2", activate: true });
paste(model, "C1");
expect(getDataValidationRules(model, sheetId)).toMatchObject([
{ id: "id", criterion, ranges: ["A1:A3", "A5"] },
]);
expect(getDataValidationRules(model, "sheet2")).toMatchObject([
{ id: expect.any(String), criterion, ranges: ["C1"] },
]);
});
test("Can cut/paste whole validation rule on another sheet", () => {
const criterion: DataValidationCriterion = { type: "textContains", values: ["1"] };
addDataValidation(model, "A1:A5", "id", criterion);
cut(model, "A1:A5");
createSheet(model, { sheetId: "sheet2", activate: true });
paste(model, "C1");
expect(getDataValidationRules(model, sheetId)).toEqual([]);
expect(getDataValidationRules(model, "sheet2")).toMatchObject([
{ id: expect.any(String), criterion, ranges: ["C1:C5"] },
]);
});
test("Paste as value or paste format only don't paste data validation", () => {
const criterion: DataValidationCriterion = { type: "textContains", values: ["1"] };
addDataValidation(model, "A1:A5", "id", criterion);
copy(model, "A1:A5");
paste(model, "C1", "onlyFormat");
expect(getDataValidationRules(model, sheetId)).toMatchObject([
{ id: "id", criterion, ranges: ["A1:A5"] },
]);
paste(model, "C1", "asValue");
expect(getDataValidationRules(model, sheetId)).toMatchObject([
{ id: "id", criterion, ranges: ["A1:A5"] },
]);
});
test("copy paste DV in another sheet => change DV => copy paste again doesnt overwrite the previously pasted DV", () => {
const model = new Model();
createSheet(model, { sheetId: "sheet2" });
const sheet1Id = model.getters.getSheetIds()[0];
const sheet2Id = model.getters.getSheetIds()[1];
addDataValidation(model, "A1", "id", { type: "textContains", values: ["1"] });
copy(model, "A1");
activateSheet(model, sheet2Id);
paste(model, "A1");
expect(getDataValidationRules(model, sheet2Id)).toMatchObject([
{ criterion: { values: ["1"] }, ranges: ["A1"] },
]);
activateSheet(model, sheet1Id);
addDataValidation(model, "A1", "id", { type: "textContains", values: ["5"] });
copy(model, "A1");
activateSheet(model, sheet2Id);
paste(model, "B2");
expect(getDataValidationRules(model, sheet2Id)).toMatchObject([
{ criterion: { values: ["1"] }, ranges: ["A1"] },
{ criterion: { values: ["5"] }, ranges: ["B2"] },
]);
});
test("Can copy/paste empty cell to clear data validation rule", () => {
const criterion: DataValidationCriterion = { type: "textContains", values: ["1"] };
addDataValidation(model, "A1:A5", "id", criterion);
copy(model, "A6");
paste(model, "A1");
expect(getDataValidationRules(model, sheetId)).toMatchObject([
{ id: expect.any(String), criterion, ranges: ["A2:A5"] },
]);
});
test("copy cells with DV => delete original DV => paste => should paste cells with copied DV", () => {
const criterion: DataValidationCriterion = { type: "textContains", values: ["1"] };
addDataValidation(model, "A1:A5", "id", criterion);
copy(model, "A1:A5");
removeDataValidation(model, "id");
paste(model, "C1");
expect(getDataValidationRules(model, sheetId)).toMatchObject([
{ id: "id", criterion, ranges: ["C1:C5"] },
]);
});
test("copy zone with independant multiple DV => delete original DV => paste => should paste cells with copied DV", () => {
const criterion1: DataValidationCriterion = { type: "textContains", values: ["1"] };
const criterion2: DataValidationCriterion = { type: "textContains", values: ["2"] };
addDataValidation(model, "A1:A5", "id1", criterion1);
addDataValidation(model, "C1:C5", "id2", criterion2);
copy(model, "A1:C5");
removeDataValidation(model, "id1");
removeDataValidation(model, "id2");
paste(model, "E1");
expect(getDataValidationRules(model, sheetId)).toMatchObject([
{ id: "id1", criterion: criterion1, ranges: ["E1:E5"] },
{ id: "id2", criterion: criterion2, ranges: ["G1:G5"] },
]);
});
test("copy zone with independant multiple DV => delete origin sheet => paste => should paste cells with copied DV", () => {
const criterion1: DataValidationCriterion = { type: "textContains", values: ["1"] };
const criterion2: DataValidationCriterion = { type: "textContains", values: ["2"] };
addDataValidation(model, "A1:A5", "id1", criterion1);
addDataValidation(model, "C1:C5", "id2", criterion2);
copy(model, "A1:C5");
createSheet(model, { sheetId: "Sheet2" });
deleteSheet(model, sheetId);
paste(model, "E1");
expect(getDataValidationRules(model, "Sheet2")).toMatchObject([
{ criterion: criterion1, ranges: ["E1:E5"] },
{ criterion: criterion2, ranges: ["G1:G5"] },
]);
});
test("copy/paste a DV zone only dispatch a singled ADD_DATA_VALIDATION_RULE", () => {
const commands: Command[] = [];
class MyUIPlugin extends UIPlugin {
handle = (cmd: Command) => commands.push(cmd);
}
addTestPlugin(featurePluginRegistry, MyUIPlugin);
const model = new Model({ sheets: [{ colNumber: 5, rowNumber: 5 }] });
const sheetId = model.getters.getActiveSheetId();
addDataValidation(model, "A1:A2", "id", { type: "textContains", values: ["1"] });
copy(model, "A1:A2");
paste(model, "B1");
expect(getDataValidationRules(model, sheetId)).toMatchObject([{ ranges: ["A1:B2"] }]);
expect(commands.filter((c) => c.type === "ADD_DATA_VALIDATION_RULE")).toHaveLength(2);
});
});