PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.3.53
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.3.53
1.4.17 1.4.16 1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 All 180 releases
disco / backend / views / components / Main / utilities / __tests__ / validateConditions.test.js

validateConditions.test.js in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.3.53, at backend/views/components/Main/utilities/__tests__/validateConditions.test.js

387 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { toast } from 'react-toastify';
2 import { validateConditions } from '../utilities';
3
4 jest.mock('react-toastify', () => ({
5 toast: {
6 error: jest.fn(),
7 },
8 }));
9
10 describe('validateConditions', () => {
11 afterEach(() => {
12 jest.clearAllMocks();
13 });
14
15 it('should return true when conditions is null', () => {
16 expect(validateConditions(null)).toBe(true);
17 expect(toast.error).not.toHaveBeenCalled();
18 });
19
20 it('should return true when conditions is undefined', () => {
21 expect(validateConditions(undefined)).toBe(true);
22 expect(toast.error).not.toHaveBeenCalled();
23 });
24
25 it('should return true when conditions is empty array', () => {
26 expect(validateConditions([])).toBe(true);
27 expect(toast.error).not.toHaveBeenCalled();
28 });
29
30 it('should return true when all conditions have valid string values', () => {
31 const conditions = [
32 {
33 id: '1',
34 base_operator: 'and',
35 base_filters: [
36 {
37 id: 'f1',
38 compare_with: 'customer_email',
39 condition: 'equals',
40 compare: 'test@example.com',
41 operator: 'and',
42 },
43 ],
44 },
45 ];
46
47 expect(validateConditions(conditions)).toBe(true);
48 expect(toast.error).not.toHaveBeenCalled();
49 });
50
51 it('should return true when condition has valid array value', () => {
52 const conditions = [
53 {
54 id: '1',
55 base_operator: 'and',
56 base_filters: [
57 {
58 id: 'f1',
59 compare_with: 'cart_total',
60 condition: 'between',
61 compare: ['100', '500'],
62 operator: 'and',
63 },
64 ],
65 },
66 ];
67
68 expect(validateConditions(conditions)).toBe(true);
69 expect(toast.error).not.toHaveBeenCalled();
70 });
71
72 it('should return false and show toast when compare_with is empty', () => {
73 const conditions = [
74 {
75 id: '1',
76 base_operator: 'and',
77 base_filters: [
78 {
79 id: 'f1',
80 compare_with: '',
81 condition: '',
82 compare: '',
83 operator: 'and',
84 },
85 ],
86 },
87 ];
88
89 expect(validateConditions(conditions)).toBe(false);
90 expect(toast.error).toHaveBeenCalledTimes(1);
91 expect(toast.error).toHaveBeenCalledWith(
92 'Please select a condition type or remove the empty condition.'
93 );
94 });
95
96 it('should return false and show toast when compare value is empty string', () => {
97 const conditions = [
98 {
99 id: '1',
100 base_operator: 'and',
101 base_filters: [
102 {
103 id: 'f1',
104 compare_with: 'customer_email',
105 condition: 'equals',
106 compare: '',
107 operator: 'and',
108 },
109 ],
110 },
111 ];
112
113 expect(validateConditions(conditions)).toBe(false);
114 expect(toast.error).toHaveBeenCalledTimes(1);
115 expect(toast.error).toHaveBeenCalledWith(
116 'Please fill in the condition value or remove the empty condition.'
117 );
118 });
119
120 it('should return false and show toast when compare value is undefined', () => {
121 const conditions = [
122 {
123 id: '1',
124 base_operator: 'and',
125 base_filters: [
126 {
127 id: 'f1',
128 compare_with: 'customer_email',
129 condition: 'equals',
130 compare: undefined,
131 operator: 'and',
132 },
133 ],
134 },
135 ];
136
137 expect(validateConditions(conditions)).toBe(false);
138 expect(toast.error).toHaveBeenCalledTimes(1);
139 expect(toast.error).toHaveBeenCalledWith(
140 'Please fill in the condition value or remove the empty condition.'
141 );
142 });
143
144 it('should return false and show toast when compare value is null', () => {
145 const conditions = [
146 {
147 id: '1',
148 base_operator: 'and',
149 base_filters: [
150 {
151 id: 'f1',
152 compare_with: 'customer_email',
153 condition: 'equals',
154 compare: null,
155 operator: 'and',
156 },
157 ],
158 },
159 ];
160
161 expect(validateConditions(conditions)).toBe(false);
162 expect(toast.error).toHaveBeenCalledTimes(1);
163 expect(toast.error).toHaveBeenCalledWith(
164 'Please fill in the condition value or remove the empty condition.'
165 );
166 });
167
168 it('should return false and show toast when compare is an empty array', () => {
169 const conditions = [
170 {
171 id: '1',
172 base_operator: 'and',
173 base_filters: [
174 {
175 id: 'f1',
176 compare_with: 'product_category',
177 condition: 'in_list',
178 compare: [],
179 operator: 'and',
180 },
181 ],
182 },
183 ];
184
185 expect(validateConditions(conditions)).toBe(false);
186 expect(toast.error).toHaveBeenCalledTimes(1);
187 expect(toast.error).toHaveBeenCalledWith(
188 'Please fill in the condition value or remove the empty condition.'
189 );
190 });
191
192 it('should return false and show toast when compare is array of empty strings', () => {
193 const conditions = [
194 {
195 id: '1',
196 base_operator: 'and',
197 base_filters: [
198 {
199 id: 'f1',
200 compare_with: 'cart_total',
201 condition: 'between',
202 compare: ['', ''],
203 operator: 'and',
204 },
205 ],
206 },
207 ];
208
209 expect(validateConditions(conditions)).toBe(false);
210 expect(toast.error).toHaveBeenCalledTimes(1);
211 expect(toast.error).toHaveBeenCalledWith(
212 'Please fill in the condition value or remove the empty condition.'
213 );
214 });
215
216 it('should return false and show toast when compare is array of null values', () => {
217 const conditions = [
218 {
219 id: '1',
220 base_operator: 'and',
221 base_filters: [
222 {
223 id: 'f1',
224 compare_with: 'cart_total',
225 condition: 'between',
226 compare: [null, null],
227 operator: 'and',
228 },
229 ],
230 },
231 ];
232
233 expect(validateConditions(conditions)).toBe(false);
234 expect(toast.error).toHaveBeenCalledTimes(1);
235 });
236
237 it('should return true when compare array has at least one valid value', () => {
238 const conditions = [
239 {
240 id: '1',
241 base_operator: 'and',
242 base_filters: [
243 {
244 id: 'f1',
245 compare_with: 'cart_total',
246 condition: 'between',
247 compare: ['100', ''],
248 operator: 'and',
249 },
250 ],
251 },
252 ];
253
254 expect(validateConditions(conditions)).toBe(true);
255 expect(toast.error).not.toHaveBeenCalled();
256 });
257
258 it('should validate multiple groups and fail on first invalid', () => {
259 const conditions = [
260 {
261 id: '1',
262 base_operator: 'and',
263 base_filters: [
264 {
265 id: 'f1',
266 compare_with: 'customer_email',
267 condition: 'equals',
268 compare: 'test@example.com',
269 operator: 'and',
270 },
271 ],
272 },
273 {
274 id: '2',
275 base_operator: 'and',
276 base_filters: [
277 {
278 id: 'f2',
279 compare_with: 'cart_total',
280 condition: 'equals',
281 compare: '',
282 operator: 'and',
283 },
284 ],
285 },
286 ];
287
288 expect(validateConditions(conditions)).toBe(false);
289 expect(toast.error).toHaveBeenCalledTimes(1);
290 });
291
292 it('should validate multiple filters within a group', () => {
293 const conditions = [
294 {
295 id: '1',
296 base_operator: 'and',
297 base_filters: [
298 {
299 id: 'f1',
300 compare_with: 'customer_email',
301 condition: 'equals',
302 compare: 'test@example.com',
303 operator: 'and',
304 },
305 {
306 id: 'f2',
307 compare_with: 'cart_total',
308 condition: 'equals',
309 compare: '',
310 operator: 'and',
311 },
312 ],
313 },
314 ];
315
316 expect(validateConditions(conditions)).toBe(false);
317 expect(toast.error).toHaveBeenCalledTimes(1);
318 });
319
320 it('should return true when group has no base_filters', () => {
321 const conditions = [
322 {
323 id: '1',
324 base_operator: 'and',
325 },
326 ];
327
328 expect(validateConditions(conditions)).toBe(true);
329 expect(toast.error).not.toHaveBeenCalled();
330 });
331
332 it('should return true when multiple groups all have valid values', () => {
333 const conditions = [
334 {
335 id: '1',
336 base_operator: 'and',
337 base_filters: [
338 {
339 id: 'f1',
340 compare_with: 'customer_email',
341 condition: 'equals',
342 compare: 'test@example.com',
343 operator: 'and',
344 },
345 ],
346 },
347 {
348 id: '2',
349 base_operator: 'or',
350 base_filters: [
351 {
352 id: 'f2',
353 compare_with: 'cart_total',
354 condition: 'greater_than',
355 compare: '50',
356 operator: 'and',
357 },
358 ],
359 },
360 ];
361
362 expect(validateConditions(conditions)).toBe(true);
363 expect(toast.error).not.toHaveBeenCalled();
364 });
365
366 it('should return true when compare is array with valid selections', () => {
367 const conditions = [
368 {
369 id: '1',
370 base_operator: 'and',
371 base_filters: [
372 {
373 id: 'f1',
374 compare_with: 'product_category',
375 condition: 'in_list',
376 compare: ['cat1', 'cat2'],
377 operator: 'and',
378 },
379 ],
380 },
381 ];
382
383 expect(validateConditions(conditions)).toBe(true);
384 expect(toast.error).not.toHaveBeenCalled();
385 });
386 });
387