|
1
|
import { RuleOperator } from "@medusajs/framework/utils" |
|
2
|
import { isContextValid } from "../utils" |
|
3
|
|
|
4
|
describe("isContextValidForRules", () => { |
|
5
|
const context = { |
|
6
|
attribute1: "value1", |
|
7
|
attribute2: "value2", |
|
8
|
attribute3: "value3", |
|
9
|
} |
|
10
|
|
|
11
|
const validRule = { |
|
12
|
attribute: "attribute1", |
|
13
|
operator: RuleOperator.EQ, |
|
14
|
value: "value1", |
|
15
|
} |
|
16
|
|
|
17
|
const invalidRule = { |
|
18
|
attribute: "attribute2", |
|
19
|
operator: RuleOperator.EQ, |
|
20
|
value: "wrongValue", |
|
21
|
} |
|
22
|
|
|
23
|
it("returns true when all rules are valid", () => { |
|
24
|
const rules = [validRule, validRule] |
|
25
|
expect(isContextValid(context, rules)).toBe(true) |
|
26
|
}) |
|
27
|
|
|
28
|
it("returns true when some rules are valid", () => { |
|
29
|
const rules = [validRule, validRule] |
|
30
|
const options = { someAreValid: true } |
|
31
|
expect(isContextValid(context, rules, options)).toBe(true) |
|
32
|
}) |
|
33
|
|
|
34
|
it("returns true when some rules are valid and someAreValid is true", () => { |
|
35
|
const rules = [validRule, invalidRule] |
|
36
|
const options = { someAreValid: true } |
|
37
|
expect(isContextValid(context, rules, options)).toBe(true) |
|
38
|
}) |
|
39
|
|
|
40
|
it("returns false when some rules are valid", () => { |
|
41
|
const rules = [validRule, invalidRule] |
|
42
|
expect(isContextValid(context, rules)).toBe(false) |
|
43
|
}) |
|
44
|
|
|
45
|
it("returns false when no rules are valid and someAreValid is true", () => { |
|
46
|
const rules = [invalidRule, invalidRule] |
|
47
|
const options = { someAreValid: true } |
|
48
|
expect(isContextValid(context, rules, options)).toBe(false) |
|
49
|
}) |
|
50
|
|
|
51
|
it("returns false when no rules are valid", () => { |
|
52
|
const rules = [invalidRule, invalidRule] |
|
53
|
expect(isContextValid(context, rules)).toBe(false) |
|
54
|
}) |
|
55
|
|
|
56
|
it("returns true when the 'gt' operator is valid", () => { |
|
57
|
const rules = [ |
|
58
|
{ |
|
59
|
attribute: "attribute1", |
|
60
|
operator: RuleOperator.GT, |
|
61
|
value: "1", // 2 > 1 |
|
62
|
}, |
|
63
|
] |
|
64
|
const context = { attribute1: "2" } |
|
65
|
expect(isContextValid(context, rules)).toBe(true) |
|
66
|
}) |
|
67
|
|
|
68
|
it("returns false when the 'gt' operator is invalid", () => { |
|
69
|
const rules = [ |
|
70
|
{ |
|
71
|
attribute: "attribute1", |
|
72
|
operator: RuleOperator.GT, |
|
73
|
value: "0", // 0 > 0 |
|
74
|
}, |
|
75
|
] |
|
76
|
const context = { attribute1: "0" } |
|
77
|
expect(isContextValid(context, rules)).toBe(false) |
|
78
|
}) |
|
79
|
|
|
80
|
it("returns true when the 'gte' operator is valid", () => { |
|
81
|
const rules = [ |
|
82
|
{ |
|
83
|
attribute: "attribute1", |
|
84
|
operator: RuleOperator.GTE, |
|
85
|
value: "2", // 2 >= 2 |
|
86
|
}, |
|
87
|
] |
|
88
|
const context = { attribute1: "2" } |
|
89
|
expect(isContextValid(context, rules)).toBe(true) |
|
90
|
}) |
|
91
|
|
|
92
|
it("returns false when the 'gte' operator is invalid", () => { |
|
93
|
const rules = [ |
|
94
|
{ |
|
95
|
attribute: "attribute1", |
|
96
|
operator: RuleOperator.GTE, |
|
97
|
value: "3", // 2 >= 3 |
|
98
|
}, |
|
99
|
] |
|
100
|
const context = { attribute1: "2" } |
|
101
|
expect(isContextValid(context, rules)).toBe(false) |
|
102
|
}) |
|
103
|
|
|
104
|
it("returns true when the 'lt' operator is valid", () => { |
|
105
|
const rules = [ |
|
106
|
{ |
|
107
|
attribute: "attribute1", |
|
108
|
operator: RuleOperator.LT, |
|
109
|
value: "3", // 2 < 3 |
|
110
|
}, |
|
111
|
] |
|
112
|
const context = { attribute1: "2" } |
|
113
|
expect(isContextValid(context, rules)).toBe(true) |
|
114
|
}) |
|
115
|
|
|
116
|
it("returns false when the 'lt' operator is invalid", () => { |
|
117
|
const rules = [ |
|
118
|
{ |
|
119
|
attribute: "attribute1", |
|
120
|
operator: RuleOperator.LT, |
|
121
|
value: "2", // 2 < 2 |
|
122
|
}, |
|
123
|
] |
|
124
|
const context = { attribute1: "2" } |
|
125
|
expect(isContextValid(context, rules)).toBe(false) |
|
126
|
}) |
|
127
|
|
|
128
|
it("returns true when the 'lte' operator is valid", () => { |
|
129
|
const rules = [ |
|
130
|
{ |
|
131
|
attribute: "attribute1", |
|
132
|
operator: RuleOperator.LTE, |
|
133
|
value: "2", // 2 <= 2 |
|
134
|
}, |
|
135
|
] |
|
136
|
const context = { attribute1: "2" } |
|
137
|
expect(isContextValid(context, rules)).toBe(true) |
|
138
|
}) |
|
139
|
|
|
140
|
// ... existing tests ... |
|
141
|
|
|
142
|
it("returns false when the 'lte' operator is invalid", () => { |
|
143
|
const rules = [ |
|
144
|
{ |
|
145
|
attribute: "attribute1", |
|
146
|
operator: RuleOperator.LTE, |
|
147
|
value: "1", // 2 <= 1 |
|
148
|
}, |
|
149
|
] |
|
150
|
const context = { attribute1: "2" } |
|
151
|
expect(isContextValid(context, rules)).toBe(false) |
|
152
|
}) |
|
153
|
|
|
154
|
it("returns true when the 'lt' operator compares numeric rule values", () => { |
|
155
|
const rules = [ |
|
156
|
{ |
|
157
|
attribute: "total_weight", |
|
158
|
operator: RuleOperator.LT, |
|
159
|
value: "2000", // 40 < 2000 |
|
160
|
}, |
|
161
|
] |
|
162
|
const context = { total_weight: "40" } |
|
163
|
expect(isContextValid(context, rules)).toBe(true) |
|
164
|
}) |
|
165
|
|
|
166
|
it("returns true when the 'gte' operator compares numeric rule values", () => { |
|
167
|
const rules = [ |
|
168
|
{ |
|
169
|
attribute: "item_total", |
|
170
|
operator: RuleOperator.GTE, |
|
171
|
value: "50", // 100 >= 50 |
|
172
|
}, |
|
173
|
] |
|
174
|
const context = { item_total: "100" } |
|
175
|
expect(isContextValid(context, rules)).toBe(true) |
|
176
|
}) |
|
177
|
|
|
178
|
it("returns false when the 'gte' operator compares numeric rule values", () => { |
|
179
|
const rules = [ |
|
180
|
{ |
|
181
|
attribute: "item_total", |
|
182
|
operator: RuleOperator.GTE, |
|
183
|
value: "50", // 49 >= 50 |
|
184
|
}, |
|
185
|
] |
|
186
|
const context = { item_total: "49" } |
|
187
|
expect(isContextValid(context, rules)).toBe(false) |
|
188
|
}) |
|
189
|
|
|
190
|
it("returns true when the 'gt' operator compares date rule values", () => { |
|
191
|
const rules = [ |
|
192
|
{ |
|
193
|
attribute: "created_at", |
|
194
|
operator: RuleOperator.GT, |
|
195
|
value: "2024-01-01", // 2025-06-01 > 2024-01-01 |
|
196
|
}, |
|
197
|
] |
|
198
|
const context = { created_at: "2025-06-01" } |
|
199
|
expect(isContextValid(context, rules)).toBe(true) |
|
200
|
}) |
|
201
|
|
|
202
|
it("returns false when the 'gt' operator compares date rule values", () => { |
|
203
|
const rules = [ |
|
204
|
{ |
|
205
|
attribute: "created_at", |
|
206
|
operator: RuleOperator.GT, |
|
207
|
value: "2024-01-01", // 2023-06-01 > 2024-01-01 |
|
208
|
}, |
|
209
|
] |
|
210
|
const context = { created_at: "2023-06-01" } |
|
211
|
expect(isContextValid(context, rules)).toBe(false) |
|
212
|
}) |
|
213
|
|
|
214
|
it("returns true when the 'in' operator is valid", () => { |
|
215
|
const rules = [ |
|
216
|
{ |
|
217
|
attribute: "attribute1", |
|
218
|
operator: RuleOperator.IN, |
|
219
|
value: ["1", "2", "3"], // 2 in [1, 2, 3] |
|
220
|
}, |
|
221
|
] |
|
222
|
const context = { attribute1: "2" } |
|
223
|
expect(isContextValid(context, rules)).toBe(true) |
|
224
|
}) |
|
225
|
|
|
226
|
it("returns false when the 'in' operator is invalid", () => { |
|
227
|
const rules = [ |
|
228
|
{ |
|
229
|
attribute: "attribute1", |
|
230
|
operator: RuleOperator.IN, |
|
231
|
value: ["1", "3", "4"], // 2 in [1, 3, 4] |
|
232
|
}, |
|
233
|
] |
|
234
|
const context = { attribute1: "2" } |
|
235
|
expect(isContextValid(context, rules)).toBe(false) |
|
236
|
}) |
|
237
|
|
|
238
|
it("returns true when the 'nin' operator is valid", () => { |
|
239
|
const rules = [ |
|
240
|
{ |
|
241
|
attribute: "attribute1", |
|
242
|
operator: RuleOperator.NIN, |
|
243
|
value: ["1", "3", "4"], // 2 not in [1, 3, 4] |
|
244
|
}, |
|
245
|
] |
|
246
|
const context = { attribute1: "2" } |
|
247
|
expect(isContextValid(context, rules)).toBe(true) |
|
248
|
}) |
|
249
|
|
|
250
|
it("returns false when the 'nin' operator is invalid", () => { |
|
251
|
const rules = [ |
|
252
|
{ |
|
253
|
attribute: "attribute1", |
|
254
|
operator: RuleOperator.NIN, |
|
255
|
value: ["1", "2", "3"], // 2 not in [1, 2, 3] |
|
256
|
}, |
|
257
|
] |
|
258
|
const context = { attribute1: "2" } |
|
259
|
expect(isContextValid(context, rules)).toBe(false) |
|
260
|
}) |
|
261
|
|
|
262
|
it("returns true when the 'ne' operator is valid", () => { |
|
263
|
const rules = [ |
|
264
|
{ |
|
265
|
attribute: "attribute1", |
|
266
|
operator: RuleOperator.NE, |
|
267
|
value: "1", // 2 != 1 |
|
268
|
}, |
|
269
|
] |
|
270
|
const context = { attribute1: "2" } |
|
271
|
expect(isContextValid(context, rules)).toBe(true) |
|
272
|
}) |
|
273
|
|
|
274
|
it("returns false when the 'ne' operator is invalid", () => { |
|
275
|
const rules = [ |
|
276
|
{ |
|
277
|
attribute: "attribute1", |
|
278
|
operator: RuleOperator.NE, |
|
279
|
value: "2", // 2 != 2 |
|
280
|
}, |
|
281
|
] |
|
282
|
const context = { attribute1: "2" } |
|
283
|
expect(isContextValid(context, rules)).toBe(false) |
|
284
|
}) |
|
285
|
|
|
286
|
it("returns true when the 'eq' operator is valid", () => { |
|
287
|
const rules = [ |
|
288
|
{ |
|
289
|
attribute: "attribute1", |
|
290
|
operator: RuleOperator.EQ, |
|
291
|
value: "2", // 2 == 2 |
|
292
|
}, |
|
293
|
] |
|
294
|
const context = { attribute1: "2" } |
|
295
|
expect(isContextValid(context, rules)).toBe(true) |
|
296
|
}) |
|
297
|
|
|
298
|
it("returns false when the 'eq' operator is invalid", () => { |
|
299
|
const rules = [ |
|
300
|
{ |
|
301
|
attribute: "attribute1", |
|
302
|
operator: RuleOperator.EQ, |
|
303
|
value: "1", // 2 == 1 |
|
304
|
}, |
|
305
|
] |
|
306
|
const context = { attribute1: "2" } |
|
307
|
expect(isContextValid(context, rules)).toBe(false) |
|
308
|
}) |
|
309
|
}) |