1- /* eslint-disable no-empty-pattern */
2- import { cleanup , render } from '@testing-library/react' ;
3- import userEvent from '@testing-library/user-event' ;
1+ import { userEvent } from "@vitest/browser/context" ;
42import { afterEach , beforeEach , expect , test as baseTest , vi } from "vitest" ;
3+ import { cleanup , render } from "vitest-browser-react" ;
54import MultiItemDisplay from "../MultiItemDisplay" ;
65
76const items = [
@@ -22,7 +21,6 @@ afterEach(() => {
2221} ) ;
2322
2423// Reference for keyboard events: https://testing-library.com/docs/user-event/keyboard/
25- const user = userEvent . setup ( ) ;
2624
2725const test = baseTest . extend < {
2826 component : ReturnType < typeof render >
@@ -32,88 +30,92 @@ const test = baseTest.extend<{
3230
3331test ( 'Changing the currently selected value using the keyboard' , async ( { component } ) => {
3432 // Check that the first item is being displayed
35- expect ( component . queryByText ( 'Item One' ) ) . not . toBeNull ( ) ;
33+ await expect . element ( component . getByText ( 'Item One' ) ) . toBeVisible ( ) ;
3634
37- await user . tab ( ) ;
38- await user . keyboard ( '2[Enter]' ) ;
35+ await userEvent . keyboard ( '[Tab]2[Enter]' ) ;
3936
4037 // Check that the first item is no longer being displayed
41- expect ( component . queryByText ( 'Item One' ) ) . toBeNull ( ) ;
38+ await expect . element ( component . getByText ( 'Item One' ) ) . not . toBeInTheDocument ( ) ;
4239
4340 // Check that the second item is being displayed
44- expect ( component . queryByText ( 'Item Two' ) ) . not . toBeNull ( ) ;
41+ await expect . element ( component . getByText ( 'Item Two' ) ) . toBeVisible ( ) ;
4542
4643 // and that the step change handler was called
4744 expect ( stepChangeHandler ) . toHaveBeenCalledTimes ( 1 ) ;
4845} ) ;
4946
5047test ( 'Changing the currently selected value via the buttons' , async ( { component } ) => {
5148 // Check that the first item is being displayed
52- expect ( component . queryByText ( 'Item One' ) ) . not . toBeNull ( ) ;
49+ await expect . element ( component . getByText ( 'Item One' ) ) . toBeVisible ( ) ;
5350
5451 // Click the "Next" button
5552 const nextButton = component . getByText ( 'Next' ) ;
56- await user . click ( nextButton ) ;
53+ await userEvent . click ( nextButton ) ;
5754
5855 // Check that the second item is being displayed and not the first
59- expect ( component . queryByText ( 'Item One' ) ) . toBeNull ( ) ;
60- expect ( component . queryByText ( 'Item Two' ) ) . not . toBeNull ( ) ;
56+ await expect . element ( component . getByText ( 'Item One' ) ) . not . toBeInTheDocument ( ) ;
57+ await expect . element ( component . getByText ( 'Item Two' ) ) . toBeVisible ( ) ;
6158
6259 // Click the "Previous" button
6360 const prevButton = component . getByText ( 'Previous' ) ;
64- await user . click ( prevButton ) ;
61+ await userEvent . click ( prevButton ) ;
6562
6663 // Check that the first item is being displayed and not the second
67- expect ( component . queryByText ( 'Item Two ' ) ) . toBeNull ( ) ;
68- expect ( component . queryByText ( 'Item One ' ) ) . not . toBeNull ( ) ;
64+ await expect . element ( component . getByText ( 'Item One ' ) ) . toBeVisible ( ) ;
65+ await expect . element ( component . getByText ( 'Item Two ' ) ) . not . toBeInTheDocument ( ) ;
6966
7067 expect ( stepChangeHandler ) . toHaveBeenCalledTimes ( 2 ) ;
7168} ) ;
7269
7370test ( 'The item selector won\'t accept the too many digits' , async ( { component } ) => {
7471 // Check that the first item was rendered
75- expect ( component . queryByText ( 'Item One' ) ) . not . toBeNull ( ) ;
72+ await expect . element ( component . getByText ( 'Item One' ) ) . toBeVisible ( ) ;
7673
7774 // Try to enter an invalid value
78- await userEvent . tab ( ) ;
79- await userEvent . keyboard ( '1234[Enter]' ) ;
75+ await userEvent . keyboard ( '[Tab]1234[Enter]' ) ;
8076
8177 // Check that the first item is still rendered
82- expect ( component . queryByText ( 'Item One' ) ) . not . toBeNull ( ) ;
78+ await expect . element ( component . getByText ( 'Item One' ) ) . toBeVisible ( ) ;
8379
8480 // and that the display didn't change
8581 expect ( stepChangeHandler ) . not . toHaveBeenCalled ( ) ;
8682} ) ;
8783
8884test ( 'The item selector will reset when all digits are removed' , async ( { component } ) => {
8985 // Check that the first item was rendered
90- expect ( component . queryByText ( 'Item One' ) ) . not . toBeNull ( ) ;
86+ await expect . element ( component . getByText ( 'Item One' ) ) . toBeVisible ( ) ;
9187
9288 // Clear the selector
93- await userEvent . tab ( ) ;
94- await userEvent . keyboard ( '[Backspace][Enter]' ) ;
89+ await userEvent . keyboard ( '[Tab][Backspace][Enter]' ) ;
9590
9691 // Check that the first item is still rendered
97- expect ( component . queryByText ( 'Item One' ) ) . not . toBeNull ( ) ;
92+ await expect . element ( component . getByText ( 'Item One' ) ) . toBeVisible ( ) ;
9893
9994 // and that the display didn't change
10095 expect ( stepChangeHandler ) . not . toHaveBeenCalled ( ) ;
10196} ) ;
10297
10398test ( 'The item selector moves to the end when set out of range' , async ( { component } ) => {
10499 // Check that the first item is rendered
105- expect ( component . queryByText ( 'Item One' ) ) . not . toBeNull ( ) ;
100+ await expect . element ( component . getByText ( 'Item One' ) ) . toBeVisible ( ) ;
106101
107- await userEvent . tab ( ) ;
108- await userEvent . keyboard ( '5[Enter]' ) ;
102+ await userEvent . keyboard ( '[Tab]5[Enter]' ) ;
109103
110104 // Check that the first item is no longer rendered
111- expect ( component . queryByText ( 'Item One' ) ) . toBeNull ( ) ;
105+ await expect . element ( component . getByText ( 'Item One' ) ) . not . toBeInTheDocument ( ) ;
112106
113107 // Check that the fourth item is now rendered
114- expect ( component . queryByText ( 'Item Four' ) ) . not . toBeNull ( ) ;
108+ await expect . element ( component . getByText ( 'Item Four' ) ) . toBeVisible ( ) ;
115109
116110 // and check that the step change only happened once
117111 expect ( stepChangeHandler ) . toHaveBeenCalledTimes ( 1 ) ;
118112 expect ( stepChangeHandler ) . toHaveBeenCalledWith ( 3 , 0 ) ;
119113} ) ;
114+
115+ test ( 'Both selector buttons are disabled when there is only one item' , async ( ) => {
116+ const component = render ( < MultiItemDisplay elements = { [ < p > Item One</ p > ] } /> ) ;
117+ const buttonLocator = component . getByRole ( 'button' ) ;
118+
119+ await expect . element ( buttonLocator . first ( ) ) . toBeDisabled ( ) ;
120+ await expect . element ( buttonLocator . nth ( 1 ) ) . toBeDisabled ( ) ;
121+ } ) ;
0 commit comments