Skip to content

Commit 5087bf3

Browse files
committed
Update snapshot.
2 parents 8d9b9ee + 18e8ff5 commit 5087bf3

File tree

50 files changed

+3780
-347
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3780
-347
lines changed

assets/js/components/OptIn.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export default function OptIn( {
4949
name = 'optIn',
5050
className,
5151
trackEventCategory,
52+
trackEventAction = 'tracking_optin',
5253
alignLeftCheckbox = false,
5354
} ) {
5455
const [ checked, setChecked ] = useState();
@@ -76,14 +77,20 @@ export default function OptIn( {
7677
if ( response.enabled ) {
7778
trackEvent(
7879
trackEventCategory || viewContext,
79-
'tracking_optin'
80+
trackEventAction
8081
);
8182
}
8283
} else {
8384
setChecked( enabled );
8485
}
8586
},
86-
[ enabled, setTrackingEnabled, trackEventCategory, viewContext ]
87+
[
88+
enabled,
89+
setTrackingEnabled,
90+
trackEventCategory,
91+
trackEventAction,
92+
viewContext,
93+
]
8794
);
8895

8996
useEffect( () => {

assets/js/components/OptIn.test.js

Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
/**
2+
* OptIn component tests.
3+
*
4+
* Site Kit by Google, Copyright 2025 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* Internal dependencies
21+
*/
22+
import {
23+
render,
24+
createTestRegistry,
25+
provideTracking,
26+
fireEvent,
27+
muteFetch,
28+
act,
29+
} from '../../../tests/js/test-utils';
30+
import * as tracking from '@/js/util/tracking';
31+
import OptIn from './OptIn';
32+
33+
const mockTrackEvent = jest.spyOn( tracking, 'trackEvent' );
34+
mockTrackEvent.mockImplementation( () => Promise.resolve() );
35+
36+
describe( 'OptIn', () => {
37+
let registry;
38+
39+
const coreUserTrackingSettingsEndpointRegExp = new RegExp(
40+
'^/google-site-kit/v1/core/user/data/tracking'
41+
);
42+
43+
const optInProps = {
44+
id: 'googlesitekit-opt-in-test',
45+
name: 'optInTest',
46+
className: 'googlesitekit-opt-in-test',
47+
alignLeftCheckbox: true,
48+
};
49+
50+
beforeEach( () => {
51+
registry = createTestRegistry();
52+
jest.useFakeTimers();
53+
} );
54+
55+
afterEach( () => {
56+
jest.resetAllMocks();
57+
} );
58+
59+
it( 'should render correctly', () => {
60+
provideTracking( registry, false );
61+
62+
const { container, getByRole, getByText } = render(
63+
<OptIn { ...optInProps } />,
64+
{
65+
viewContext: 'test-view-context',
66+
registry,
67+
}
68+
);
69+
70+
expect( container ).toMatchSnapshot();
71+
72+
expect( getByRole( 'checkbox' ) ).toBeInTheDocument();
73+
74+
expect(
75+
getByText(
76+
'Help us improve Site Kit by sharing anonymous usage data.'
77+
)
78+
).toBeInTheDocument();
79+
} );
80+
81+
it( 'should show a spinner when tracking is loading', () => {
82+
muteFetch( coreUserTrackingSettingsEndpointRegExp );
83+
84+
const { container, queryByRole } = render( <OptIn />, {
85+
registry,
86+
} );
87+
88+
expect( container ).toMatchSnapshot();
89+
90+
expect( queryByRole( 'checkbox' ) ).not.toBeInTheDocument();
91+
92+
expect( container.querySelector( '.spinner' ) ).toBeInTheDocument();
93+
} );
94+
95+
it( 'should enable tracking when the user opts in', async () => {
96+
provideTracking( registry, false );
97+
98+
fetchMock.post( coreUserTrackingSettingsEndpointRegExp, {
99+
status: 200,
100+
body: { enabled: true },
101+
} );
102+
103+
const { getByRole } = render( <OptIn { ...optInProps } />, {
104+
registry,
105+
} );
106+
107+
expect( getByRole( 'checkbox' ) ).not.toBeChecked();
108+
expect( tracking.isTrackingEnabled() ).toBe( false );
109+
110+
fireEvent.click( getByRole( 'checkbox' ) );
111+
112+
expect( getByRole( 'checkbox' ) ).toBeChecked();
113+
114+
// Wait for the debounced handler to complete, and advance to the next tick.
115+
await act( () => {
116+
jest.advanceTimersByTime( 300 );
117+
return Promise.resolve();
118+
} );
119+
120+
expect( tracking.isTrackingEnabled() ).toBe( true );
121+
} );
122+
123+
it( 'should disable tracking when the user opts out', async () => {
124+
provideTracking( registry, true );
125+
126+
fetchMock.post( coreUserTrackingSettingsEndpointRegExp, {
127+
status: 200,
128+
body: { enabled: false },
129+
} );
130+
131+
const { getByRole } = render( <OptIn { ...optInProps } />, {
132+
registry,
133+
} );
134+
135+
expect( getByRole( 'checkbox' ) ).toBeChecked();
136+
expect( tracking.isTrackingEnabled() ).toBe( true );
137+
138+
fireEvent.click( getByRole( 'checkbox' ) );
139+
140+
expect( getByRole( 'checkbox' ) ).not.toBeChecked();
141+
142+
// Wait for the debounced handler to complete, and advance to the next tick.
143+
await act( () => {
144+
jest.advanceTimersByTime( 300 );
145+
return Promise.resolve();
146+
} );
147+
148+
expect( tracking.isTrackingEnabled() ).toBe( false );
149+
} );
150+
151+
it( 'should show an error message when the tracking setting update fails', async () => {
152+
provideTracking( registry, false );
153+
154+
fetchMock.post( coreUserTrackingSettingsEndpointRegExp, {
155+
status: 500,
156+
body: {
157+
message: 'Internal server error',
158+
},
159+
} );
160+
161+
const { getByRole, getByText } = render( <OptIn { ...optInProps } />, {
162+
registry,
163+
} );
164+
165+
expect( getByRole( 'checkbox' ) ).not.toBeChecked();
166+
expect( tracking.isTrackingEnabled() ).toBe( false );
167+
168+
fireEvent.click( getByRole( 'checkbox' ) );
169+
170+
// Wait for the debounced handler to complete, and advance to the next tick.
171+
await act( () => {
172+
jest.advanceTimersByTime( 300 );
173+
return Promise.resolve();
174+
} );
175+
176+
expect( getByRole( 'checkbox' ) ).not.toBeChecked();
177+
expect( tracking.isTrackingEnabled() ).toBe( false );
178+
179+
expect( getByText( 'Internal server error' ) ).toBeInTheDocument();
180+
expect( console ).toHaveErrored();
181+
} );
182+
183+
it( 'should track an event when the user opts in', async () => {
184+
provideTracking( registry, false );
185+
186+
fetchMock.post( coreUserTrackingSettingsEndpointRegExp, {
187+
status: 200,
188+
body: { enabled: true },
189+
} );
190+
191+
const { getByRole } = render( <OptIn { ...optInProps } />, {
192+
registry,
193+
viewContext: 'test-view-context',
194+
} );
195+
196+
expect( tracking.trackEvent ).not.toHaveBeenCalled();
197+
198+
fireEvent.click( getByRole( 'checkbox' ) );
199+
200+
// Wait for the debounced handler to complete, and advance to the next tick.
201+
await act( () => {
202+
jest.advanceTimersByTime( 300 );
203+
return Promise.resolve();
204+
} );
205+
206+
expect( tracking.trackEvent ).toHaveBeenCalledWith(
207+
'test-view-context',
208+
'tracking_optin'
209+
);
210+
expect( tracking.trackEvent ).toHaveBeenCalledTimes( 1 );
211+
} );
212+
213+
it( 'should track an event with a custom category', async () => {
214+
provideTracking( registry, false );
215+
216+
fetchMock.post( coreUserTrackingSettingsEndpointRegExp, {
217+
status: 200,
218+
body: { enabled: true },
219+
} );
220+
221+
const { getByRole } = render(
222+
<OptIn
223+
{ ...optInProps }
224+
trackEventCategory="test-event-category"
225+
/>,
226+
{
227+
registry,
228+
viewContext: 'test-view-context',
229+
}
230+
);
231+
232+
fireEvent.click( getByRole( 'checkbox' ) );
233+
234+
// Wait for the debounced handler to complete, and advance to the next tick.
235+
await act( () => {
236+
jest.advanceTimersByTime( 300 );
237+
return Promise.resolve();
238+
} );
239+
240+
expect( tracking.trackEvent ).toHaveBeenCalledWith(
241+
'test-event-category',
242+
'tracking_optin'
243+
);
244+
} );
245+
246+
it( 'should track an event with a custom action', async () => {
247+
provideTracking( registry, false );
248+
fetchMock.post( coreUserTrackingSettingsEndpointRegExp, {
249+
status: 200,
250+
body: { enabled: true },
251+
} );
252+
253+
const { getByRole } = render(
254+
<OptIn { ...optInProps } trackEventAction="test-event-action" />,
255+
{
256+
registry,
257+
viewContext: 'test-view-context',
258+
}
259+
);
260+
261+
fireEvent.click( getByRole( 'checkbox' ) );
262+
263+
// Wait for the debounced handler to complete, and advance to the next tick.
264+
await act( () => {
265+
jest.advanceTimersByTime( 300 );
266+
return Promise.resolve();
267+
} );
268+
269+
expect( tracking.trackEvent ).toHaveBeenCalledWith(
270+
'test-view-context',
271+
'test-event-action'
272+
);
273+
expect( tracking.trackEvent ).toHaveBeenCalledTimes( 1 );
274+
} );
275+
276+
it( 'should not track an event when the user opts out', async () => {
277+
provideTracking( registry, true );
278+
279+
fetchMock.post( coreUserTrackingSettingsEndpointRegExp, {
280+
status: 200,
281+
body: { enabled: false },
282+
} );
283+
284+
const { getByRole } = render( <OptIn { ...optInProps } />, {
285+
registry,
286+
} );
287+
288+
fireEvent.click( getByRole( 'checkbox' ) );
289+
290+
// Wait for the debounced handler to complete, and advance to the next tick.
291+
await act( () => {
292+
jest.advanceTimersByTime( 300 );
293+
return Promise.resolve();
294+
} );
295+
296+
expect( tracking.trackEvent ).not.toHaveBeenCalled();
297+
} );
298+
} );

assets/js/components/OverlayNotification/AnalyticsAndAdSenseAccountsDetectedAsLinkedOverlayNotification.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import AnalyticsAndAdSenseAccountsDetectedAsLinkedOverlayNotification, {
5050
ANALYTICS_ADSENSE_LINKED_OVERLAY_NOTIFICATION,
5151
} from './AnalyticsAndAdSenseAccountsDetectedAsLinkedOverlayNotification';
5252
import { withNotificationComponentProps } from '@/js/googlesitekit/notifications/util/component-props';
53-
import { DEFAULT_NOTIFICATIONS } from '@/js/googlesitekit/notifications/register-defaults';
53+
import { ADSENSE_NOTIFICATIONS } from '@/js/modules/adsense';
5454
import {
5555
NOTIFICATION_AREAS,
5656
NOTIFICATION_GROUPS,
@@ -65,7 +65,7 @@ describe( 'AnalyticsAndAdSenseAccountsDetectedAsLinkedOverlayNotification', () =
6565
)( AnalyticsAndAdSenseAccountsDetectedAsLinkedOverlayNotification );
6666

6767
const notification =
68-
DEFAULT_NOTIFICATIONS[ ANALYTICS_ADSENSE_LINKED_OVERLAY_NOTIFICATION ];
68+
ADSENSE_NOTIFICATIONS[ ANALYTICS_ADSENSE_LINKED_OVERLAY_NOTIFICATION ];
6969

7070
let registry;
7171

assets/js/components/OverlayNotification/LinkAnalyticsAndAdSenseAccountsOverlayNotification.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {
4141
} from '@/js/googlesitekit/constants';
4242
import { withNotificationComponentProps } from '@/js/googlesitekit/notifications/util/component-props';
4343
import { CORE_NOTIFICATIONS } from '@/js/googlesitekit/notifications/datastore/constants';
44-
import { DEFAULT_NOTIFICATIONS } from '@/js/googlesitekit/notifications/register-defaults';
44+
import { ADSENSE_NOTIFICATIONS } from '@/js/modules/adsense';
4545
import Notifications from '@/js/components/notifications/Notifications';
4646
import {
4747
NOTIFICATION_AREAS,
@@ -55,7 +55,7 @@ describe( 'LinkAnalyticsAndAdSenseAccountsOverlayNotification', () => {
5555
)( LinkAnalyticsAndAdSenseAccountsOverlayNotification );
5656

5757
const notification =
58-
DEFAULT_NOTIFICATIONS[ LINK_ANALYTICS_ADSENSE_OVERLAY_NOTIFICATION ];
58+
ADSENSE_NOTIFICATIONS[ LINK_ANALYTICS_ADSENSE_OVERLAY_NOTIFICATION ];
5959

6060
let registry;
6161

0 commit comments

Comments
 (0)