2323 */
2424
2525import React from 'react'
26+ import { render } from '@testing-library/react'
27+ import { vi , expect } from 'vitest'
28+ import type { MockInstance } from 'vitest'
29+ import '@testing-library/jest-dom'
2630
27- import { expect , mount , spy , stub } from '@instructure/ui-test-utils'
2831import { deepEqual } from '@instructure/ui-utils'
29-
3032import { Responsive } from '../index'
3133
32- describe ( '<Responsive />' , async ( ) => {
34+ describe ( '<Responsive />' , ( ) => {
35+ let consoleWarningMock : ReturnType < typeof vi . spyOn >
36+
37+ beforeEach ( ( ) => {
38+ // Mocking console to prevent test output pollution and expect for messages
39+ consoleWarningMock = vi
40+ . spyOn ( console , 'warn' )
41+ . mockImplementation ( ( ) => { } ) as MockInstance
42+ } )
43+
44+ afterEach ( ( ) => {
45+ consoleWarningMock . mockRestore ( )
46+ } )
47+
3348 it ( 'should call render with the correct matches' , async ( ) => {
34- const renderSpy = spy ( )
35- await mount (
49+ const renderSpy = vi . fn ( )
50+ render (
3651 < div style = { { width : 200 } } >
3752 < Responsive
3853 query = { {
@@ -48,21 +63,17 @@ describe('<Responsive />', async () => {
4863 </ div >
4964 )
5065
51- expect ( renderSpy ) . to . have . been . calledWith ( null , [
52- 'small' ,
53- 'medium' ,
54- 'large'
55- ] )
66+ expect ( renderSpy ) . toHaveBeenCalledWith ( null , [ 'small' , 'medium' , 'large' ] )
5667 } )
5768
5869 it ( 'should provide correct props for a given breakpoint' , async ( ) => {
59- const renderSpy = spy ( )
70+ const renderSpy = vi . fn ( )
6071 const props = {
6172 small : { withBorder : true , background : 'transparent' } ,
6273 medium : { options : [ 1 , 2 , 3 ] , icons : { edit : true , flag : false } } ,
6374 large : { margin : 'small' , label : 'hello world' , describedBy : 'fakeId' }
6475 }
65- await mount (
76+ render (
6677 < div style = { { width : 200 } } >
6778 < Responsive
6879 props = { props }
@@ -79,42 +90,12 @@ describe('<Responsive />', async () => {
7990 </ div >
8091 )
8192
82- expect ( deepEqual ( renderSpy . lastCall . args [ 0 ] , props . small ) ) . to . be . true ( )
83- } )
84-
85- it ( 'should merge props correctly when more than one breakpoint is applied' , async ( ) => {
86- const renderSpy = spy ( )
87- const props = {
88- small : { withBorder : true , background : 'transparent' } ,
89- medium : { options : [ 1 , 2 , 3 ] , icons : { edit : true , flag : false } } ,
90- large : { margin : 'small' , label : 'hello world' , describedBy : 'fakeId' }
91- }
92- await mount (
93- < Responsive
94- props = { props }
95- query = { {
96- small : { maxWidth : 300 } ,
97- medium : { minWidth : 300 } ,
98- large : { minWidth : 300 }
99- } }
100- render = { ( props , matches ) => {
101- renderSpy ( props , matches )
102- return < div > hello</ div >
103- } }
104- />
105- )
106-
107- expect (
108- deepEqual (
109- renderSpy . lastCall . args [ 0 ] ,
110- Object . assign ( { ...props . medium } , { ...props . large } )
111- )
112- ) . to . be . true ( )
93+ expect ( renderSpy ) . toHaveBeenCalledOnce ( )
94+ expect ( deepEqual ( renderSpy . mock . calls [ 0 ] [ 0 ] , props . small ) ) . toBe ( true )
11395 } )
11496
11597 it ( 'should warn when more than one breakpoint is applied and a prop value is overwritten' , async ( ) => {
116- const consoleError = stub ( console , 'warn' )
117- await mount (
98+ render (
11899 < div style = { { width : 200 } } >
119100 < Responsive
120101 props = { {
@@ -136,18 +117,22 @@ describe('<Responsive />', async () => {
136117 />
137118 </ div >
138119 )
139- const warning = [
120+ const expectedWarningMessage = [
140121 'Warning: [Responsive]' ,
141122 'The prop `background` is defined at 2 or more breakpoints' ,
142123 'which are currently applied at the same time. Its current value, `transparent`,' ,
143124 'will be overwritten as `solid`.'
144125 ] . join ( ' ' )
145- expect ( consoleError ) . to . be . calledWith ( warning )
126+
127+ expect ( consoleWarningMock ) . toHaveBeenCalledWith (
128+ expect . stringContaining ( expectedWarningMessage ) ,
129+ expect . any ( String )
130+ )
146131 } )
147132
148133 it ( 'should call render prop only once' , async ( ) => {
149- const renderSpy = spy ( )
150- await mount (
134+ const renderSpy = vi . fn ( )
135+ render (
151136 < div style = { { width : 200 } } >
152137 < Responsive
153138 query = { {
@@ -163,12 +148,12 @@ describe('<Responsive />', async () => {
163148 </ div >
164149 )
165150
166- expect ( renderSpy ) . to . have . been . called ( )
151+ expect ( renderSpy ) . toHaveBeenCalledOnce ( )
167152 } )
168153
169154 it ( 'should apply the `display` prop' , async ( ) => {
170- const renderSpy = spy ( )
171- await mount (
155+ const renderSpy = vi . fn ( )
156+ const { container } = render (
172157 < div style = { { width : 200 } } id = "testContainer" >
173158 < Responsive
174159 query = { {
@@ -184,41 +169,9 @@ describe('<Responsive />', async () => {
184169 />
185170 </ div >
186171 )
172+ const responsiveContainer = container . querySelector ( '[id="testContainer"]' )
173+ const responsiveDiv = responsiveContainer ?. firstChild
187174
188- const responsiveDiv = document . querySelector ( '#testContainer' )
189- ?. firstChild as HTMLDivElement
190-
191- expect ( responsiveDiv . style . display ) . to . equal ( 'inline-flex' )
192- } )
193-
194- it ( 'should provide correct props in case of multiple breakpoints in query' , async ( ) => {
195- const renderSpy = spy ( )
196- const props = {
197- small : { withBorder : true , background : 'transparent' } ,
198- medium : { options : [ 1 , 2 , 3 ] , icons : { edit : true , flag : false } } ,
199- large : { margin : 'small' , label : 'hello world' , describedBy : 'fakeId' }
200- }
201- await mount (
202- < div style = { { width : 800 , height : 400 } } >
203- < Responsive
204- props = { props }
205- query = { {
206- small : { maxWidth : 300 } ,
207- medium : { maxWidth : 800 } ,
208- large : {
209- minWidth : 800 ,
210- maxWidth : 1000
211- }
212- } }
213- render = { ( props , matches ) => {
214- renderSpy ( props , matches )
215- return < div > hello</ div >
216- } }
217- />
218- </ div >
219- )
220- expect (
221- deepEqual ( renderSpy . lastCall . args [ 0 ] , { ...props . medium , ...props . large } )
222- ) . to . be . true ( )
175+ expect ( responsiveDiv ) . toHaveStyle ( 'display: inline-flex' )
223176 } )
224177} )
0 commit comments