|
| 1 | +import { isValidUUID } from "./uuid.js"; |
| 2 | + |
| 3 | +describe("isValidUUID", () => { |
| 4 | + // POSITIVE TEST CASES |
| 5 | + test.each([ |
| 6 | + // The UUID spec defines a total of 8 versions |
| 7 | + ...["1", "2", "3", "4", "5", "6", "7", "8"].map((uuidVersionNum) => ({ |
| 8 | + // Note: the hard-coded 8 reflects the UUID variant |
| 9 | + input: `aaaaaaaa-aaaa-${uuidVersionNum}aaa-8aaa-aaaaaaaaaaaa`, |
| 10 | + description: `a version-${uuidVersionNum} UUID string`, |
| 11 | + })), |
| 12 | + { input: "00000000-0000-0000-0000-000000000000", description: "the nil UUID (all zeros)" }, |
| 13 | + { input: "ffffffff-ffff-ffff-ffff-ffffffffffff", description: "the max UUID (all f's)" }, |
| 14 | + ])(`returns true when called with $description`, ({ input }) => { |
| 15 | + expect(isValidUUID(input)).toBe(true); |
| 16 | + }); |
| 17 | + |
| 18 | + // NEGATIVE TEST CASES |
| 19 | + test.each([ |
| 20 | + { input: "", description: "an empty string" }, |
| 21 | + { input: "foo", description: "a non-UUID string" }, |
| 22 | + { input: 1, description: "1 (a truthy number)" }, |
| 23 | + { input: 0, description: "0 (zero)" }, |
| 24 | + { input: true, description: "true (boolean)" }, |
| 25 | + { input: false, description: "false (boolean)" }, |
| 26 | + { input: null, description: "null" }, |
| 27 | + { input: undefined, description: "undefined" }, |
| 28 | + { |
| 29 | + input: "aaaaaaaa-aaaa-9aaa-8aaa-aaaaaaaaaaaa", |
| 30 | + description: "a UUID with an invalid version number", |
| 31 | + }, |
| 32 | + { |
| 33 | + input: "aaaaaaaa-aaaa-1aaa-7aaa-aaaaaaaaaaaa", |
| 34 | + description: "a UUID with an invalid variant identifier", // must be 8, 9, a, or b |
| 35 | + }, |
| 36 | + ])("returns false when called with $description", ({ input }) => { |
| 37 | + expect(isValidUUID(input)).toBe(false); |
| 38 | + }); |
| 39 | +}); |
0 commit comments