|
| 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 | +} ); |
0 commit comments