|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import type { CategoryConfig, PluginConfig } from '@code-pushup/models'; |
| 3 | +import { axeCategories, extractGroupSlugs } from './categories.js'; |
| 4 | +import { AXE_PLUGIN_SLUG } from './constants.js'; |
| 5 | +import { axeGroupRef } from './utils.js'; |
| 6 | + |
| 7 | +describe('axeCategories', () => { |
| 8 | + const createMockPlugin = ( |
| 9 | + overrides: Partial<Pick<PluginConfig, 'groups' | 'context'>> = {}, |
| 10 | + ): Pick<PluginConfig, 'groups' | 'context'> => ({ |
| 11 | + groups: [ |
| 12 | + { slug: 'aria', title: 'ARIA', refs: [] }, |
| 13 | + { slug: 'color', title: 'Color & Contrast', refs: [] }, |
| 14 | + ], |
| 15 | + context: { urlCount: 1, weights: { 1: 1 } }, |
| 16 | + ...overrides, |
| 17 | + }); |
| 18 | + |
| 19 | + it('should create accessibility category with all groups', () => { |
| 20 | + expect(axeCategories(createMockPlugin())).toEqual([ |
| 21 | + { |
| 22 | + slug: 'axe-a11y', |
| 23 | + title: 'Axe Accessibility', |
| 24 | + refs: [ |
| 25 | + { plugin: AXE_PLUGIN_SLUG, slug: 'aria', type: 'group', weight: 1 }, |
| 26 | + { |
| 27 | + plugin: AXE_PLUGIN_SLUG, |
| 28 | + slug: 'color', |
| 29 | + type: 'group', |
| 30 | + weight: 1, |
| 31 | + }, |
| 32 | + ], |
| 33 | + }, |
| 34 | + ]); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should expand refs for multi-URL', () => { |
| 38 | + const plugin = createMockPlugin({ |
| 39 | + groups: [ |
| 40 | + { slug: 'aria-1', title: 'ARIA 1', refs: [] }, |
| 41 | + { slug: 'aria-2', title: 'ARIA 2', refs: [] }, |
| 42 | + ], |
| 43 | + context: { urlCount: 2, weights: { 1: 1, 2: 1 } }, |
| 44 | + }); |
| 45 | + |
| 46 | + expect(axeCategories(plugin)).toEqual([ |
| 47 | + { |
| 48 | + slug: 'axe-a11y', |
| 49 | + title: 'Axe Accessibility', |
| 50 | + refs: [ |
| 51 | + { |
| 52 | + plugin: AXE_PLUGIN_SLUG, |
| 53 | + slug: 'aria-1', |
| 54 | + type: 'group', |
| 55 | + weight: 1, |
| 56 | + }, |
| 57 | + { |
| 58 | + plugin: AXE_PLUGIN_SLUG, |
| 59 | + slug: 'aria-2', |
| 60 | + type: 'group', |
| 61 | + weight: 1, |
| 62 | + }, |
| 63 | + ], |
| 64 | + }, |
| 65 | + ]); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should return empty array if plugin has no groups', () => { |
| 69 | + expect(axeCategories(createMockPlugin({ groups: [] }))).toEqual([]); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should return categories unchanged for single URL', () => { |
| 73 | + const categories: CategoryConfig[] = [ |
| 74 | + { |
| 75 | + slug: 'axe-a11y', |
| 76 | + title: 'Axe Accessibility', |
| 77 | + refs: [axeGroupRef('aria')], |
| 78 | + }, |
| 79 | + ]; |
| 80 | + |
| 81 | + expect(axeCategories(createMockPlugin(), categories)).toEqual(categories); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should expand Axe refs and preserve non-Axe refs for multi-URL', () => { |
| 85 | + const categories: CategoryConfig[] = [ |
| 86 | + { |
| 87 | + slug: 'axe-a11y', |
| 88 | + title: 'Axe Accessibility', |
| 89 | + refs: [ |
| 90 | + axeGroupRef('aria'), |
| 91 | + { plugin: 'lighthouse', type: 'group', slug: 'seo', weight: 1 }, |
| 92 | + ], |
| 93 | + }, |
| 94 | + ]; |
| 95 | + |
| 96 | + expect( |
| 97 | + axeCategories( |
| 98 | + createMockPlugin({ context: { urlCount: 2, weights: { 1: 1, 2: 1 } } }), |
| 99 | + categories, |
| 100 | + ), |
| 101 | + ).toEqual([ |
| 102 | + { |
| 103 | + slug: 'axe-a11y', |
| 104 | + title: 'Axe Accessibility', |
| 105 | + refs: [ |
| 106 | + { plugin: AXE_PLUGIN_SLUG, slug: 'aria-1', type: 'group', weight: 1 }, |
| 107 | + { plugin: AXE_PLUGIN_SLUG, slug: 'aria-2', type: 'group', weight: 1 }, |
| 108 | + { plugin: 'lighthouse', type: 'group', slug: 'seo', weight: 1 }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + ]); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should throw for invalid context', () => { |
| 115 | + const plugin = createMockPlugin({ |
| 116 | + context: { urlCount: 2, weights: { 1: 1 } }, |
| 117 | + }); |
| 118 | + |
| 119 | + expect(() => axeCategories(plugin)).toThrow( |
| 120 | + 'Invalid plugin context: weights count must match urlCount', |
| 121 | + ); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe('extractGroupSlugs', () => { |
| 126 | + it('should extract unique base slugs from groups', () => { |
| 127 | + expect( |
| 128 | + extractGroupSlugs([ |
| 129 | + { slug: 'aria-1', title: 'ARIA 1', refs: [] }, |
| 130 | + { slug: 'aria-2', title: 'ARIA 2', refs: [] }, |
| 131 | + { slug: 'color', title: 'Color & Contrast', refs: [] }, |
| 132 | + ]), |
| 133 | + ).toEqual(['aria', 'color']); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should filter out invalid group slugs', () => { |
| 137 | + expect( |
| 138 | + extractGroupSlugs([ |
| 139 | + { slug: 'aria', title: 'ARIA', refs: [] }, |
| 140 | + { slug: 'invalid-group', title: 'Invalid', refs: [] }, |
| 141 | + ]), |
| 142 | + ).toEqual(['aria']); |
| 143 | + }); |
| 144 | +}); |
0 commit comments