|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import type { StringIssue } from '../../schemas/index.ts'; |
| 3 | +import { _getCodePointCount } from '../../utils/index.ts'; |
| 4 | +import { expectActionIssue, expectNoActionIssue } from '../../vitest/index.ts'; |
| 5 | +import { |
| 6 | + codePoints, |
| 7 | + type CodePointsAction, |
| 8 | + type CodePointsIssue, |
| 9 | +} from './codePoints.ts'; |
| 10 | + |
| 11 | +describe('graphemes', () => { |
| 12 | + describe('should return action object', () => { |
| 13 | + const baseAction: Omit<CodePointsAction<string, 5, never>, 'message'> = { |
| 14 | + kind: 'validation', |
| 15 | + type: 'code_points', |
| 16 | + reference: codePoints, |
| 17 | + expects: '5', |
| 18 | + requirement: 5, |
| 19 | + async: false, |
| 20 | + '~validate': expect.any(Function), |
| 21 | + }; |
| 22 | + |
| 23 | + test('with undefined message', () => { |
| 24 | + const action: CodePointsAction<string, 5, undefined> = { |
| 25 | + ...baseAction, |
| 26 | + message: undefined, |
| 27 | + }; |
| 28 | + expect(codePoints(5)).toStrictEqual(action); |
| 29 | + expect(codePoints(5, undefined)).toStrictEqual(action); |
| 30 | + }); |
| 31 | + |
| 32 | + test('with string message', () => { |
| 33 | + expect(codePoints(5, 'message')).toStrictEqual({ |
| 34 | + ...baseAction, |
| 35 | + message: 'message', |
| 36 | + } satisfies CodePointsAction<string, 5, string>); |
| 37 | + }); |
| 38 | + |
| 39 | + test('with function message', () => { |
| 40 | + const message = () => 'message'; |
| 41 | + expect(codePoints(5, message)).toStrictEqual({ |
| 42 | + ...baseAction, |
| 43 | + message, |
| 44 | + } satisfies CodePointsAction<string, 5, typeof message>); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('should return dataset without issues', () => { |
| 49 | + const action = codePoints(5); |
| 50 | + |
| 51 | + test('for untyped inputs', () => { |
| 52 | + const issues: [StringIssue] = [ |
| 53 | + { |
| 54 | + kind: 'schema', |
| 55 | + type: 'string', |
| 56 | + input: null, |
| 57 | + expected: 'string', |
| 58 | + received: 'null', |
| 59 | + message: 'message', |
| 60 | + }, |
| 61 | + ]; |
| 62 | + expect( |
| 63 | + action['~validate']({ typed: false, value: null, issues }, {}) |
| 64 | + ).toStrictEqual({ |
| 65 | + typed: false, |
| 66 | + value: null, |
| 67 | + issues, |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + test('for valid strings', () => { |
| 72 | + expectNoActionIssue(action, ['12345', '12 45', '1234 ', 'hello']); |
| 73 | + }); |
| 74 | + |
| 75 | + test('for valid emoji', () => { |
| 76 | + expectNoActionIssue(action, ['👨🏽👩🏽', '😶🌫️😀', '😡👍😁😂😀', '0️⃣㊙️']); |
| 77 | + }); |
| 78 | + |
| 79 | + test('for valid non-latin', () => { |
| 80 | + expectNoActionIssue(action, ['あ𛀙よろし', '𠮷野家で𩸽', '葛󠄀城市!']); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('should return dataset with issues', () => { |
| 85 | + const action = codePoints(5, 'message'); |
| 86 | + const baseIssue: Omit<CodePointsIssue<string, 5>, 'input' | 'received'> = { |
| 87 | + kind: 'validation', |
| 88 | + type: 'code_points', |
| 89 | + expected: '5', |
| 90 | + message: 'message', |
| 91 | + requirement: 5, |
| 92 | + }; |
| 93 | + |
| 94 | + test('for invalid strings', () => { |
| 95 | + expectActionIssue( |
| 96 | + action, |
| 97 | + baseIssue, |
| 98 | + ['', ' ', '1', '1234', '123 ', '123456', '12 456', '123456789'], |
| 99 | + (value) => `${_getCodePointCount(value)}` |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + test('for invalid emoji', () => { |
| 104 | + expectActionIssue( |
| 105 | + action, |
| 106 | + baseIssue, |
| 107 | + ['😀👋🏼🧩👩🏻🏫🫥', '㊙️㊙️0️⃣1️⃣2️⃣'], |
| 108 | + (value) => `${_getCodePointCount(value)}` |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + test('for invalid non-latin', () => { |
| 113 | + expectActionIssue( |
| 114 | + action, |
| 115 | + baseIssue, |
| 116 | + ['竈門禰󠄀豆子', '葛󠄀城市'], |
| 117 | + (value) => `${_getCodePointCount(value)}` |
| 118 | + ); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments