fix: Match state trigger/condition semantics to HA (#2565)
* Closes #2530
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
allPromises,
|
||||
arefloatsApproximatelyEqual,
|
||||
arrayify,
|
||||
arrayifyWithFalsy,
|
||||
arrayMove,
|
||||
aspectRatioToStyle,
|
||||
contentsChanged,
|
||||
@@ -69,6 +70,22 @@ describe('arrayify', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('arrayifyWithFalsy', () => {
|
||||
it('should wrap a non-array in an array', () => {
|
||||
expect(arrayifyWithFalsy(1)).toEqual([1]);
|
||||
});
|
||||
it('should return an existing array unchanged', () => {
|
||||
const data = [1, 2, 3];
|
||||
expect(arrayifyWithFalsy(data)).toBe(data);
|
||||
});
|
||||
it('should preserve falsy values instead of dropping them', () => {
|
||||
expect(arrayifyWithFalsy(0)).toEqual([0]);
|
||||
expect(arrayifyWithFalsy(false)).toEqual([false]);
|
||||
expect(arrayifyWithFalsy('')).toEqual(['']);
|
||||
expect(arrayifyWithFalsy(null)).toEqual([null]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setify', () => {
|
||||
it('should convert non set to set', () => {
|
||||
expect(setify(1)).toEqual(new Set([1]));
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { forwardIssues } from '../../../src/utils/zod/forward-issues';
|
||||
|
||||
// Parse `value` through a schema whose `superRefine` delegates to
|
||||
// `forwardIssues`, returning the resulting error (or null on success).
|
||||
const runThrough = (
|
||||
value: unknown,
|
||||
target: z.ZodType,
|
||||
path?: PropertyKey[],
|
||||
): z.ZodError | null => {
|
||||
const result = z
|
||||
.unknown()
|
||||
.superRefine((v, ctx) => forwardIssues(ctx, v, target, path))
|
||||
.safeParse(value);
|
||||
return result.success ? null : result.error;
|
||||
};
|
||||
|
||||
describe('forwardIssues', () => {
|
||||
it('should add no issues when the value satisfies the schema', () => {
|
||||
expect(runThrough('on', z.string())).toBeNull();
|
||||
});
|
||||
|
||||
it('should forward issues with the given path prefix', () => {
|
||||
const error = runThrough(5, z.string(), ['field']);
|
||||
expect(error?.issues).toHaveLength(1);
|
||||
expect(error?.issues[0].path).toEqual(['field']);
|
||||
});
|
||||
|
||||
it('should forward at the issue path when no prefix is given', () => {
|
||||
const error = runThrough(5, z.string());
|
||||
expect(error?.issues[0].path).toEqual([]);
|
||||
});
|
||||
|
||||
it('should prepend the prefix to nested issue paths', () => {
|
||||
const error = runThrough({ inner: 5 }, z.object({ inner: z.string() }), ['outer']);
|
||||
expect(error?.issues[0].path).toEqual(['outer', 'inner']);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user