What is the recommended approach for dealing with a debounced input in Cypress E2E tests.
For example, let's assume I have the following component that uses lodash/debounce
import React, { useState, useCallback } from 'react';
import _ from 'lodash';
const DebouncedInput = () => {
const [query, setQuery] = useState("");
const [searchResult, setSearchResult] = useState("");
// The debounced function
const searchApi = useCallback(
_.debounce(async (inputValue) => {
setSearchResult(`User searched for: ${inputValue}`);
}, 200),
[]
);
const handleChange = (e) => {
setQuery(e.target.value);
searchApi(e.target.value);
};
return (
<div>
<input
type="text"
value={query}
onChange={handleChange}
placeholder="Type something..."
/>
{searchResult && <p>{searchResult}</p>}
</div>
);
};
export default DebouncedInput;
How would I go about making sure that my Cypress tests won't be flaky?
In my mind there are two ways:
- Dealing with cy.clock() and using cy.tick()
cy.get('[data-cy="foo"]').type('hey').tick(250)
- Taking advantage of
cy.typeand thedelayoption
cy.get('[data-cy="foo"]').type('hey', {delay: 250});
I tried both approaches and seem to work. But I am not sure if there is actually a recommended way to do this or if one apporach is better than the other.
You can use cy.wait(The same delay period of the used debounce).