|
| 1 | +import assert from 'node:assert'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | +import { s } from '../index.ts'; |
| 4 | + |
| 5 | +describe('s.function()', () => { |
| 6 | + // ─── valid inputs ────────────────────────────────────────────────────────── |
| 7 | + it('passes an arrow function', () => { |
| 8 | + const fn = () => {}; |
| 9 | + assert.strictEqual(s.function().parse(fn), fn); |
| 10 | + }); |
| 11 | + |
| 12 | + it('passes a named function', () => { |
| 13 | + function handler() {} |
| 14 | + assert.strictEqual(s.function().parse(handler), handler); |
| 15 | + }); |
| 16 | + |
| 17 | + it('passes an async function', () => { |
| 18 | + const fn = async () => {}; |
| 19 | + assert.strictEqual(s.function().parse(fn), fn); |
| 20 | + }); |
| 21 | + |
| 22 | + it('passes a function with arguments', () => { |
| 23 | + const fn = (a, b) => a + b; |
| 24 | + assert.strictEqual(s.function().parse(fn), fn); |
| 25 | + }); |
| 26 | + |
| 27 | + it('passes a class constructor (callable)', () => { |
| 28 | + class MyClass {} |
| 29 | + assert.strictEqual(s.function().parse(MyClass), MyClass); |
| 30 | + }); |
| 31 | + |
| 32 | + // ─── invalid inputs ──────────────────────────────────────────────────────── |
| 33 | + it('throws for a string', () => |
| 34 | + assert.throws(() => s.function().parse('hello'))); |
| 35 | + |
| 36 | + it('throws for a number', () => assert.throws(() => s.function().parse(42))); |
| 37 | + |
| 38 | + it('throws for null', () => assert.throws(() => s.function().parse(null))); |
| 39 | + |
| 40 | + it('throws for undefined', () => |
| 41 | + assert.throws(() => s.function().parse(undefined))); |
| 42 | + |
| 43 | + it('throws for a plain object', () => |
| 44 | + assert.throws(() => s.function().parse({ call: () => {} }))); |
| 45 | + |
| 46 | + it('throws for an array', () => assert.throws(() => s.function().parse([]))); |
| 47 | + |
| 48 | + it('throws for a boolean', () => |
| 49 | + assert.throws(() => s.function().parse(true))); |
| 50 | + |
| 51 | + // ─── error message ───────────────────────────────────────────────────────── |
| 52 | + it('default error message contains the invalid value', () => { |
| 53 | + const result = s.function().safeParse('nope'); |
| 54 | + assert.equal(result.success, false); |
| 55 | + assert.ok(result.error.message.includes('nope')); |
| 56 | + }); |
| 57 | + |
| 58 | + it('supports custom message as string', () => { |
| 59 | + const schema = s.function({ message: 'Expected a callback' }); |
| 60 | + const result = schema.safeParse(42); |
| 61 | + assert.equal(result.success, false); |
| 62 | + assert.equal(result.error.message, 'Expected a callback'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('supports custom message as function', () => { |
| 66 | + const schema = s.function({ message: (v) => `"${v}" is not a function` }); |
| 67 | + const result = schema.safeParse(42); |
| 68 | + assert.equal(result.success, false); |
| 69 | + assert.equal(result.error.message, '"42" is not a function'); |
| 70 | + }); |
| 71 | + |
| 72 | + // ─── modifiers ───────────────────────────────────────────────────────────── |
| 73 | + it('.optional() allows undefined', () => { |
| 74 | + const schema = s.function().optional(); |
| 75 | + assert.equal(schema.parse(undefined), undefined); |
| 76 | + const fn = () => {}; |
| 77 | + assert.strictEqual(schema.parse(fn), fn); |
| 78 | + }); |
| 79 | + |
| 80 | + it('.nullable() allows null', () => { |
| 81 | + const schema = s.function().nullable(); |
| 82 | + assert.equal(schema.parse(null), null); |
| 83 | + const fn = () => {}; |
| 84 | + assert.strictEqual(schema.parse(fn), fn); |
| 85 | + }); |
| 86 | + |
| 87 | + it('.refine() chains correctly', () => { |
| 88 | + const schema = s.function().refine((fn) => fn.length === 2, { |
| 89 | + message: 'Must accept exactly 2 arguments', |
| 90 | + }); |
| 91 | + assert.ok(schema.parse((a, b) => a + b)); |
| 92 | + assert.throws(() => schema.parse(() => {})); |
| 93 | + }); |
| 94 | + |
| 95 | + // ─── inside s.object() ───────────────────────────────────────────────────── |
| 96 | + it('works as a field inside s.object()', () => { |
| 97 | + const schema = s.object({ |
| 98 | + name: s.string(), |
| 99 | + onClick: s.function(), |
| 100 | + }); |
| 101 | + const fn = () => {}; |
| 102 | + const result = schema.parse({ name: 'btn', onClick: fn }); |
| 103 | + assert.equal(result.name, 'btn'); |
| 104 | + assert.strictEqual(result.onClick, fn); |
| 105 | + }); |
| 106 | + |
| 107 | + it('fails inside s.object() when field is not a function', () => { |
| 108 | + const schema = s.object({ |
| 109 | + name: s.string(), |
| 110 | + onClick: s.function(), |
| 111 | + }); |
| 112 | + assert.throws(() => schema.parse({ name: 'btn', onClick: 'not-a-fn' })); |
| 113 | + }); |
| 114 | + |
| 115 | + it('safeParse returns success:false for invalid function field', () => { |
| 116 | + const schema = s.object({ handler: s.function() }); |
| 117 | + const result = schema.safeParse({ handler: null }); |
| 118 | + assert.equal(result.success, false); |
| 119 | + }); |
| 120 | +}); |
0 commit comments