angular/packages/forms/signals/test/node/path.spec.ts
Leon Senft 46dbd18566 refactor(forms): remove customError()
Remove the `customError` function and `CustomValidationError` type.

These were made obsolete by support for returning plain object literals
as custom errors.

This also catches few `field` properties that were missed in the
renaming to `fieldTree`.
2026-01-07 15:07:30 -05:00

94 lines
2.4 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {Injector, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {apply, applyEach, applyWhen, form, requiredError, validate} from '../../public_api';
describe('path', () => {
describe('Active path', () => {
it('Disallows using parent paths for applyWhen', () => {
const data = signal({first: '', needLastName: false, last: ''});
form(
data,
(path) => {
applyWhen(
path,
({value}) => value().needLastName,
(/* UNUSED */) => {
expect(() => {
validate(path.last, ({value}) =>
value().length > 0 ? undefined : requiredError(),
);
}).toThrowError();
},
);
},
{injector: TestBed.inject(Injector)},
);
});
it('Disallows using parent paths for apply', () => {
const data = signal({first: '', needLastName: false, last: ''});
form(
data,
(path) => {
apply(path, () => {
expect(() => {
validate(path.last, () => {
return {kind: 'custom'};
});
}).toThrowError();
});
},
{injector: TestBed.inject(Injector)},
);
});
it('Disallows using the same path', () => {
const data = signal({first: '', needLastName: false, last: ''});
form(
data,
(path) => {
apply(path, () => {
expect(() => {
validate(path, () => {
return {kind: 'custom'};
});
}).toThrowError();
});
},
{injector: TestBed.inject(Injector)},
);
});
it('Disallows using parent paths for apply', () => {
const data = signal({
needLastName: false,
items: [{first: '', last: ''}],
});
form(
data,
(path) => {
applyEach(path.items, () => {
expect(() => {
validate(path.needLastName, () => {
return {kind: 'custom'};
});
}).toThrowError();
});
},
{injector: TestBed.inject(Injector)},
);
});
});
});