Skip to content

Commit 7fd076b

Browse files
cexbrayatthePunderWoman
authored andcommitted
test(forms): improve test coverage for multiple pattern validators
Add comprehensive test cases to validate behavior when multiple pattern validators are applied to the same field. These tests clarify that multiple patterns operate with AND logic, where each pattern is validated independently and produces its own error when it doesn't match.
1 parent 1b4dcc0 commit 7fd076b

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

packages/forms/signals/test/node/api/validators/pattern.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,57 @@ describe('pattern validator', () => {
9595
expect(f.name().pattern()).toEqual([/pir.*jok/, /pelmeni/]);
9696
});
9797

98+
it('validates multiple patterns independently (AND logic)', () => {
99+
const model = signal('abc123');
100+
const f = form(
101+
model,
102+
(p) => {
103+
pattern(p, /abc/); // matches
104+
pattern(p, /\d+/); // matches
105+
},
106+
{injector: TestBed.inject(Injector)},
107+
);
108+
109+
// Both patterns match, so no errors
110+
expect(f().pattern()).toEqual([/abc/, /\d+/]);
111+
expect(f().errors()).toEqual([]);
112+
});
113+
114+
it('validates multiple patterns independently - partial match produces errors', () => {
115+
const model = signal('abc');
116+
const f = form(
117+
model,
118+
(p) => {
119+
pattern(p, /abc/); // matches
120+
pattern(p, /\d+/); // does not match
121+
},
122+
{injector: TestBed.inject(Injector)},
123+
);
124+
125+
// Only one pattern matches, so we get an error from the non-matching one
126+
expect(f().pattern()).toEqual([/abc/, /\d+/]);
127+
expect(f().errors()).toEqual([patternError(/\d+/, {fieldTree: f})]);
128+
});
129+
130+
it('validates multiple patterns - no match produces multiple errors', () => {
131+
const model = signal('xyz');
132+
const f = form(
133+
model,
134+
(p) => {
135+
pattern(p, /abc/); // does not match
136+
pattern(p, /\d+/); // does not match
137+
},
138+
{injector: TestBed.inject(Injector)},
139+
);
140+
141+
// No patterns match, so we get errors from both
142+
expect(f().pattern()).toEqual([/abc/, /\d+/]);
143+
expect(f().errors()).toEqual([
144+
patternError(/abc/, {fieldTree: f}),
145+
patternError(/\d+/, {fieldTree: f}),
146+
]);
147+
});
148+
98149
it('PATTERN property defaults to empty list', () => {
99150
const cat = signal({name: 'pelmeni-the-cat'});
100151
const f = form(

0 commit comments

Comments
 (0)