@@ -6,14 +6,14 @@ function TestHarness({children}: {children: React.ReactNode}) {
6
6
return < React . Fragment > { children } </ React . Fragment > ;
7
7
}
8
8
9
- const Component1 = ( ) => < React . Fragment > { useUniqueId ( ) } </ React . Fragment > ;
10
- const Component2 = ( ) => < React . Fragment > { useUniqueId ( ) } </ React . Fragment > ;
11
- const Component3 = ( ) => < React . Fragment > { useUniqueId ( ) } </ React . Fragment > ;
9
+ const Component1 = ( ) => < div id = { useUniqueId ( ) } / >;
10
+ const Component2 = ( ) => < div id = { useUniqueId ( ) } / >;
11
+ const Component3 = ( ) => < div id = { useUniqueId ( ) } / >;
12
12
13
- const HasPrefix1 = ( ) => < React . Fragment > { useUniqueId ( 'a ' ) } </ React . Fragment > ;
14
- const HasPrefix2 = ( ) => < React . Fragment > { useUniqueId ( 'a ' ) } </ React . Fragment > ;
15
- const HasPrefix3 = ( ) => < React . Fragment > { useUniqueId ( 'a ' ) } </ React . Fragment > ;
16
- const HasPrefix4 = ( ) => < React . Fragment > { useUniqueId ( 'b ' ) } </ React . Fragment > ;
13
+ const HasPrefix1 = ( ) => < div id = { useUniqueId ( 'A ' ) } / >;
14
+ const HasPrefix2 = ( ) => < div id = { useUniqueId ( 'A ' ) } / >;
15
+ const HasPrefix3 = ( ) => < div id = { useUniqueId ( 'A ' ) } / >;
16
+ const HasPrefix4 = ( ) => < div id = { useUniqueId ( 'B ' ) } / >;
17
17
18
18
describe ( 'useUniqueId' , ( ) => {
19
19
it ( 'returns unique IDs across a single component' , ( ) => {
@@ -28,13 +28,12 @@ describe('useUniqueId', () => {
28
28
</ TestHarness > ,
29
29
) ;
30
30
31
- expect ( harness . findAll ( Component1 ) [ 0 ] . html ( ) ) . toStrictEqual ( 'polaris-1' ) ;
32
- expect ( harness . findAll ( Component1 ) [ 1 ] . html ( ) ) . toStrictEqual ( 'polaris-2' ) ;
33
- expect ( harness . findAll ( Component1 ) [ 2 ] . html ( ) ) . toStrictEqual ( 'polaris-3' ) ;
34
-
35
- expect ( harness . findAll ( HasPrefix1 ) [ 0 ] . html ( ) ) . toStrictEqual ( 'polaris-a1' ) ;
36
- expect ( harness . findAll ( HasPrefix1 ) [ 1 ] . html ( ) ) . toStrictEqual ( 'polaris-a2' ) ;
37
- expect ( harness . findAll ( HasPrefix1 ) [ 2 ] . html ( ) ) . toStrictEqual ( 'polaris-a3' ) ;
31
+ expect ( harness . findAll ( 'div' ) [ 0 ] ) . toHaveReactProps ( { id : 'Polaris1' } ) ;
32
+ expect ( harness . findAll ( 'div' ) [ 1 ] ) . toHaveReactProps ( { id : 'Polaris2' } ) ;
33
+ expect ( harness . findAll ( 'div' ) [ 2 ] ) . toHaveReactProps ( { id : 'Polaris3' } ) ;
34
+ expect ( harness . findAll ( 'div' ) [ 3 ] ) . toHaveReactProps ( { id : 'PolarisA1' } ) ;
35
+ expect ( harness . findAll ( 'div' ) [ 4 ] ) . toHaveReactProps ( { id : 'PolarisA2' } ) ;
36
+ expect ( harness . findAll ( 'div' ) [ 5 ] ) . toHaveReactProps ( { id : 'PolarisA3' } ) ;
38
37
} ) ;
39
38
40
39
it ( 'returns unique IDs across multiple components' , ( ) => {
@@ -50,41 +49,39 @@ describe('useUniqueId', () => {
50
49
</ TestHarness > ,
51
50
) ;
52
51
53
- expect ( harness . find ( Component1 ) ! . html ( ) ) . toStrictEqual ( 'polaris-1' ) ;
54
- expect ( harness . find ( Component2 ) ! . html ( ) ) . toStrictEqual ( 'polaris-2' ) ;
55
- expect ( harness . find ( Component3 ) ! . html ( ) ) . toStrictEqual ( 'polaris-3' ) ;
56
-
57
- expect ( harness . find ( HasPrefix1 ) ! . html ( ) ) . toStrictEqual ( 'polaris-a1' ) ;
58
- expect ( harness . find ( HasPrefix2 ) ! . html ( ) ) . toStrictEqual ( 'polaris-a2' ) ;
59
- expect ( harness . find ( HasPrefix3 ) ! . html ( ) ) . toStrictEqual ( 'polaris-a3' ) ;
60
- expect ( harness . find ( HasPrefix4 ) ! . html ( ) ) . toStrictEqual ( 'polaris-b1' ) ;
52
+ expect ( harness . findAll ( 'div' ) [ 0 ] ) . toHaveReactProps ( { id : 'Polaris1' } ) ;
53
+ expect ( harness . findAll ( 'div' ) [ 1 ] ) . toHaveReactProps ( { id : 'Polaris2' } ) ;
54
+ expect ( harness . findAll ( 'div' ) [ 2 ] ) . toHaveReactProps ( { id : 'Polaris3' } ) ;
55
+ expect ( harness . findAll ( 'div' ) [ 3 ] ) . toHaveReactProps ( { id : 'PolarisA1' } ) ;
56
+ expect ( harness . findAll ( 'div' ) [ 4 ] ) . toHaveReactProps ( { id : 'PolarisA2' } ) ;
57
+ expect ( harness . findAll ( 'div' ) [ 5 ] ) . toHaveReactProps ( { id : 'PolarisA3' } ) ;
58
+ expect ( harness . findAll ( 'div' ) [ 6 ] ) . toHaveReactProps ( { id : 'PolarisB1' } ) ;
61
59
} ) ;
62
60
63
61
it ( 'increments multiple IDs within the same component' , ( ) => {
64
62
const HasMultipleIds = ( ) => (
65
- < React . Fragment >
66
- { useUniqueId ( ) } :: { useUniqueId ( ) }
67
- </ React . Fragment >
63
+ < div id = { useUniqueId ( ) } title = { useUniqueId ( ) } />
68
64
) ;
69
65
70
66
const harness = mountWithApp ( < HasMultipleIds /> ) ;
71
67
72
- expect ( harness . html ( ) ) . toStrictEqual ( 'polaris-1 :: polaris-2' ) ;
68
+ expect ( harness . find ( 'div' ) ) . toHaveReactProps ( {
69
+ id : 'Polaris1' ,
70
+ title : 'Polaris2' ,
71
+ } ) ;
73
72
} ) ;
74
73
75
74
it ( 'uses an override if specified' , ( ) => {
76
- const HasOverride = ( ) => (
77
- < React . Fragment > { useUniqueId ( '' , 'overridden' ) } </ React . Fragment >
78
- ) ;
75
+ const HasOverride = ( ) => < div id = { useUniqueId ( '' , 'overridden' ) } /> ;
79
76
80
77
const harness = mountWithApp ( < HasOverride /> ) ;
81
78
82
- expect ( harness . html ( ) ) . toStrictEqual ( 'overridden' ) ;
79
+ expect ( harness . find ( 'div' ) ) . toHaveReactProps ( { id : 'overridden' } ) ;
83
80
} ) ;
84
81
85
82
it ( 'uses an override if specified and the override does not interupt the count' , ( ) => {
86
83
const HasOverride = ( { idOverride} : { idOverride ?: string } ) => (
87
- < React . Fragment > { useUniqueId ( '' , idOverride ) } </ React . Fragment >
84
+ < div id = { useUniqueId ( '' , idOverride ) } / >
88
85
) ;
89
86
90
87
const harness = mountWithApp (
@@ -95,16 +92,14 @@ describe('useUniqueId', () => {
95
92
</ TestHarness > ,
96
93
) ;
97
94
98
- expect ( harness . findAll ( HasOverride ) [ 0 ] . html ( ) ) . toStrictEqual ( 'polaris-1' ) ;
99
- expect ( harness . findAll ( HasOverride ) [ 1 ] . html ( ) ) . toStrictEqual ( 'overridden' ) ;
100
- expect ( harness . findAll ( HasOverride ) [ 2 ] . html ( ) ) . toStrictEqual ( 'polaris-2' ) ;
95
+ expect ( harness . findAll ( 'div' ) [ 0 ] ) . toHaveReactProps ( { id : 'Polaris1' } ) ;
96
+ expect ( harness . findAll ( 'div' ) [ 1 ] ) . toHaveReactProps ( { id : 'overridden' } ) ;
97
+ expect ( harness . findAll ( 'div' ) [ 2 ] ) . toHaveReactProps ( { id : 'Polaris2' } ) ;
101
98
} ) ;
102
99
103
100
it ( 'keeps the same ID across multiple rerenders' , ( ) => {
104
101
const HasProp = ( { info} : { info : string } ) => (
105
- < React . Fragment >
106
- { info } :: { useUniqueId ( ) }
107
- </ React . Fragment >
102
+ < div id = { useUniqueId ( ) } title = { info } />
108
103
) ;
109
104
110
105
const ReRenderingTestHarness = ( ) => {
@@ -124,19 +119,23 @@ describe('useUniqueId', () => {
124
119
125
120
const harness = mountWithApp ( < ReRenderingTestHarness /> ) ;
126
121
127
- expect ( harness . find ( HasProp ) ! . html ( ) ) . toStrictEqual ( 'count1 :: polaris-1' ) ;
122
+ expect ( harness . findAll ( 'div' ) [ 0 ] ) . toHaveReactProps ( {
123
+ title : 'count1' ,
124
+ id : 'Polaris1' ,
125
+ } ) ;
128
126
129
127
harness . find ( 'button' ) ! . trigger ( 'onClick' ) ;
130
128
131
- expect ( harness . find ( HasProp ) ! . html ( ) ) . toStrictEqual ( 'count2 :: polaris-1' ) ;
129
+ expect ( harness . findAll ( 'div' ) [ 0 ] ) . toHaveReactProps ( {
130
+ title : 'count2' ,
131
+ id : 'Polaris1' ,
132
+ } ) ;
132
133
} ) ;
133
134
134
135
it ( 'updates the ID if the overridden ID changes' , ( ) => {
135
136
type HasPropProps = { info : string ; idOverride ?: string } ;
136
137
const HasProp = ( { info, idOverride} : HasPropProps ) => (
137
- < React . Fragment >
138
- { info } :: { useUniqueId ( '' , idOverride ) }
139
- </ React . Fragment >
138
+ < div id = { useUniqueId ( '' , idOverride ) } title = { info } />
140
139
) ;
141
140
142
141
const ReRenderingTestHarness = ( ) => {
@@ -147,7 +146,7 @@ describe('useUniqueId', () => {
147
146
) ;
148
147
149
148
// eslint-disable-next-line shopify/jest/no-if
150
- const override = count % 2 === 0 ? `override- ${ count } ` : undefined ;
149
+ const override = count % 2 === 0 ? `Override ${ count } ` : undefined ;
151
150
152
151
return (
153
152
< React . Fragment >
@@ -160,23 +159,38 @@ describe('useUniqueId', () => {
160
159
const harness = mountWithApp ( < ReRenderingTestHarness /> ) ;
161
160
162
161
// Initially we use an incremental id
163
- expect ( harness . find ( HasProp ) ! . html ( ) ) . toStrictEqual ( 'count1 :: polaris-1' ) ;
162
+ expect ( harness . find ( 'div' ) ) . toHaveReactProps ( {
163
+ title : 'count1' ,
164
+ id : 'Polaris1' ,
165
+ } ) ;
164
166
165
167
// But then we set an override id, so it should use that
166
168
harness . find ( 'button' ) ! . trigger ( 'onClick' ) ;
167
- expect ( harness . find ( HasProp ) ! . html ( ) ) . toStrictEqual ( 'count2 :: override-2' ) ;
169
+ expect ( harness . find ( 'div' ) ) . toHaveReactProps ( {
170
+ title : 'count2' ,
171
+ id : 'Override2' ,
172
+ } ) ;
168
173
169
174
// Then on the next render we don't set an override id, so we should go back
170
175
// to using the incremental id
171
176
harness . find ( 'button' ) ! . trigger ( 'onClick' ) ;
172
- expect ( harness . find ( HasProp ) ! . html ( ) ) . toStrictEqual ( 'count3 :: polaris-1' ) ;
177
+ expect ( harness . find ( 'div' ) ) . toHaveReactProps ( {
178
+ title : 'count3' ,
179
+ id : 'Polaris1' ,
180
+ } ) ;
173
181
174
182
// Back to setting an override id
175
183
harness . find ( 'button' ) ! . trigger ( 'onClick' ) ;
176
- expect ( harness . find ( HasProp ) ! . html ( ) ) . toStrictEqual ( 'count4 :: override-4' ) ;
184
+ expect ( harness . find ( 'div' ) ) . toHaveReactProps ( {
185
+ title : 'count4' ,
186
+ id : 'Override4' ,
187
+ } ) ;
177
188
178
189
// Back to not setting an override, so back to using the incremental id
179
190
harness . find ( 'button' ) ! . trigger ( 'onClick' ) ;
180
- expect ( harness . find ( HasProp ) ! . html ( ) ) . toStrictEqual ( 'count5 :: polaris-1' ) ;
191
+ expect ( harness . find ( 'div' ) ) . toHaveReactProps ( {
192
+ title : 'count5' ,
193
+ id : 'Polaris1' ,
194
+ } ) ;
181
195
} ) ;
182
196
} ) ;
0 commit comments