Skip to content

Commit d981603

Browse files
author
Marco Franceschi
committed
chore: Included unit tests for matchAny and matchAll operators
1 parent a01c65c commit d981603

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/rules-engine/operators/regex.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
if (Array.isArray(a)) {
77
const result = a
88
.map(value =>
9-
typeof value === 'string' ? !new RegExp(b).test(value) : undefined
9+
typeof value === 'string' ? new RegExp(b).test(value) : undefined
1010
)
1111
.some(v => v)
1212
return result
@@ -18,7 +18,7 @@ export default {
1818
if (Array.isArray(a)) {
1919
const result = a
2020
.map(value =>
21-
typeof value === 'string' ? !new RegExp(b).test(value) : undefined
21+
typeof value === 'string' ? new RegExp(b).test(value) : undefined
2222
)
2323
.every(v => v)
2424
return result

tests/operators/regex.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,50 @@ describe('Regex Operators', () => {
3333
).toBeFalsy()
3434
})
3535
})
36+
37+
describe('matchAny Operator', () => {
38+
test('Should fail given an invalid array', () => {
39+
expect(
40+
RegexOperators.matchAny('john.doe', /[\w\d.-]+@[\w\d.-]+\.[\w\d.-]+/)
41+
).toBeFalsy()
42+
})
43+
44+
test('Should fail given an array with invalid emails', () => {
45+
expect(
46+
RegexOperators.matchAny(['john.doe'], /[\w\d.-]+@[\w\d.-]+\.[\w\d.-]+/)
47+
).toBeFalsy()
48+
})
49+
50+
test('Should pass given at least one valid email', () => {
51+
expect(
52+
RegexOperators.matchAny(
53+
['john.doe', 'matt.dAvella@autocloud.dev'],
54+
/[\w\d.-]+@[\w\d.-]+\.[\w\d.-]+/
55+
)
56+
).toBeTruthy()
57+
})
58+
})
59+
60+
describe('matchAll Operator', () => {
61+
test('Should fail given an invalid array', () => {
62+
expect(
63+
RegexOperators.matchAll('john.doe', /[\w\d.-]+@[\w\d.-]+\.[\w\d.-]+/)
64+
).toBeFalsy()
65+
})
66+
67+
test('Should fail given an array with invalid emails', () => {
68+
expect(
69+
RegexOperators.matchAll(['john.doe'], /[\w\d.-]+@[\w\d.-]+\.[\w\d.-]+/)
70+
).toBeFalsy()
71+
})
72+
73+
test('Should fail given an array of valid emails', () => {
74+
expect(
75+
RegexOperators.matchAll(
76+
['john.doe@autocloud.dev', 'matt.dAvella@autocloud.dev'],
77+
/[\w\d.-]+@[\w\d.-]+\.[\w\d.-]+/
78+
)
79+
).toBeTruthy()
80+
})
81+
})
3682
})

0 commit comments

Comments
 (0)