|
| 1 | +/* global describe, test, expect */ |
| 2 | + |
| 3 | +import { Kind } from 'graphql/language'; |
| 4 | +import { GraphQLCuid2 } from '../src/scalars/Cuid2.js'; |
| 5 | + |
| 6 | +describe('Cuid2', () => { |
| 7 | + describe('valid', () => { |
| 8 | + const validCuid2 = 'dp71y53f6eykvl5g1393rmhl'; |
| 9 | + |
| 10 | + test('serialize', () => { |
| 11 | + expect(GraphQLCuid2.serialize(validCuid2)).toBe(validCuid2); |
| 12 | + }); |
| 13 | + |
| 14 | + test('parseValue', () => { |
| 15 | + expect(GraphQLCuid2.parseValue(validCuid2)).toBe(validCuid2); |
| 16 | + }); |
| 17 | + |
| 18 | + test('parseLiteral', () => { |
| 19 | + expect( |
| 20 | + GraphQLCuid2.parseLiteral( |
| 21 | + { |
| 22 | + value: validCuid2, |
| 23 | + kind: Kind.STRING, |
| 24 | + }, |
| 25 | + {}, |
| 26 | + ), |
| 27 | + ).toBe(validCuid2); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('invalid', () => { |
| 32 | + describe('not a cuid2', () => { |
| 33 | + test('serialize', () => { |
| 34 | + expect(() => GraphQLCuid2.serialize('not-a-valid-cuid2')).toThrow( |
| 35 | + /Value is not a valid cuid2/, |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + test('parseValue', () => { |
| 40 | + expect(() => GraphQLCuid2.parseValue('not-a-valid-cuid2')).toThrow( |
| 41 | + /Value is not a valid cuid2/, |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + test('parseLiteral', () => { |
| 46 | + expect(() => |
| 47 | + GraphQLCuid2.parseLiteral( |
| 48 | + { |
| 49 | + value: 'not-a-valid-cuid2', |
| 50 | + kind: Kind.STRING, |
| 51 | + }, |
| 52 | + {}, |
| 53 | + ), |
| 54 | + ).toThrow(/Value is not a valid cuid2/); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('not a string', () => { |
| 59 | + test('serialize', () => { |
| 60 | + expect(() => GraphQLCuid2.serialize(123)).toThrow(/Value is not string/); |
| 61 | + }); |
| 62 | + |
| 63 | + test('parseValue', () => { |
| 64 | + expect(() => GraphQLCuid2.parseValue(123)).toThrow(/Value is not string/); |
| 65 | + }); |
| 66 | + |
| 67 | + test('parseLiteral', () => { |
| 68 | + expect(() => GraphQLCuid2.parseLiteral({ value: '123', kind: Kind.INT }, {})).toThrow( |
| 69 | + /Can only validate strings as cuid2 but got/, |
| 70 | + ); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('boundary cases', () => { |
| 75 | + test('minimum length (2 characters)', () => { |
| 76 | + const minCuid2 = 'a1'; |
| 77 | + expect(GraphQLCuid2.serialize(minCuid2)).toBe(minCuid2); |
| 78 | + expect(GraphQLCuid2.parseValue(minCuid2)).toBe(minCuid2); |
| 79 | + expect(GraphQLCuid2.parseLiteral({ value: minCuid2, kind: Kind.STRING }, {})).toBe( |
| 80 | + minCuid2, |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + test('maximum length (32 characters)', () => { |
| 85 | + const maxCuid2 = 'a123456789abcdef123456789abcdef1'; // starts with a letter |
| 86 | + expect(GraphQLCuid2.serialize(maxCuid2)).toBe(maxCuid2); |
| 87 | + expect(GraphQLCuid2.parseValue(maxCuid2)).toBe(maxCuid2); |
| 88 | + expect(GraphQLCuid2.parseLiteral({ value: maxCuid2, kind: Kind.STRING }, {})).toBe( |
| 89 | + maxCuid2, |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + test('too short (1 character)', () => { |
| 94 | + const tooShort = 'a'; |
| 95 | + expect(() => GraphQLCuid2.serialize(tooShort)).toThrow(/Value is not a valid cuid2/); |
| 96 | + }); |
| 97 | + |
| 98 | + test('too long (33 characters)', () => { |
| 99 | + const tooLong = 'a1234567890abcdef1234567890abcdef1'; |
| 100 | + expect(() => GraphQLCuid2.serialize(tooLong)).toThrow(/Value is not a valid cuid2/); |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments