|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 3 | + |
| 4 | +const mockMatchCount = vi.hoisted(() => ({ value: 1 })); |
| 5 | +const mockViewState = vi.hoisted(() => ({ |
| 6 | + mode: 'preview' as string, |
| 7 | + pane: null as HTMLElement | null, |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('mark.js', () => ({ |
| 11 | + default: class MockMark { |
| 12 | + private container: Element; |
| 13 | + constructor(container: Element) { this.container = container; } |
| 14 | + |
| 15 | + mark(_keyword: string, options?: { className?: string; done?: (n: number) => void }) { |
| 16 | + for (let i = 0; i < mockMatchCount.value; i++) { |
| 17 | + const el = document.createElement('mark'); |
| 18 | + el.className = options?.className ?? ''; |
| 19 | + this.container.appendChild(el); |
| 20 | + } |
| 21 | + options?.done?.(mockMatchCount.value); |
| 22 | + } |
| 23 | + |
| 24 | + markRegExp(_re: RegExp, options?: { className?: string; done?: (n: number) => void }) { |
| 25 | + for (let i = 0; i < mockMatchCount.value; i++) { |
| 26 | + const el = document.createElement('mark'); |
| 27 | + el.className = options?.className ?? ''; |
| 28 | + this.container.appendChild(el); |
| 29 | + } |
| 30 | + options?.done?.(mockMatchCount.value); |
| 31 | + } |
| 32 | + |
| 33 | + unmark(options?: { done?: () => void }) { |
| 34 | + this.container.querySelectorAll('mark').forEach(el => el.remove()); |
| 35 | + options?.done?.(); |
| 36 | + } |
| 37 | + }, |
| 38 | +})); |
| 39 | + |
| 40 | +vi.mock('../src/settings', () => ({ themeName: 'github' })); |
| 41 | + |
| 42 | +vi.mock('../src/view', () => ({ |
| 43 | + ViewMode: { edit: 'edit', sideBySide: 'side-by-side', preview: 'preview' }, |
| 44 | + currentViewMode: vi.fn(() => mockViewState.mode), |
| 45 | + getPreviewPane: vi.fn(() => mockViewState.pane), |
| 46 | +})); |
| 47 | + |
| 48 | +import { performSearch, setSearchMatchIndex, clearSearch, searchCounterInfo } from '../src/search'; |
| 49 | + |
| 50 | +const baseOptions = { |
| 51 | + search: 'hello', |
| 52 | + caseSensitive: false, |
| 53 | + diacriticInsensitive: false, |
| 54 | + wholeWord: false, |
| 55 | + regexp: false, |
| 56 | +}; |
| 57 | + |
| 58 | +beforeEach(() => { |
| 59 | + mockViewState.pane = document.createElement('div'); |
| 60 | + document.body.appendChild(mockViewState.pane); |
| 61 | + mockViewState.mode = 'preview'; |
| 62 | + mockMatchCount.value = 1; |
| 63 | + clearSearch(); |
| 64 | +}); |
| 65 | + |
| 66 | +afterEach(() => { |
| 67 | + document.body.innerHTML = ''; |
| 68 | + mockViewState.pane = null; |
| 69 | +}); |
| 70 | + |
| 71 | +describe('searchCounterInfo', () => { |
| 72 | + it('returns undefined in edit mode', () => { |
| 73 | + mockViewState.mode = 'edit'; |
| 74 | + expect(searchCounterInfo()).toBeUndefined(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns undefined in side-by-side mode', () => { |
| 78 | + mockViewState.mode = 'side-by-side'; |
| 79 | + expect(searchCounterInfo()).toBeUndefined(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns zero counter in preview mode with no active search', () => { |
| 83 | + expect(searchCounterInfo()).toEqual({ numberOfItems: 0, currentIndex: 0 }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('reflects mark count after search', () => { |
| 87 | + mockMatchCount.value = 3; |
| 88 | + performSearch(baseOptions); |
| 89 | + expect(searchCounterInfo()).toEqual({ numberOfItems: 3, currentIndex: 0 }); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe('performSearch', () => { |
| 94 | + it('clears marks when query is empty', () => { |
| 95 | + mockMatchCount.value = 2; |
| 96 | + performSearch(baseOptions); |
| 97 | + performSearch({ ...baseOptions, search: '' }); |
| 98 | + expect(searchCounterInfo()?.numberOfItems).toBe(0); |
| 99 | + }); |
| 100 | + |
| 101 | + it('resets currentIndex to 0 on new search', () => { |
| 102 | + mockMatchCount.value = 3; |
| 103 | + performSearch(baseOptions); |
| 104 | + setSearchMatchIndex(2); |
| 105 | + performSearch({ ...baseOptions, search: 'world' }); |
| 106 | + expect(searchCounterInfo()?.currentIndex).toBe(0); |
| 107 | + }); |
| 108 | + |
| 109 | + it('handles regexp queries', () => { |
| 110 | + performSearch({ ...baseOptions, regexp: true }); |
| 111 | + expect(searchCounterInfo()?.numberOfItems).toBe(1); |
| 112 | + }); |
| 113 | + |
| 114 | + it('handles invalid regexp without throwing', () => { |
| 115 | + expect(() => { |
| 116 | + performSearch({ ...baseOptions, regexp: true, search: '[invalid' }); |
| 117 | + }).not.toThrow(); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('setSearchMatchIndex', () => { |
| 122 | + it('is a no-op when there are no marks', () => { |
| 123 | + setSearchMatchIndex(5); |
| 124 | + expect(searchCounterInfo()?.currentIndex).toBe(0); |
| 125 | + }); |
| 126 | + |
| 127 | + it('sets the current index within range', () => { |
| 128 | + mockMatchCount.value = 3; |
| 129 | + performSearch(baseOptions); |
| 130 | + setSearchMatchIndex(1); |
| 131 | + expect(searchCounterInfo()?.currentIndex).toBe(1); |
| 132 | + }); |
| 133 | + |
| 134 | + it('wraps index using modulo when out of range', () => { |
| 135 | + mockMatchCount.value = 3; |
| 136 | + performSearch(baseOptions); |
| 137 | + setSearchMatchIndex(5); // 5 % 3 = 2 |
| 138 | + expect(searchCounterInfo()?.currentIndex).toBe(2); |
| 139 | + }); |
| 140 | + |
| 141 | + it('wraps to 0 when index equals mark count', () => { |
| 142 | + mockMatchCount.value = 3; |
| 143 | + performSearch(baseOptions); |
| 144 | + setSearchMatchIndex(3); // 3 % 3 = 0 |
| 145 | + expect(searchCounterInfo()?.currentIndex).toBe(0); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +describe('clearSearch', () => { |
| 150 | + it('resets mark count to 0', () => { |
| 151 | + mockMatchCount.value = 3; |
| 152 | + performSearch(baseOptions); |
| 153 | + clearSearch(); |
| 154 | + expect(searchCounterInfo()?.numberOfItems).toBe(0); |
| 155 | + }); |
| 156 | + |
| 157 | + it('resets currentIndex to 0', () => { |
| 158 | + mockMatchCount.value = 3; |
| 159 | + performSearch(baseOptions); |
| 160 | + setSearchMatchIndex(2); |
| 161 | + clearSearch(); |
| 162 | + expect(searchCounterInfo()?.currentIndex).toBe(0); |
| 163 | + }); |
| 164 | +}); |
0 commit comments