11'use strict' ;
22
33const assert = require ( 'assert' ) ;
4+ const jsdom = require ( 'jsdom-global' ) ;
45const sinon = require ( 'sinon' ) ;
6+
57const React = require ( 'react' ) ;
68const ReactTestRenderer = require ( 'react-test-renderer' ) ;
79
10+ const Preact = require ( 'preact/compat' ) ;
11+ const PreactTestRenderer = require ( '@testing-library/preact' ) ;
12+
813const equal = require ( '../..' ) ;
914const tests = require ( './tests' ) ;
1015
11- class ChildWithShouldComponentUpdate extends React . Component {
16+ class ReactChild extends React . Component {
1217 shouldComponentUpdate ( nextProps ) {
1318 // this.props.children is a h1 with a circular reference to its owner, Container
1419 return ! equal ( this . props , nextProps ) ;
@@ -18,72 +23,151 @@ class ChildWithShouldComponentUpdate extends React.Component {
1823 }
1924}
2025
21- class Container extends React . Component {
26+ class ReactContainer extends React . Component {
2227 render ( ) {
23- return React . createElement ( ChildWithShouldComponentUpdate , {
28+ return React . createElement ( ReactChild , {
2429 children : [
2530 React . createElement ( 'h1' , this . props . title || '' ) ,
26- React . createElement ( 'h2' , this . props . subtitle || '' )
27- ]
31+ React . createElement ( 'h2' , this . props . subtitle || '' ) ,
32+ ] ,
33+ } ) ;
34+ }
35+ }
36+
37+ class PreactChild extends Preact . Component {
38+ shouldComponentUpdate ( nextProps ) {
39+ // this.props.children is a h1 with a circular reference to its owner, Container
40+ return ! equal ( this . props , nextProps ) ;
41+ }
42+ render ( ) {
43+ return null ;
44+ }
45+ }
46+
47+ class PreactContainer extends Preact . Component {
48+ render ( ) {
49+ return Preact . createElement ( PreactChild , {
50+ children : [
51+ Preact . createElement ( 'h1' , this . props . title || '' ) ,
52+ Preact . createElement ( 'h2' , this . props . subtitle || '' ) ,
53+ ] ,
2854 } ) ;
2955 }
3056}
3157
3258describe ( 'advanced' , ( ) => {
3359 let sandbox ;
3460 let warnStub ;
35- let childRenderSpy ;
3661
3762 beforeEach ( ( ) => {
3863 sandbox = sinon . createSandbox ( ) ;
3964 warnStub = sandbox . stub ( console , 'warn' ) ;
40- childRenderSpy = sandbox . spy ( ChildWithShouldComponentUpdate . prototype , 'render' ) ;
4165 } ) ;
4266
4367 afterEach ( ( ) => {
4468 sandbox . restore ( ) ;
4569 } ) ;
4670
4771 describe ( 'React' , ( ) => {
72+ let reactChildRenderSpy ;
73+
74+ beforeEach ( ( ) => {
75+ reactChildRenderSpy = sandbox . spy ( ReactChild . prototype , 'render' ) ;
76+ } ) ;
77+
78+ describe ( 'element (with circular references)' , ( ) => {
79+ it ( 'compares without warning or errors' , ( ) => {
80+ const reactApp = ReactTestRenderer . create (
81+ React . createElement ( ReactContainer )
82+ ) ;
83+ reactApp . update ( React . createElement ( ReactContainer ) ) ;
84+ assert . strictEqual ( warnStub . callCount , 0 ) ;
85+ } ) ;
86+ it ( 'elements of same type and props are equal' , ( ) => {
87+ const reactApp = ReactTestRenderer . create (
88+ React . createElement ( ReactContainer )
89+ ) ;
90+ reactApp . update ( React . createElement ( ReactContainer ) ) ;
91+ assert . strictEqual ( reactChildRenderSpy . callCount , 1 ) ;
92+ } ) ;
93+ it ( 'elements of same type with different props are not equal' , ( ) => {
94+ const reactApp = ReactTestRenderer . create (
95+ React . createElement ( ReactContainer )
96+ ) ;
97+ reactApp . update ( React . createElement ( ReactContainer , { title : 'New' } ) ) ;
98+ assert . strictEqual ( reactChildRenderSpy . callCount , 2 ) ;
99+ } ) ;
100+ } ) ;
101+ } ) ;
102+
103+ describe ( 'Preact' , ( ) => {
104+ let cleanupJsDom ;
105+ let preactChildRenderSpy ;
106+
107+ beforeEach ( ( ) => {
108+ cleanupJsDom = jsdom ( ) ;
109+ preactChildRenderSpy = sandbox . spy ( PreactChild . prototype , 'render' ) ;
110+ } ) ;
111+
112+ afterEach ( ( ) => {
113+ PreactTestRenderer . cleanup ( ) ;
114+ if ( cleanupJsDom ) cleanupJsDom ( ) ;
115+ } ) ;
116+
48117 describe ( 'element (with circular references)' , ( ) => {
49118 it ( 'compares without warning or errors' , ( ) => {
50- const testRenderer = ReactTestRenderer . create ( React . createElement ( Container ) ) ;
51- testRenderer . update ( React . createElement ( Container ) ) ;
119+ const { rerender } = PreactTestRenderer . render (
120+ Preact . createElement ( PreactContainer )
121+ ) ;
122+ rerender ( Preact . createElement ( PreactContainer ) ) ;
52123 assert . strictEqual ( warnStub . callCount , 0 ) ;
53124 } ) ;
54125 it ( 'elements of same type and props are equal' , ( ) => {
55- const testRenderer = ReactTestRenderer . create ( React . createElement ( Container ) ) ;
56- testRenderer . update ( React . createElement ( Container ) ) ;
57- assert . strictEqual ( childRenderSpy . callCount , 1 ) ;
126+ const { rerender } = PreactTestRenderer . render (
127+ Preact . createElement ( PreactContainer )
128+ ) ;
129+ rerender ( Preact . createElement ( PreactContainer ) ) ;
130+ assert . strictEqual ( preactChildRenderSpy . callCount , 1 ) ;
58131 } ) ;
59132 it ( 'elements of same type with different props are not equal' , ( ) => {
60- const testRenderer = ReactTestRenderer . create ( React . createElement ( Container ) ) ;
61- testRenderer . update ( React . createElement ( Container , { title : 'New' } ) ) ;
62- assert . strictEqual ( childRenderSpy . callCount , 2 ) ;
133+ const { rerender } = PreactTestRenderer . render (
134+ Preact . createElement ( PreactContainer )
135+ ) ;
136+ rerender ( Preact . createElement ( PreactContainer , { title : 'New' } ) ) ;
137+ assert . strictEqual ( preactChildRenderSpy . callCount , 2 ) ;
63138 } ) ;
64139 } ) ;
65140 } ) ;
66141
67142 describe ( 'warnings' , ( ) => {
68143 describe ( 'circular reference' , ( ) => {
69144 it ( 'warns on circular refs but do not throw' , ( ) => {
70- const circularA = { a : 1 } ;
145+ const circularA = { a : 1 } ;
71146 circularA . self = circularA ;
72- const circularB = { a : 1 } ;
147+ const circularB = { a : 1 } ;
73148 circularB . self = circularB ;
74149 equal ( circularA , circularB ) ;
75150 assert . strictEqual ( warnStub . callCount , 1 ) ;
76151 } ) ;
77152 } ) ;
78153 describe ( 'basics usage' , ( ) => {
79154 it ( 'never warns' , ( ) => {
80- tests . generic . forEach ( ( suite ) => {
81- suite . tests . forEach ( ( test ) => {
82- assert . strictEqual ( equal ( test . value1 , test . value2 ) , test . equal , test . description ) ;
155+ tests . generic . forEach ( ( suite ) => {
156+ suite . tests . forEach ( ( test ) => {
157+ assert . strictEqual (
158+ equal ( test . value1 , test . value2 ) ,
159+ test . equal ,
160+ test . description
161+ ) ;
83162 } ) ;
84163 } ) ;
85- assert . strictEqual ( warnStub . callCount , 0 ,
86- `console.warn called ${ warnStub . callCount } with arguments: ${ JSON . stringify ( warnStub . args ) } ` ) ;
164+ assert . strictEqual (
165+ warnStub . callCount ,
166+ 0 ,
167+ `console.warn called ${
168+ warnStub . callCount
169+ } with arguments: ${ JSON . stringify ( warnStub . args ) } `
170+ ) ;
87171 } ) ;
88172 } ) ;
89173 } ) ;
0 commit comments