-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathSetupForm.test.js
More file actions
571 lines (508 loc) · 15.3 KB
/
SetupForm.test.js
File metadata and controls
571 lines (508 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/**
* Site Kit by Google, Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Internal dependencies
*/
import { act, fireEvent, render } from '../../../../../../tests/js/test-utils';
import {
createTestRegistry,
muteFetch,
provideModules,
provideSiteInfo,
provideUserAuthentication,
waitForDefaultTimeouts,
} from '../../../../../../tests/js/utils';
import { CORE_FORMS } from '@/js/googlesitekit/datastore/forms/constants';
import { MODULES_TAGMANAGER } from '@/js/modules/tagmanager/datastore/constants';
import {
EDIT_SCOPE,
ENHANCED_MEASUREMENT_ENABLED,
ENHANCED_MEASUREMENT_FORM,
FORM_SETUP,
MODULES_ANALYTICS_4,
} from '@/js/modules/analytics-4/datastore/constants';
import { MODULE_SLUG_ANALYTICS_4 } from '@/js/modules/analytics-4/constants';
import * as fixtures from '@/js/modules/analytics-4/datastore/__fixtures__';
import SetupForm from './SetupForm';
import { CORE_SITE } from '@/js/googlesitekit/datastore/site/constants';
const {
accountSummaries,
defaultEnhancedMeasurementSettings,
webDataStreamsBatch,
webDataStreams,
} = fixtures;
const accounts = accountSummaries.accountSummaries;
const properties = accounts[ 1 ].propertySummaries;
const accountID = accounts[ 1 ]._id;
const propertyID = properties[ 0 ]._id;
const webDataStreamID = webDataStreamsBatch[ propertyID ][ 0 ]._id;
const REGEX_REST_GA4_SETTINGS = new RegExp( '/analytics-4/data/settings' );
const REGEX_REST_DISMISS_ITEM = new RegExp(
'^/google-site-kit/v1/core/user/data/dismiss-item'
);
const REGEX_REST_GA4_CREATE_PROPERTY = new RegExp(
'/analytics-4/data/create-property'
);
const REGEX_REST_GA4_CREATE_WEBDATASTREAM = new RegExp(
'/analytics-4/data/create-webdatastream'
);
const REGEX_REST_GA4_ACCOUNT_SUMMARIES = new RegExp(
'/analytics-4/data/account-summaries'
);
const REGEX_REST_CONVERSION_TRACKING_SETTINGS = new RegExp(
'^/google-site-kit/v1/core/site/data/conversion-tracking'
);
describe( 'SetupForm', () => {
let registry;
beforeEach( () => {
registry = createTestRegistry();
provideSiteInfo( registry );
provideUserAuthentication( registry );
provideModules( registry, [
{ slug: MODULE_SLUG_ANALYTICS_4, active: true },
] );
registry.dispatch( MODULES_ANALYTICS_4 ).receiveGetExistingTag( null );
registry.dispatch( CORE_SITE ).receiveGetConversionTrackingSettings( {
enabled: false,
} );
muteFetch( REGEX_REST_CONVERSION_TRACKING_SETTINGS );
} );
it( 'renders the form correctly', async () => {
registry.dispatch( CORE_SITE ).receiveGetConversionTrackingSettings( {
enabled: true, // Hide notice for this case.
} );
registry.dispatch( MODULES_ANALYTICS_4 ).setSettings( {} );
registry.dispatch( MODULES_TAGMANAGER ).setSettings( {} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetAccountSummaries( accountSummaries );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetProperty( properties[ 0 ], {
propertyID,
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetWebDataStreamsBatch( webDataStreamsBatch, {
propertyIDs: [ propertyID ],
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetEnhancedMeasurementSettings(
{
...defaultEnhancedMeasurementSettings,
streamEnabled: false,
},
{
propertyID,
webDataStreamID,
}
);
await registry
.dispatch( MODULES_ANALYTICS_4 )
.selectAccount( accountID );
const { container, getByText, waitForRegistry } = render(
<SetupForm />,
{
registry,
}
);
await waitForRegistry();
expect( container ).toMatchSnapshot();
expect( getByText( 'Account' ) ).toBeInTheDocument();
expect( getByText( 'Property' ) ).toBeInTheDocument();
expect( getByText( 'Web data stream' ) ).toBeInTheDocument();
} );
it( 'renders the form correctly with setupFlowRefresh enabled', async () => {
registry.dispatch( CORE_SITE ).receiveGetConversionTrackingSettings( {
enabled: true, // Hide notice for this case.
} );
registry.dispatch( CORE_SITE ).setConversionTrackingEnabled( false );
registry.dispatch( MODULES_ANALYTICS_4 ).setSettings( {} );
registry.dispatch( MODULES_TAGMANAGER ).setSettings( {} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetAccountSummaries( accountSummaries );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetProperty( properties[ 0 ], {
propertyID,
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetWebDataStreamsBatch( webDataStreamsBatch, {
propertyIDs: [ propertyID ],
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetEnhancedMeasurementSettings(
{
...defaultEnhancedMeasurementSettings,
streamEnabled: false,
},
{
propertyID,
webDataStreamID,
}
);
await registry
.dispatch( MODULES_ANALYTICS_4 )
.selectAccount( accountID );
const { container, queryByText, getByRole, waitForRegistry } = render(
<SetupForm />,
{
registry,
features: [ 'setupFlowRefresh' ],
}
);
await waitForRegistry();
expect( container ).toMatchSnapshot();
expect(
container.querySelector( 'a[href*=plugin-conversion-tracking]' )
).toBeInTheDocument();
expect(
getByRole( 'button', { name: /Set up/i } )
).toBeInTheDocument();
expect( queryByText( 'Complete setup' ) ).not.toBeInTheDocument();
} );
it( 'submits the form upon pressing the CTA', async () => {
registry.dispatch( MODULES_TAGMANAGER ).setSettings( {} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetAccountSummaries( accountSummaries );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetProperty( properties[ 0 ], {
propertyID,
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetWebDataStreamsBatch( webDataStreamsBatch, {
propertyIDs: [ propertyID ],
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetEnhancedMeasurementSettings(
defaultEnhancedMeasurementSettings,
{
propertyID,
webDataStreamID,
}
);
await registry
.dispatch( MODULES_ANALYTICS_4 )
.selectAccount( accountID );
const finishSetup = jest.fn();
const { getByRole, waitForRegistry } = render(
<SetupForm finishSetup={ finishSetup } />,
{
registry,
}
);
await waitForRegistry();
act( () => {
// It doesn't seem possible to show the dropdown menu within the test by clicking on the dropdown in the usual way.
// Therefore, we simulate the click on the dropdown menu item directly, despite it being hidden.
fireEvent.click(
getByRole( 'menuitem', {
name: /Example Com/i,
hidden: true,
} )
);
} );
muteFetch( REGEX_REST_GA4_SETTINGS );
fetchMock.post( REGEX_REST_DISMISS_ITEM, {
status: 200,
body: JSON.stringify( [ 'enhanced-measurement-notification' ] ),
} );
act( () => {
fireEvent.click(
getByRole( 'button', { name: /Complete setup/i } )
);
} );
await waitForRegistry();
// An additional wait is required in order for all resolvers to finish.
await act( waitForDefaultTimeouts );
expect( fetchMock ).toHaveFetchedTimes( 1, REGEX_REST_GA4_SETTINGS );
expect( finishSetup ).toHaveBeenCalledTimes( 1 );
} );
it( 'auto-submits the form', async () => {
registry.dispatch( MODULES_ANALYTICS_4 ).setSettings( {} );
registry.dispatch( MODULES_TAGMANAGER ).setSettings( {} );
registry.dispatch( MODULES_ANALYTICS_4 ).receiveGetExistingTag( null );
registry.dispatch( MODULES_ANALYTICS_4 ).receiveGetAccountSummaries( {
accountSummaries: accountSummaries.accountSummaries.map(
( account ) => ( {
...account,
propertySummaries: [],
} )
),
nextPageToken: null,
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetProperty( properties[ 0 ], {
propertyID,
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetWebDataStreamsBatch( webDataStreamsBatch, {
propertyIDs: [ propertyID ],
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetEnhancedMeasurementSettings(
fixtures.defaultEnhancedMeasurementSettings,
{
propertyID,
webDataStreamID,
}
);
registry.dispatch( CORE_FORMS ).setValues( ENHANCED_MEASUREMENT_FORM, {
[ ENHANCED_MEASUREMENT_ENABLED ]: false,
} );
fetchMock.post(
new RegExp(
'^/google-site-kit/v1/modules/analytics-4/data/enhanced-measurement-settings'
),
{
status: 200,
body: fixtures.defaultEnhancedMeasurementSettings,
}
);
await registry
.dispatch( MODULES_ANALYTICS_4 )
.selectAccount( accountID );
// Simulate an auto-submit case where the user is returning to the page
// after granting extra scopes necessary to submit.
// In this situation, the autoSubmit is set before the user goes to oAuth,
// store state is snapshotted, and then restored upon returning.
registry.dispatch( CORE_FORMS ).setValues( FORM_SETUP, {
autoSubmit: true,
webDataStreamName: fixtures.createWebDataStream.displayName,
} );
provideUserAuthentication( registry, {
grantedScopes: [ EDIT_SCOPE ],
} );
fetchMock.post( REGEX_REST_GA4_CREATE_PROPERTY, {
status: 200,
body: properties[ 0 ],
} );
fetchMock.post( REGEX_REST_GA4_CREATE_WEBDATASTREAM, {
status: 200,
body: webDataStreams[ 2 ],
} );
fetchMock.get( REGEX_REST_GA4_ACCOUNT_SUMMARIES, {
status: 200,
body: accountSummaries,
} );
muteFetch( REGEX_REST_GA4_SETTINGS );
const finishSetup = jest.fn();
const { getByRole, waitForRegistry } = render(
<SetupForm finishSetup={ finishSetup } />,
{
registry,
}
);
await waitForRegistry();
// An additional wait is required in order for all resolvers to finish.
await act( async () => {
await waitForDefaultTimeouts();
} );
// Ensure the form rendered successfully.
expect(
getByRole( 'button', { name: /Complete setup/i } )
).toBeInTheDocument();
await waitForRegistry();
// An additional wait is required in order for all resolvers to finish.
await act( async () => {
await waitForDefaultTimeouts();
} );
expect( fetchMock ).toHaveFetchedTimes(
1,
REGEX_REST_GA4_CREATE_PROPERTY
);
expect( fetchMock ).toHaveFetchedTimes(
1,
REGEX_REST_GA4_CREATE_WEBDATASTREAM
);
expect( fetchMock ).toHaveFetchedTimes( 1, REGEX_REST_GA4_SETTINGS );
expect( finishSetup ).toHaveBeenCalledTimes( 1 );
} );
it( 'auto-submits the form only once in the case of an error', async () => {
registry.dispatch( MODULES_TAGMANAGER ).setSettings( {} );
registry.dispatch( MODULES_ANALYTICS_4 ).receiveGetExistingTag( null );
registry.dispatch( MODULES_ANALYTICS_4 ).receiveGetAccountSummaries( {
accountSummaries: accountSummaries.accountSummaries.map(
( account ) => ( {
...account,
propertySummaries: [],
} )
),
nextPageToken: null,
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetWebDataStreamsBatch( webDataStreamsBatch, {
propertyIDs: [ propertyID ],
} );
await registry
.dispatch( MODULES_ANALYTICS_4 )
.selectAccount( accountID );
// Simulate an auto-submit case where the user is returning to the page
// after granting extra scopes necessary to submit.
// In this situation, the autoSubmit is set before the user goes to oAuth,
// store state is snapshotted, and then restored upon returning.
registry.dispatch( CORE_FORMS ).setValues( FORM_SETUP, {
autoSubmit: true,
webDataStreamName: fixtures.createWebDataStream.displayName,
} );
provideUserAuthentication( registry, {
grantedScopes: [ EDIT_SCOPE ],
} );
fetchMock.post( REGEX_REST_GA4_CREATE_PROPERTY, {
status: 403,
body: {
code: 403,
error: 'Insufficient permissions',
},
} );
const finishSetup = jest.fn();
const { getByRole, waitForRegistry } = render(
<SetupForm finishSetup={ finishSetup } />,
{
registry,
}
);
await waitForRegistry();
// Ensure the form rendered successfully.
expect(
getByRole( 'button', { name: /Complete setup/i } )
).toBeInTheDocument();
// While not strictly needed, add waits to match the successful auto-submit test case to help avoid a false positive result.
await waitForRegistry();
await act( async () => {
await waitForDefaultTimeouts();
} );
// Create property should have only been called once.
expect( fetchMock ).toHaveFetchedTimes(
1,
REGEX_REST_GA4_CREATE_PROPERTY
);
// Setup was not successful, so the finish function should not be called.
expect( finishSetup ).not.toHaveBeenCalled();
// Expect a console error due to the API error (otherwise this test will fail).
expect( console ).toHaveErrored();
} );
describe.each( [
[
'account is changed',
( getByRole ) =>
fireEvent.click(
getByRole( 'menuitem', {
name: /Example Org/i,
hidden: true,
} )
),
],
[
'property is changed',
( getByRole ) =>
fireEvent.click(
getByRole( 'menuitem', {
name: /set up a new property/i,
hidden: true,
} )
),
],
[
'web data stream is changed',
( getByRole ) =>
fireEvent.click(
getByRole( 'menuitem', {
name: /set up a new web data stream/i,
hidden: true,
} )
),
],
] )( 'when the %s', ( _, triggerChange ) => {
beforeEach( async () => {
muteFetch( { query: { tagverify: '1' } } );
registry.dispatch( MODULES_ANALYTICS_4 ).setSettings( {} );
registry.dispatch( MODULES_TAGMANAGER ).setSettings( {} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetAccountSummaries( accountSummaries );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetProperty( properties[ 0 ], {
propertyID,
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetWebDataStreamsBatch( webDataStreamsBatch, {
propertyIDs: [ propertyID ],
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetEnhancedMeasurementSettings(
{
...defaultEnhancedMeasurementSettings,
streamEnabled: false,
},
{
propertyID,
webDataStreamID,
}
);
await registry
.dispatch( MODULES_ANALYTICS_4 )
.selectAccount( accountID );
const { getByRole, getByLabelText, waitForRegistry } = render(
<SetupForm />,
{
registry,
}
);
await waitForRegistry();
const switchControl = getByLabelText(
'Enable enhanced measurement'
);
switchControl.click();
const isEnhancedMeasurementEnabled = registry
.select( CORE_FORMS )
.getValue(
ENHANCED_MEASUREMENT_FORM,
ENHANCED_MEASUREMENT_ENABLED
);
expect( isEnhancedMeasurementEnabled ).toBe( false );
act( () => {
triggerChange( getByRole );
} );
// An additional wait is required in order for all resolvers to finish.
await act( waitForDefaultTimeouts );
} );
it( 'should revert the enhanced measurement from off to on', () => {
const updatedIsEnhancedMeasurementEnabled = registry
.select( CORE_FORMS )
.getValue(
ENHANCED_MEASUREMENT_FORM,
ENHANCED_MEASUREMENT_ENABLED
);
expect( updatedIsEnhancedMeasurementEnabled ).toBe( true );
} );
} );
} );