Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix flaky test on simpleField #259

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions tests/components/form/fields/simpleField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import React from 'react'
import { describe, expect, jest, test } from '@jest/globals';
import { afterEach, beforeEach, describe, expect, jest, test } from '@jest/globals';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event'
import '@testing-library/jest-dom/jest-globals'
Expand Down Expand Up @@ -64,8 +64,18 @@ const theme: Theme = buildTheme({
type Model = { simple: string }

describe('DOM testing', () => {

beforeEach(() => {
jest.useFakeTimers();
})

afterEach(() => {
jest.runOnlyPendingTimers()
jest.useRealTimers();
});

test('default settings', async () => {
const user = userEvent.setup()
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime })

const key = 'simple'
const label = 'simple'
Expand Down Expand Up @@ -172,7 +182,7 @@ describe('DOM testing', () => {
hint: 'value.match'
})

const user = userEvent.setup()
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime })

const key = 'simple'
const label = 'simple'
Expand Down Expand Up @@ -215,6 +225,9 @@ describe('DOM testing', () => {
const invalidValue = 'ILoveApples'
await user.clear(input)
await user.type(input, invalidValue)

// Fast-forward until all timers have been executed (handle year debounced value)
await jest.runOnlyPendingTimersAsync();

expect(onFieldChange).toHaveBeenLastCalledWith(
expect.objectContaining({
Expand Down