Skip to content

Commit ae13e2f

Browse files
committed
test[isReadonlyOf]: test with isObjectOf with symbol properties
1 parent f12da7f commit ae13e2f

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

is/__snapshots__/readonly_of_test.ts.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,25 @@ snapshot[`isReadonlyOf<T> > with isTupleOf > returns properly named predicate fu
4545
snapshot[`isReadonlyOf<T> > with isUniformTupleOf > returns properly named predicate function 1`] = `"isReadonlyOf(isUniformTupleOf(3, isNumber))"`;
4646
4747
snapshot[`isReadonlyOf<T> > with isUniformTupleOf > returns properly named predicate function 2`] = `"isReadonlyOf(isUniformTupleOf(3, isNumber))"`;
48+
49+
snapshot[`isReadonlyOf<T> > with symbol properties > with isObjectOf > returns properly named predicate function 1`] = `
50+
"isReadonlyOf(isObjectOf({
51+
a: isNumber,
52+
Symbol(b): isUnionOf([
53+
isString,
54+
isUndefined
55+
]),
56+
Symbol(c): asReadonly(isBoolean)
57+
}))"
58+
`;
59+
60+
snapshot[`isReadonlyOf<T> > with symbol properties > with isObjectOf > returns properly named predicate function 2`] = `
61+
"isReadonlyOf(isObjectOf({
62+
a: isNumber,
63+
Symbol(b): isUnionOf([
64+
isString,
65+
isUndefined
66+
]),
67+
Symbol(c): asReadonly(isBoolean)
68+
}))"
69+
`;

is/readonly_of_test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,52 @@ Deno.test("isReadonlyOf<T>", async (t) => {
170170
}
171171
});
172172
});
173+
174+
await t.step("with symbol properties", async (t) => {
175+
await t.step("with isObjectOf", async (t) => {
176+
const b = Symbol("b");
177+
const c = Symbol("c");
178+
const pred = is.ObjectOf({
179+
a: is.Number,
180+
[b]: is.UnionOf([is.String, is.Undefined]),
181+
[c]: as.Readonly(is.Boolean),
182+
});
183+
await t.step("returns properly named predicate function", async (t) => {
184+
await assertSnapshot(t, isReadonlyOf(pred).name);
185+
await assertSnapshot(t, isReadonlyOf(isReadonlyOf(pred)).name);
186+
});
187+
188+
await t.step("returns true on Readonly<T> object", () => {
189+
assertEquals(
190+
isReadonlyOf(pred)({ a: 0, [b]: "b", [c]: true } as const),
191+
true,
192+
);
193+
assertEquals(
194+
isReadonlyOf(pred)({ a: 0, [b]: "b", [c]: true }),
195+
true,
196+
);
197+
});
198+
199+
await t.step("returns false on non Readonly<T> object", () => {
200+
assertEquals(isReadonlyOf(pred)("a"), false, "Value is not an object");
201+
assertEquals(
202+
isReadonlyOf(pred)({ a: 0, [b]: "a", [c]: "" }),
203+
false,
204+
"Object have a different type property",
205+
);
206+
});
207+
208+
await t.step("predicated type is correct", () => {
209+
const a: unknown = { a: 0, [b]: "a", [c]: true };
210+
if (isReadonlyOf(pred)(a)) {
211+
assertType<
212+
Equal<
213+
typeof a,
214+
Readonly<{ a: number; [b]: string | undefined; [c]: boolean }>
215+
>
216+
>(true);
217+
}
218+
});
219+
});
220+
});
173221
});

0 commit comments

Comments
 (0)