Fix SearchBar fake timer tests for latest user-event.

Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Eric Peterson
2022-04-01 16:39:13 +02:00
committed by Johan Haals
parent f177208035
commit 6303fe61e3
@@ -108,6 +108,7 @@ describe('SearchBar', () => {
});
it('Updates term state when text is entered', async () => {
const user = userEvent.setup({ delay: null });
jest.useFakeTimers();
const defaultDebounceTime = 200;
@@ -124,7 +125,7 @@ describe('SearchBar', () => {
const value = 'value';
await userEvent.type(textbox, value);
await user.type(textbox, value);
act(() => {
jest.advanceTimersByTime(defaultDebounceTime);
@@ -137,6 +138,7 @@ describe('SearchBar', () => {
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({ term: value }),
);
jest.useRealTimers();
});
it('Clear button clears term state', async () => {
@@ -172,12 +174,15 @@ describe('SearchBar', () => {
</ApiProvider>,
);
expect(
screen.queryByRole('button', { name: 'Clear' }),
).not.toBeInTheDocument();
await waitFor(() => {
expect(
screen.queryByRole('button', { name: 'Clear' }),
).not.toBeInTheDocument();
});
});
it('Adheres to provided debounceTime', async () => {
const user = userEvent.setup({ delay: null });
jest.useFakeTimers();
const debounceTime = 600;
@@ -199,7 +204,7 @@ describe('SearchBar', () => {
const value = 'value';
await userEvent.type(textbox, value);
await user.type(textbox, value);
expect(query).not.toHaveBeenLastCalledWith(
expect.objectContaining({ term: value }),
@@ -216,9 +221,11 @@ describe('SearchBar', () => {
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({ term: value }),
);
jest.useRealTimers();
});
it('does not capture analytics event if not enabled in app', async () => {
const user = userEvent.setup({ delay: null });
jest.useFakeTimers();
const debounceTime = 600;
@@ -240,7 +247,7 @@ describe('SearchBar', () => {
const value = 'value';
await userEvent.type(textbox, value);
await user.type(textbox, value);
act(() => {
jest.advanceTimersByTime(debounceTime);
@@ -249,9 +256,11 @@ describe('SearchBar', () => {
await waitFor(() => expect(textbox).toHaveValue(value));
expect(analyticsApiSpy.getEvents()).toHaveLength(0);
jest.useRealTimers();
});
it('captures analytics events if enabled in app', async () => {
const user = userEvent.setup({ delay: null });
jest.useFakeTimers();
const debounceTime = 600;
@@ -296,7 +305,7 @@ describe('SearchBar', () => {
const value = 'value';
await userEvent.type(textbox, value);
await user.type(textbox, value);
expect(analyticsApiSpy.getEvents()).toHaveLength(0);
@@ -318,10 +327,10 @@ describe('SearchBar', () => {
subject: 'value',
});
userEvent.clear(textbox);
await user.clear(textbox);
// make sure new term is captured
await userEvent.type(textbox, 'new value');
await user.type(textbox, 'new value');
act(() => {
jest.advanceTimersByTime(debounceTime);
@@ -340,5 +349,6 @@ describe('SearchBar', () => {
},
subject: 'new value',
});
jest.useRealTimers();
});
});