|
| 1 | +import { randomUUID } from "crypto"; |
| 2 | +import { z } from "zod"; |
| 3 | + |
| 4 | +import { unsafe } from "../../behaviours/matchers"; |
| 5 | +import { when } from "../../behaviours"; |
| 6 | +import { Mock, resetCompletely } from "../../mocks"; |
| 7 | +import { m } from "../.."; |
| 8 | + |
| 9 | +test("test new zod comparison", () => { |
| 10 | + function toTest(params: any) {} |
| 11 | + |
| 12 | + const mock = Mock(toTest); |
| 13 | + expect(mock({ name: -1 })).toBe(undefined); |
| 14 | + |
| 15 | + when(mock) |
| 16 | + .isCalledWith({ name: m.validates(z.number().int().negative()) }) |
| 17 | + .thenReturn(unsafe(999)); |
| 18 | + |
| 19 | + expect(mock({ name: -1 })).toBe(999); |
| 20 | + |
| 21 | + resetCompletely(mock); |
| 22 | + |
| 23 | + when(mock) |
| 24 | + .isCalledWith({ |
| 25 | + x: m.validates(z.array(z.number().int().positive())), |
| 26 | + y: m.validates(z.array(z.number().int().negative())), |
| 27 | + z: 2, |
| 28 | + }) |
| 29 | + .thenReturn(unsafe("ding")); |
| 30 | + |
| 31 | + expect(mock({ x: [1, 2], y: [-1, -2], z: 2 })).toBe("ding"); |
| 32 | + expect(mock({ x: [1, 2], y: [-1, -2], z: 3 })).toBe(undefined); |
| 33 | + expect(mock({ x: ["Victor", "D"], y: [-1, -2], z: 2 })).toBe(undefined); |
| 34 | + expect(mock({ x: [1, 2], y: ["Victor", "D"], z: 2 })).toBe(undefined); |
| 35 | + |
| 36 | + resetCompletely(mock); |
| 37 | + |
| 38 | + when(mock) |
| 39 | + .isCalledWith({ |
| 40 | + x: m.validates(z.set(z.number().int().positive())), |
| 41 | + y: m.validates(z.set(z.number().int().negative())), |
| 42 | + z: 2, |
| 43 | + }) |
| 44 | + .thenReturn(unsafe("ding")); |
| 45 | + |
| 46 | + expect(mock({ x: new Set([1, 2]), y: new Set([-1, -2]), z: 2 })).toBe("ding"); |
| 47 | + expect(mock({ x: new Set(["V", "D"]), y: new Set([-1, -2]), z: 2 })).toBe( |
| 48 | + undefined |
| 49 | + ); |
| 50 | + expect(mock({ x: new Set([1, 2]), y: new Set(["V", "D"]), z: 2 })).toBe( |
| 51 | + undefined |
| 52 | + ); |
| 53 | + expect(mock({ x: new Set([1, 2]), y: new Set([-1, -2]), z: 3 })).toBe( |
| 54 | + undefined |
| 55 | + ); |
| 56 | + |
| 57 | + resetCompletely(mock); |
| 58 | + |
| 59 | + when(mock) |
| 60 | + .isCalledWith({ |
| 61 | + x: { |
| 62 | + y: { |
| 63 | + z: { |
| 64 | + a: { |
| 65 | + b: { |
| 66 | + c: m.validates(z.string().uuid()), |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }) |
| 73 | + .thenReturn(unsafe("ding")); |
| 74 | + |
| 75 | + expect(mock(1)).toBeUndefined(); |
| 76 | + expect(mock("v")).toBeUndefined(); |
| 77 | + expect(mock({})).toBeUndefined(); |
| 78 | + |
| 79 | + // this does not work |
| 80 | + expect( |
| 81 | + mock({ |
| 82 | + x: { |
| 83 | + y: { |
| 84 | + z: { |
| 85 | + a: { |
| 86 | + b: { |
| 87 | + c: "not an uuid", |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }) |
| 94 | + ).toBeUndefined(); |
| 95 | + |
| 96 | + expect( |
| 97 | + mock({ |
| 98 | + x: { |
| 99 | + y: { |
| 100 | + z: { |
| 101 | + a: { |
| 102 | + b: { |
| 103 | + c: randomUUID(), |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }) |
| 110 | + ).toEqual("ding"); |
| 111 | +}); |
0 commit comments