Skip to content

Commit 152db57

Browse files
committed
expect no console
1 parent 405229a commit 152db57

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

.changeset/soft-berries-obey.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
- Extends `useControlledValue` to accept any type.
66
- Adds `updateValue` function in return value. This method triggers a synthetic event to update the value of a controlled or uncontrolled component.
7+
- Adds `initialValue` argument. Used for setting the initial value for uncontrolled components. Without this we may encounter a React error for switching between controlled/uncontrolled inputs
78
- The value of `isControlled` is now immutable after the first render

packages/hooks/src/useControlledValue/useControlledValue.spec.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ const mutableRefMock = {
1515
current: document.createElement('div'),
1616
};
1717

18+
const errorSpy = jest.spyOn(console, 'error');
19+
1820
describe('packages/hooks/useControlledValue', () => {
21+
beforeEach(() => {
22+
errorSpy.mockImplementation(() => {});
23+
});
24+
25+
afterEach(() => {
26+
errorSpy.mockReset();
27+
});
28+
1929
describe('with controlled component', () => {
2030
test('calling with a value sets value and isControlled', () => {
2131
const { result } = renderHook(v => useControlledValue(v), {
@@ -235,6 +245,7 @@ describe('packages/hooks/useControlledValue', () => {
235245
act(() => rerender('apple'));
236246
expect(result.current.isControlled).toBe(false);
237247
expect(result.current.value).toBe('');
248+
expect(errorSpy).not.toHaveBeenCalled();
238249
});
239250
});
240251

@@ -246,12 +257,13 @@ describe('packages/hooks/useControlledValue', () => {
246257
valueProp?: string;
247258
handlerProp?: ChangeEventHandler;
248259
}) => {
260+
const initialVal = '';
249261
const inputRef = useRef<HTMLInputElement>(null);
250262
// eslint-disable-next-line react-hooks/rules-of-hooks
251263
const { value, handleChange, updateValue } = useControlledValue(
252264
valueProp,
253265
handlerProp,
254-
'',
266+
initialVal,
255267
);
256268

257269
return (
@@ -318,6 +330,7 @@ describe('packages/hooks/useControlledValue', () => {
318330
const result = render(<TestComponent />);
319331
const input = result.getByTestId('test-input');
320332
expect(input).toHaveValue('');
333+
expect(errorSpy).not.toHaveBeenCalled();
321334
});
322335

323336
test('user interaction triggers handler', () => {

0 commit comments

Comments
 (0)