Skip to content

Commit e07b9bb

Browse files
fix: added proper synthetic event emitter NO-JIRA (#130)
* fix: added proper synthetic event emitter NO-JIRA * chore: removed unnecessary exports NO-JIRA * feat(text input): test update NO-JIRA * feat(text input inherited components): tests update NO-JIRA * fix: cr issue NO-JIRA
1 parent 8d0c57e commit e07b9bb

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

src/components/InlineSearchInput/InlineSearchInput.test.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,18 @@ describe('InlineSearchInput', () => {
9696

9797
it('should clear controlled text input', () => {
9898
const { input, clearButton } = getInlineSearchInput(
99-
<InlineSearchInput value="1234" onChange={handleEventMock} />,
99+
<InlineSearchInput onChange={handleEventMock} />,
100100
);
101101

102-
if (input && clearButton) {
103-
fireEvent.change(input, { target: { value: 'test' } });
102+
if (!input || !clearButton) return;
104103

105-
expect(input.value).toBe('1234');
104+
fireEvent.change(input, { target: { value: 'test' } });
106105

107-
fireEvent.click(clearButton);
106+
expect(input.value).toBe('test');
108107

109-
expect(handleEventMock).toBeCalledWith({ target: { value: '' } });
110-
}
108+
fireEvent.click(clearButton);
109+
110+
expect(handleEventMock).toBeCalledWith({ target: { value: '' } });
111111

112112
expect(input).toBeDefined();
113113
});

src/components/SearchInput/SearchInput.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ describe('SearchInput', () => {
103103

104104
it('should clear controlled text input', () => {
105105
const { input, clearButton } = getSearchInput(
106-
<SearchInput value="1234" onChange={handleEventMock} />,
106+
<SearchInput onChange={handleEventMock} />,
107107
);
108108

109109
if (input && clearButton) {
110110
fireEvent.change(input, { target: { value: 'test' } });
111111

112-
expect(input.value).toBe('1234');
112+
expect(input.value).toBe('test');
113113

114114
fireEvent.click(clearButton);
115115

src/components/TextInput/TextInput.test.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,25 @@ describe('TextInput', () => {
126126
});
127127

128128
it('should clear controlled text input', () => {
129+
const mockedOnChanged = vi.fn();
130+
129131
const { input, clearButton } = getTextInput(
130-
<TextInput value="1234" onChange={handleEventMock} hasClearButton />,
132+
<TextInput onChange={mockedOnChanged} hasClearButton />,
131133
);
132134

133135
if (input && clearButton) {
134-
fireEvent.change(input, { target: { value: 'test' } });
136+
fireEvent.change(input, { target: { value: 'test value' } });
135137

136-
expect(input.value).toBe('1234');
138+
expect(input.value).toBe('test value');
137139

138140
fireEvent.click(clearButton);
139141

140-
expect(handleEventMock).toBeCalledWith({ target: { value: '' } });
142+
expect(mockedOnChanged).toHaveBeenCalledWith(
143+
expect.objectContaining({
144+
_reactName: 'onChange',
145+
target: expect.objectContaining({ value: '' }),
146+
}),
147+
);
141148
}
142149

143150
expect(input).toBeDefined();

src/components/TextInput/useTextInput.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
ChangeEvent,
32
ChangeEventHandler,
43
MouseEventHandler,
54
FocusEvent,
@@ -53,13 +52,33 @@ export const useTextInput = ({
5352
[onChange],
5453
);
5554

56-
const handleOnClear: MouseEventHandler<HTMLButtonElement> =
57-
useCallback(() => {
58-
setInnerValue('');
59-
onChange?.({
60-
target: { value: '' },
61-
} as ChangeEvent<HTMLInputElement>);
62-
}, [onChange]);
55+
const handleOnClear: MouseEventHandler<HTMLButtonElement> = () => {
56+
setInnerValue('');
57+
58+
const input = containerRef.current?.querySelector('input');
59+
60+
if (!input) return;
61+
62+
const valueSetter = Object.getOwnPropertyDescriptor(
63+
input.constructor.prototype,
64+
'value',
65+
)?.set;
66+
67+
const prototype = Object.getPrototypeOf(input);
68+
const prototypeValueSetter = Object.getOwnPropertyDescriptor(
69+
prototype,
70+
'value',
71+
)?.set;
72+
73+
if (valueSetter && valueSetter !== prototypeValueSetter) {
74+
prototypeValueSetter?.call(input, '');
75+
} else {
76+
valueSetter?.call(input, '');
77+
}
78+
79+
input.dispatchEvent(new Event('input', { bubbles: true }));
80+
};
81+
6382
return {
6483
innerValue,
6584
styles,

0 commit comments

Comments
 (0)