|
2 | 2 | * External dependencies |
3 | 3 | */ |
4 | 4 | import { render, screen } from '@testing-library/react'; |
5 | | -import userEvent from '@testing-library/user-event'; |
6 | 5 |
|
7 | 6 | /** |
8 | 7 | * WordPress dependencies |
9 | 8 | */ |
10 | | -import { Component } from '@wordpress/element'; |
| 9 | +import { logged } from '@wordpress/deprecated'; |
11 | 10 |
|
12 | 11 | /** |
13 | 12 | * Internal dependencies |
14 | 13 | */ |
15 | 14 | import pure from '../'; |
16 | 15 |
|
17 | 16 | describe( 'pure', () => { |
18 | | - it( 'functional component should rerender only when props change', () => { |
19 | | - let i = 0; |
20 | | - const MyComp = pure( () => { |
21 | | - return <p data-testid="counter">{ ++i }</p>; |
22 | | - } ); |
23 | | - const { rerender } = render( <MyComp /> ); |
24 | | - |
25 | | - // Updating with same props doesn't rerender. |
26 | | - rerender( <MyComp /> ); |
27 | | - expect( screen.getByTestId( 'counter' ) ).toHaveTextContent( '1' ); |
28 | | - |
29 | | - // New prop should trigger a rerender. |
30 | | - rerender( <MyComp { ...{ prop: 'a' } } /> ); |
31 | | - expect( screen.getByTestId( 'counter' ) ).toHaveTextContent( '2' ); |
32 | | - |
33 | | - // Keeping the same prop value should not rerender. |
34 | | - rerender( <MyComp { ...{ prop: 'a' } } /> ); |
35 | | - expect( screen.getByTestId( 'counter' ) ).toHaveTextContent( '2' ); |
36 | | - |
37 | | - // Changing the prop value should rerender. |
38 | | - rerender( <MyComp { ...{ prop: 'b' } } /> ); |
39 | | - expect( screen.getByTestId( 'counter' ) ).toHaveTextContent( '3' ); |
| 17 | + afterEach( () => { |
| 18 | + for ( const key in logged ) { |
| 19 | + delete logged[ key ]; |
| 20 | + } |
40 | 21 | } ); |
41 | 22 |
|
42 | | - it( 'class component should rerender if the props or state change', async () => { |
43 | | - const user = userEvent.setup(); |
44 | | - let i = 0; |
45 | | - const MyComp = pure( |
46 | | - class extends Component { |
47 | | - constructor() { |
48 | | - super( ...arguments ); |
49 | | - this.state = { |
50 | | - val: '', |
51 | | - }; |
52 | | - } |
53 | | - render() { |
54 | | - return ( |
55 | | - <> |
56 | | - <p data-testid="counter">{ ++i }</p> |
57 | | - <input |
58 | | - type="text" |
59 | | - value={ this.state.val } |
60 | | - onChange={ ( e ) => |
61 | | - this.setState( { val: e.target.value } ) |
62 | | - } |
63 | | - /> |
64 | | - <input |
65 | | - type="button" |
66 | | - onClick={ () => |
67 | | - this.setState( { val: this.state.val } ) |
68 | | - } |
69 | | - /> |
70 | | - </> |
71 | | - ); |
72 | | - } |
73 | | - } |
74 | | - ); |
75 | | - |
76 | | - const { rerender } = render( <MyComp /> ); |
| 23 | + it( 'wraps a component and logs a deprecation warning', () => { |
| 24 | + const MyComp = pure( () => <p data-testid="content">content</p> ); |
77 | 25 |
|
78 | | - // Updating with same props doesn't rerender. |
79 | | - rerender( <MyComp /> ); |
80 | | - expect( screen.getByTestId( 'counter' ) ).toHaveTextContent( '1' ); |
| 26 | + render( <MyComp /> ); |
81 | 27 |
|
82 | | - // New prop should trigger a rerender. |
83 | | - rerender( <MyComp { ...{ prop: 'a' } } /> ); |
84 | | - expect( screen.getByTestId( 'counter' ) ).toHaveTextContent( '2' ); |
85 | | - |
86 | | - // Keeping the same prop value should not rerender. |
87 | | - rerender( <MyComp { ...{ prop: 'a' } } /> ); |
88 | | - expect( screen.getByTestId( 'counter' ) ).toHaveTextContent( '2' ); |
89 | | - |
90 | | - // Changing the prop value should rerender. |
91 | | - rerender( <MyComp { ...{ prop: 'b' } } /> ); |
92 | | - expect( screen.getByTestId( 'counter' ) ).toHaveTextContent( '3' ); |
93 | | - |
94 | | - // New state value should trigger a rerender. |
95 | | - await user.type( screen.getByRole( 'textbox' ), 'a' ); |
96 | | - expect( screen.getByTestId( 'counter' ) ).toHaveTextContent( '4' ); |
97 | | - |
98 | | - // Keeping the same state value should not trigger a rerender. |
99 | | - await user.click( screen.getByRole( 'button' ) ); |
100 | | - expect( screen.getByTestId( 'counter' ) ).toHaveTextContent( '4' ); |
| 28 | + expect( console ).toHaveWarnedWith( |
| 29 | + 'wp.compose.pure is deprecated since version 7.1. Please use Use `memo` or `PureComponent` instead instead.' |
| 30 | + ); |
| 31 | + expect( screen.getByTestId( 'content' ) ).toHaveTextContent( |
| 32 | + 'content' |
| 33 | + ); |
101 | 34 | } ); |
102 | 35 | } ); |
0 commit comments