Skip to content

Commit 57eec4e

Browse files
committed
Extra tests for search events
1 parent 3e5dfde commit 57eec4e

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/main/default/lwc/lookup/__tests__/lookupEventFiring.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createElement } from 'lwc';
22
import Lookup from 'c/lookup';
33

4+
const SAMPLE_SEARCH_TOO_SHORT = 'A ';
45
const SAMPLE_SEARCH_RAW = 'Sample search* ';
56
const SAMPLE_SEARCH_CLEAN = 'sample search';
67
const SAMPLE_SEARCH_ITEMS = [
@@ -55,4 +56,62 @@ describe('c-lookup event fires', () => {
5556
selectedIds: ['id1', 'id2']
5657
});
5758
});
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+
});
58117
});

0 commit comments

Comments
 (0)