|
1 | 1 | import { createElement } from 'lwc'; |
2 | 2 | import Lookup from 'c/lookup'; |
3 | 3 |
|
| 4 | +const SAMPLE_SEARCH_TOO_SHORT = 'A '; |
4 | 5 | const SAMPLE_SEARCH_RAW = 'Sample search* '; |
5 | 6 | const SAMPLE_SEARCH_CLEAN = 'sample search'; |
6 | 7 | const SAMPLE_SEARCH_ITEMS = [ |
@@ -55,4 +56,62 @@ describe('c-lookup event fires', () => { |
55 | 56 | selectedIds: ['id1', 'id2'] |
56 | 57 | }); |
57 | 58 | }); |
| 59 | + |
| 60 | + it('search event does not fires when search term is too short', () => { |
| 61 | + jest.useFakeTimers(); |
| 62 | + |
| 63 | + // Create element with mock search handler |
| 64 | + const mockSearchFn = jest.fn(); |
| 65 | + const element = createElement('c-lookup', { |
| 66 | + is: Lookup |
| 67 | + }); |
| 68 | + element.addEventListener('search', mockSearchFn); |
| 69 | + document.body.appendChild(element); |
| 70 | + |
| 71 | + // Set search term and force input change |
| 72 | + const searchInput = element.shadowRoot.querySelector('input'); |
| 73 | + searchInput.value = SAMPLE_SEARCH_TOO_SHORT; |
| 74 | + searchInput.dispatchEvent(new CustomEvent('input')); |
| 75 | + |
| 76 | + // Disable search throttling |
| 77 | + jest.runAllTimers(); |
| 78 | + |
| 79 | + // Check that search event wasn't fired |
| 80 | + expect(mockSearchFn).not.toBeCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('search event does not fires twice when search term matches clean search term', () => { |
| 84 | + jest.useFakeTimers(); |
| 85 | + |
| 86 | + // Create element with mock search handler |
| 87 | + const mockSearchFn = jest.fn(); |
| 88 | + const element = createElement('c-lookup', { |
| 89 | + is: Lookup |
| 90 | + }); |
| 91 | + element.addEventListener('search', mockSearchFn); |
| 92 | + document.body.appendChild(element); |
| 93 | + |
| 94 | + // Set search term and force input change |
| 95 | + const searchInput = element.shadowRoot.querySelector('input'); |
| 96 | + searchInput.value = SAMPLE_SEARCH_RAW; |
| 97 | + searchInput.dispatchEvent(new CustomEvent('input')); |
| 98 | + |
| 99 | + // Disable search throttling |
| 100 | + jest.runAllTimers(); |
| 101 | + |
| 102 | + // Update search term |
| 103 | + searchInput.value = SAMPLE_SEARCH_CLEAN; |
| 104 | + searchInput.dispatchEvent(new CustomEvent('input')); |
| 105 | + |
| 106 | + // Disable search throttling |
| 107 | + jest.runAllTimers(); |
| 108 | + |
| 109 | + // Check fired search event |
| 110 | + expect(mockSearchFn).toHaveBeenCalledTimes(1); |
| 111 | + const searchEvent = mockSearchFn.mock.calls[0][0]; |
| 112 | + expect(searchEvent.detail).toEqual({ |
| 113 | + searchTerm: SAMPLE_SEARCH_CLEAN, |
| 114 | + selectedIds: [] |
| 115 | + }); |
| 116 | + }); |
58 | 117 | }); |
0 commit comments