|
| 1 | +/** |
| 2 | + * Tests for pattern matching utilities |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it } from 'node:test'; |
| 6 | +import assert from 'node:assert/strict'; |
| 7 | +import { |
| 8 | + matchPattern, |
| 9 | + filterByPattern, |
| 10 | + findMatchingHook, |
| 11 | +} from '../src/utils/patterns.js'; |
| 12 | + |
| 13 | +describe('matchPattern', () => { |
| 14 | + it('should match exact string', () => { |
| 15 | + assert.ok(matchPattern('button--primary', 'button--primary')); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should match with wildcard', () => { |
| 19 | + assert.ok(matchPattern('button--primary', 'button*')); |
| 20 | + assert.ok(matchPattern('button--secondary', 'button*')); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should match with double wildcard', () => { |
| 24 | + assert.ok(matchPattern('components/atoms/button', 'components/**')); |
| 25 | + assert.ok(matchPattern('components/atoms/button/primary', 'components/**')); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should not match different strings', () => { |
| 29 | + assert.ok(!matchPattern('card--default', 'button*')); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return true for empty pattern', () => { |
| 33 | + assert.ok(matchPattern('anything', '')); |
| 34 | + assert.ok(matchPattern('anything', null)); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return false for empty string', () => { |
| 38 | + assert.ok(!matchPattern('', 'pattern')); |
| 39 | + assert.ok(!matchPattern(null, 'pattern')); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('filterByPattern', () => { |
| 44 | + let stories = [ |
| 45 | + { id: 'button--primary', title: 'Button' }, |
| 46 | + { id: 'button--secondary', title: 'Button' }, |
| 47 | + { id: 'card--default', title: 'Card' }, |
| 48 | + ]; |
| 49 | + |
| 50 | + it('should filter by include pattern', () => { |
| 51 | + let filtered = filterByPattern(stories, 'button*', null); |
| 52 | + |
| 53 | + assert.equal(filtered.length, 2); |
| 54 | + assert.ok(filtered.every(s => s.id.startsWith('button'))); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should filter by exclude pattern', () => { |
| 58 | + let filtered = filterByPattern(stories, null, 'button*'); |
| 59 | + |
| 60 | + assert.equal(filtered.length, 1); |
| 61 | + assert.equal(filtered[0].id, 'card--default'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should apply both include and exclude', () => { |
| 65 | + let filtered = filterByPattern(stories, 'button*', 'button--secondary'); |
| 66 | + |
| 67 | + assert.equal(filtered.length, 1); |
| 68 | + assert.equal(filtered[0].id, 'button--primary'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should return all stories when no patterns', () => { |
| 72 | + let filtered = filterByPattern(stories, null, null); |
| 73 | + |
| 74 | + assert.equal(filtered.length, 3); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should use title as fallback when no id', () => { |
| 78 | + let storiesWithoutId = [ |
| 79 | + { title: 'Button' }, |
| 80 | + { title: 'Card' }, |
| 81 | + ]; |
| 82 | + |
| 83 | + let filtered = filterByPattern(storiesWithoutId, 'Button', null); |
| 84 | + |
| 85 | + assert.equal(filtered.length, 1); |
| 86 | + assert.equal(filtered[0].title, 'Button'); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('findMatchingHook', () => { |
| 91 | + it('should find matching hook from simple object format', () => { |
| 92 | + let hook = () => {}; |
| 93 | + let interactions = { |
| 94 | + 'button*': hook, |
| 95 | + }; |
| 96 | + let story = { id: 'button--primary' }; |
| 97 | + |
| 98 | + let result = findMatchingHook(story, interactions); |
| 99 | + |
| 100 | + assert.equal(result, hook); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should return null when no match in simple format', () => { |
| 104 | + let interactions = { |
| 105 | + 'card*': () => {}, |
| 106 | + }; |
| 107 | + let story = { id: 'button--primary' }; |
| 108 | + |
| 109 | + let result = findMatchingHook(story, interactions); |
| 110 | + |
| 111 | + assert.equal(result, null); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should find matching hook from patterns array format', () => { |
| 115 | + let hook = () => {}; |
| 116 | + let interactions = { |
| 117 | + patterns: [ |
| 118 | + { match: 'card*', beforeScreenshot: () => {} }, |
| 119 | + { match: 'button*', beforeScreenshot: hook }, |
| 120 | + ], |
| 121 | + }; |
| 122 | + let story = { id: 'button--primary' }; |
| 123 | + |
| 124 | + let result = findMatchingHook(story, interactions); |
| 125 | + |
| 126 | + assert.equal(result, hook); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should return null when no match in patterns array format', () => { |
| 130 | + let interactions = { |
| 131 | + patterns: [ |
| 132 | + { match: 'card*', beforeScreenshot: () => {} }, |
| 133 | + ], |
| 134 | + }; |
| 135 | + let story = { id: 'button--primary' }; |
| 136 | + |
| 137 | + let result = findMatchingHook(story, interactions); |
| 138 | + |
| 139 | + assert.equal(result, null); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should return null for null interactions', () => { |
| 143 | + let story = { id: 'button--primary' }; |
| 144 | + |
| 145 | + let result = findMatchingHook(story, null); |
| 146 | + |
| 147 | + assert.equal(result, null); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should return null for undefined interactions', () => { |
| 151 | + let story = { id: 'button--primary' }; |
| 152 | + |
| 153 | + let result = findMatchingHook(story, undefined); |
| 154 | + |
| 155 | + assert.equal(result, null); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should return null for empty interactions object', () => { |
| 159 | + let story = { id: 'button--primary' }; |
| 160 | + |
| 161 | + let result = findMatchingHook(story, {}); |
| 162 | + |
| 163 | + assert.equal(result, null); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should use title as fallback when no id', () => { |
| 167 | + let hook = () => {}; |
| 168 | + let interactions = { |
| 169 | + 'Button*': hook, |
| 170 | + }; |
| 171 | + let story = { title: 'Button' }; |
| 172 | + |
| 173 | + let result = findMatchingHook(story, interactions); |
| 174 | + |
| 175 | + assert.equal(result, hook); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should return first matching hook in patterns array', () => { |
| 179 | + let firstHook = () => 'first'; |
| 180 | + let secondHook = () => 'second'; |
| 181 | + let interactions = { |
| 182 | + patterns: [ |
| 183 | + { match: 'button*', beforeScreenshot: firstHook }, |
| 184 | + { match: 'button--primary', beforeScreenshot: secondHook }, |
| 185 | + ], |
| 186 | + }; |
| 187 | + let story = { id: 'button--primary' }; |
| 188 | + |
| 189 | + let result = findMatchingHook(story, interactions); |
| 190 | + |
| 191 | + assert.equal(result, firstHook); |
| 192 | + }); |
| 193 | +}); |
0 commit comments