|
| 1 | +import type z from 'zod'; |
| 2 | +import type { Audit } from '../audit.js'; |
| 3 | +import type { CategoryRef } from '../category-config.js'; |
| 4 | +import { |
| 5 | + createCheck, |
| 6 | + createDuplicateSlugsCheck, |
| 7 | + createDuplicatesCheck, |
| 8 | +} from './checks.js'; |
| 9 | + |
| 10 | +describe('createCheck', () => { |
| 11 | + it('should add issue if callback finds an error', () => { |
| 12 | + const findErrorFn = vi |
| 13 | + .fn<[string]>() |
| 14 | + .mockReturnValue({ message: 'Something went wrong' }); |
| 15 | + |
| 16 | + const check = createCheck(findErrorFn); |
| 17 | + |
| 18 | + expect(findErrorFn).not.toHaveBeenCalled(); |
| 19 | + |
| 20 | + const ctx: z.core.ParsePayload<string> = { value: 'XYZ', issues: [] }; |
| 21 | + check(ctx); |
| 22 | + |
| 23 | + expect(findErrorFn).toHaveBeenCalledWith('XYZ'); |
| 24 | + expect(ctx.issues).toEqual([ |
| 25 | + { |
| 26 | + code: 'custom', |
| 27 | + message: 'Something went wrong', |
| 28 | + input: 'XYZ', |
| 29 | + }, |
| 30 | + ]); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should NOT add issue if callback finds no error', () => { |
| 34 | + const findErrorFn = vi.fn<[string]>().mockReturnValue(false); |
| 35 | + |
| 36 | + const check = createCheck<string>(findErrorFn); |
| 37 | + |
| 38 | + expect(findErrorFn).not.toHaveBeenCalled(); |
| 39 | + |
| 40 | + const ctx: z.core.ParsePayload<string> = { value: 'XYZ', issues: [] }; |
| 41 | + check(ctx); |
| 42 | + |
| 43 | + expect(findErrorFn).toHaveBeenCalledWith('XYZ'); |
| 44 | + expect(ctx.issues).toEqual([]); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +describe('createDuplicatesCheck', () => { |
| 49 | + const keyFn = vi.fn( |
| 50 | + ({ type, plugin, slug }: CategoryRef) => `${type} ${plugin}/${slug}`, |
| 51 | + ); |
| 52 | + const errorMsgFn = vi.fn( |
| 53 | + (duplicates: string[]) => `Duplicate refs found: ${duplicates.join(', ')}`, |
| 54 | + ); |
| 55 | + |
| 56 | + it('add issue with custom message if there are duplicate keys', () => { |
| 57 | + const check = createDuplicatesCheck<CategoryRef>(keyFn, errorMsgFn); |
| 58 | + |
| 59 | + expect(keyFn).not.toHaveBeenCalled(); |
| 60 | + expect(errorMsgFn).not.toHaveBeenCalled(); |
| 61 | + |
| 62 | + const ctx: z.core.ParsePayload<CategoryRef[]> = { |
| 63 | + value: [ |
| 64 | + { type: 'audit', plugin: 'coverage', slug: 'coverage', weight: 2 }, |
| 65 | + { type: 'audit', plugin: 'jsdocs', slug: 'coverage', weight: 1 }, |
| 66 | + { type: 'audit', plugin: 'coverage', slug: 'coverage', weight: 1 }, |
| 67 | + ], |
| 68 | + issues: [], |
| 69 | + }; |
| 70 | + check(ctx); |
| 71 | + |
| 72 | + expect(keyFn).toHaveBeenCalledTimes(3); |
| 73 | + expect(errorMsgFn).toHaveBeenCalledWith(['audit coverage/coverage']); |
| 74 | + expect(ctx.issues).toEqual([ |
| 75 | + { |
| 76 | + code: 'custom', |
| 77 | + message: 'Duplicate refs found: audit coverage/coverage', |
| 78 | + input: ctx.value, |
| 79 | + }, |
| 80 | + ]); |
| 81 | + }); |
| 82 | + |
| 83 | + it('add NOT add issue if all keys are unique', () => { |
| 84 | + const check = createDuplicatesCheck<CategoryRef>(keyFn, errorMsgFn); |
| 85 | + |
| 86 | + expect(keyFn).not.toHaveBeenCalled(); |
| 87 | + expect(errorMsgFn).not.toHaveBeenCalled(); |
| 88 | + |
| 89 | + const ctx: z.core.ParsePayload<CategoryRef[]> = { |
| 90 | + value: [ |
| 91 | + { type: 'group', plugin: 'eslint', slug: 'errors', weight: 1 }, |
| 92 | + { type: 'group', plugin: 'eslint', slug: 'warnings', weight: 1 }, |
| 93 | + { type: 'group', plugin: 'typescript', slug: 'errors', weight: 1 }, |
| 94 | + ], |
| 95 | + issues: [], |
| 96 | + }; |
| 97 | + check(ctx); |
| 98 | + |
| 99 | + expect(keyFn).toHaveBeenCalledTimes(3); |
| 100 | + expect(errorMsgFn).not.toHaveBeenCalled(); |
| 101 | + expect(ctx.issues).toEqual([]); |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +describe('createDuplicateSlugsCheck', () => { |
| 106 | + it('should add issue if there are duplicate slugs', () => { |
| 107 | + const check = createDuplicateSlugsCheck<Audit>('Audit'); |
| 108 | + |
| 109 | + const ctx: z.core.ParsePayload<Audit[]> = { |
| 110 | + value: [ |
| 111 | + { slug: 'lcp', title: 'Largest Contentful Paint' }, |
| 112 | + { slug: 'cls', title: 'Cumulative Layout Shift' }, |
| 113 | + { slug: 'fcp', title: 'First Contentful Paint' }, |
| 114 | + { slug: 'lcp', title: 'LCP' }, |
| 115 | + { slug: 'fcp', title: 'FCP' }, |
| 116 | + ], |
| 117 | + issues: [], |
| 118 | + }; |
| 119 | + check(ctx); |
| 120 | + |
| 121 | + expect(ctx.issues).toEqual([ |
| 122 | + { |
| 123 | + code: 'custom', |
| 124 | + message: |
| 125 | + 'Audit slugs must be unique, but received duplicates: "fcp", "lcp"', |
| 126 | + input: ctx.value, |
| 127 | + }, |
| 128 | + ]); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should NOT add issue if all slugs are unique', () => { |
| 132 | + const check = createDuplicateSlugsCheck<Audit>('Audit'); |
| 133 | + |
| 134 | + const ctx: z.core.ParsePayload<Audit[]> = { |
| 135 | + value: [ |
| 136 | + { slug: 'lcp', title: 'Largest Contentful Paint' }, |
| 137 | + { slug: 'cls', title: 'Cumulative Layout Shift' }, |
| 138 | + { slug: 'fcp', title: 'First Contentful Paint' }, |
| 139 | + ], |
| 140 | + issues: [], |
| 141 | + }; |
| 142 | + check(ctx); |
| 143 | + |
| 144 | + expect(ctx.issues).toEqual([]); |
| 145 | + }); |
| 146 | +}); |
0 commit comments