import React from 'react';
import { mount } from 'enzyme';
import { act } from 'react-dom/test-utils';
import useDebounce from '../src/useDebounce';
jest.useFakeTimers();
type Props = {
text: any;
};
function Component({ text }: Props) {
const [value, setValue] = useDebounce(text, 1000);
return
setValue(`${value} World!`)}>{value}
;
}
describe('useDebounce Hook', () => {
test('set initial value in first render', () => {
const component = mount();
expect(component.text()).toBe('Hello');
});
test('update value when after debounce timer end', () => {
const component = mount();
expect(component.text()).toBe('Hello');
// onclick to update
act(() => {
component.simulate('click');
});
expect(component.text()).toBe('Hello');
act(() => {
jest.runAllTimers();
});
// after Debounce Timer ended, update text
expect(component.text()).toBe('Hello World!');
});
test('cancel to debouce update', () => {
function Component({ text }: Props) {
const [value, setValue, cancel] = useDebounce(text, 1000);
return (
{value}
);
}
const component = mount();
expect(component.find('span').text()).toBe('Hello');
// update
act(() => {
component.find('.update').simulate('click');
});
expect(component.find('span').text()).toBe('Hello');
// timer
act(() => {
jest.setTimeout(500);
});
// before debounce Timer end
expect(component.find('span').text()).toBe('Hello');
// cacncel event
act(() => {
component.find('.cancel').simulate('click');
jest.runAllTimers();
});
expect(component.find('span').text()).toBe('Hello');
});
});