1
1
import React , { createElement } from 'preact/compat' ;
2
2
import { setupRerender } from 'preact/test-utils' ;
3
3
import { setupScratch , teardown } from '../../../test/_util/helpers' ;
4
+ import { vi } from 'vitest' ;
4
5
5
6
describe ( 'PureComponent' , ( ) => {
6
7
/** @type {HTMLDivElement } */
@@ -23,7 +24,7 @@ describe('PureComponent', () => {
23
24
} ) ;
24
25
25
26
it ( 'should pass props in constructor' , ( ) => {
26
- let spy = sinon . spy ( ) ;
27
+ let spy = vi . fn ( ) ;
27
28
class Foo extends React . PureComponent {
28
29
constructor ( props ) {
29
30
super ( props ) ;
@@ -34,7 +35,7 @@ describe('PureComponent', () => {
34
35
React . render ( < Foo foo = "bar" /> , scratch ) ;
35
36
36
37
let expected = { foo : 'bar' } ;
37
- expect ( spy ) . to . be . calledWithMatch ( expected , expected ) ;
38
+ expect ( spy ) . toHaveBeenCalledWith ( expected , expected ) ;
38
39
} ) ;
39
40
40
41
it ( 'should pass context in constructor' , ( ) => {
@@ -54,14 +55,14 @@ describe('PureComponent', () => {
54
55
}
55
56
}
56
57
57
- sinon . spy ( Foo . prototype , 'render' ) ;
58
+ vi . spyOn ( Foo . prototype , 'render' ) ;
58
59
59
60
const PROPS = { foo : 'bar' } ;
60
61
React . render ( < Foo { ...PROPS } /> , scratch ) ;
61
62
62
- expect ( Foo . prototype . render )
63
- . to . have . been . calledOnce . and . to . have . been . calledWithMatch ( PROPS , { } , { } )
64
- . and . to . have . returned ( sinon . match ( { type : 'div' , props : PROPS } ) ) ;
63
+ expect ( Foo . prototype . render ) . toHaveBeenCalledOnce ( ) ;
64
+ expect ( Foo . prototype . render ) . toHaveBeenCalledWith ( PROPS , { } , { } ) ;
65
+ expect ( Foo . prototype . render ) . toHaveReturned ( { type : 'div' , props : PROPS } ) ;
65
66
expect ( instance . props ) . to . deep . equal ( PROPS ) ;
66
67
expect ( instance . state ) . to . deep . equal ( { } ) ;
67
68
expect ( instance . context ) . to . deep . equal ( { } ) ;
@@ -70,8 +71,8 @@ describe('PureComponent', () => {
70
71
} ) ;
71
72
72
73
it ( 'should ignore the __source variable' , ( ) => {
73
- const pureSpy = sinon . spy ( ) ;
74
- const appSpy = sinon . spy ( ) ;
74
+ const pureSpy = vi . fn ( ) ;
75
+ const appSpy = vi . fn ( ) ;
75
76
/** @type {(v) => void } */
76
77
let set ;
77
78
class Pure extends React . PureComponent {
@@ -89,13 +90,13 @@ describe('PureComponent', () => {
89
90
} ;
90
91
91
92
React . render ( < App /> , scratch ) ;
92
- expect ( appSpy ) . to . be . calledOnce ;
93
- expect ( pureSpy ) . to . be . calledOnce ;
93
+ expect ( appSpy ) . toHaveBeenCalledOnce ( ) ;
94
+ expect ( pureSpy ) . toHaveBeenCalledOnce ( ) ;
94
95
95
96
set ( 1 ) ;
96
97
rerender ( ) ;
97
- expect ( appSpy ) . to . be . calledTwice ;
98
- expect ( pureSpy ) . to . be . calledOnce ;
98
+ expect ( appSpy ) . toHaveBeenCalledTimes ( 2 ) ;
99
+ expect ( pureSpy ) . toHaveBeenCalledOnce ( ) ;
99
100
} ) ;
100
101
101
102
it ( 'should only re-render when props or state change' , ( ) => {
@@ -104,39 +105,39 @@ describe('PureComponent', () => {
104
105
return < div /> ;
105
106
}
106
107
}
107
- let spy = sinon . spy ( C . prototype , 'render' ) ;
108
+ let spy = vi . spyOn ( C . prototype , 'render' ) ;
108
109
109
110
let inst = React . render ( < C /> , scratch ) ;
110
- expect ( spy ) . to . have . been . calledOnce ;
111
- spy . resetHistory ( ) ;
111
+ expect ( spy ) . toHaveBeenCalledOnce ( ) ;
112
+ spy . mockClear ( ) ;
112
113
113
114
inst = React . render ( < C /> , scratch ) ;
114
- expect ( spy ) . not . to . have . been . called ;
115
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
115
116
116
117
let b = { foo : 'bar' } ;
117
118
inst = React . render ( < C a = "a" b = { b } /> , scratch ) ;
118
- expect ( spy ) . to . have . been . calledOnce ;
119
- spy . resetHistory ( ) ;
119
+ expect ( spy ) . toHaveBeenCalledOnce ( ) ;
120
+ spy . mockClear ( ) ;
120
121
121
122
inst = React . render ( < C a = "a" b = { b } /> , scratch ) ;
122
- expect ( spy ) . not . to . have . been . called ;
123
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
123
124
124
125
inst . setState ( { } ) ;
125
126
rerender ( ) ;
126
- expect ( spy ) . not . to . have . been . called ;
127
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
127
128
128
129
inst . setState ( { a : 'a' , b } ) ;
129
130
rerender ( ) ;
130
- expect ( spy ) . to . have . been . calledOnce ;
131
- spy . resetHistory ( ) ;
131
+ expect ( spy ) . toHaveBeenCalledOnce ( ) ;
132
+ spy . mockClear ( ) ;
132
133
133
134
inst . setState ( { a : 'a' , b } ) ;
134
135
rerender ( ) ;
135
- expect ( spy ) . not . to . have . been . called ;
136
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
136
137
} ) ;
137
138
138
139
it ( 'should update when props are removed' , ( ) => {
139
- let spy = sinon . spy ( ) ;
140
+ let spy = vi . fn ( ) ;
140
141
class App extends React . PureComponent {
141
142
render ( ) {
142
143
spy ( ) ;
@@ -146,7 +147,7 @@ describe('PureComponent', () => {
146
147
147
148
React . render ( < App a = "foo" /> , scratch ) ;
148
149
React . render ( < App /> , scratch ) ;
149
- expect ( spy ) . to . be . calledTwice ;
150
+ expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
150
151
} ) ;
151
152
152
153
it ( 'should have "isPureReactComponent" property' , ( ) => {
0 commit comments