Skip to content

Commit

Permalink
Merge pull request #9840 from google/enhancement/#9595-groups-edit-scope
Browse files Browse the repository at this point in the history
  • Loading branch information
aaemnnosttv authored Feb 21, 2025
2 parents 644a676 + 9a03b2a commit 76e630c
Show file tree
Hide file tree
Showing 8 changed files with 516 additions and 436 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,26 @@ function AudienceSegmentationSetupCTAWidget( { id, Notification } ) {

const { dismissItem, dismissPrompt } = useDispatch( CORE_USER );

const onSuccess = useCallback( () => {
invalidateResolution( 'getQueuedNotifications', [
viewContext,
NOTIFICATION_GROUPS.DEFAULT,
] );
dismissPrompt( id, {
expiresInSeconds: 0,
} );
// Dismiss success notification in settings.
dismissItem( SETTINGS_VISITOR_GROUPS_SETUP_SUCCESS_NOTIFICATION );
}, [ dismissItem, dismissPrompt, id, invalidateResolution, viewContext ] );

const onError = useCallback( () => {
setShowErrorModal( true );
}, [ setShowErrorModal ] );

const { apiErrors, failedAudiences, isSaving, onEnableGroups } =
useEnableAudienceGroup( {
onSuccess: () => {
invalidateResolution( 'getQueuedNotifications', [
viewContext,
NOTIFICATION_GROUPS.DEFAULT,
] );
dismissPrompt( id, {
expiresInSeconds: 0,
} );
// Dismiss success notification in settings.
dismissItem(
SETTINGS_VISITOR_GROUPS_SETUP_SUCCESS_NOTIFICATION
);
},
onError: () => {
setShowErrorModal( true );
},
onSuccess,
onError,
} );

const { clearPermissionScopeError } = useDispatch( CORE_USER );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '../../../../../../../tests/js/test-utils';
import {
createTestRegistry,
freezeFetch,
muteFetch,
provideModules,
provideSiteInfo,
Expand Down Expand Up @@ -898,6 +899,8 @@ describe( 'AudienceSegmentationSetupCTAWidget', () => {
it( 'should track an event when the Retry button is clicked', () => {
mockTrackEvent.mockClear();

freezeFetch( syncAvailableAudiencesEndpoint );

act( () => {
fireEvent.click(
getByRole( 'button', { name: /retry/i } )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { useCallback, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
Expand All @@ -46,18 +46,20 @@ export default function SetupCTA() {

const { dismissNotification } = useDispatch( CORE_NOTIFICATIONS );

const onSuccess = useCallback( () => {
// Dismiss success notification in dashboard.
dismissNotification( AUDIENCE_SEGMENTATION_SETUP_SUCCESS_NOTIFICATION );
}, [ dismissNotification ] );

const onError = useCallback( () => {
setShowErrorModal( true );
}, [ setShowErrorModal ] );

const { apiErrors, failedAudiences, isSaving, onEnableGroups } =
useEnableAudienceGroup( {
redirectURL: global.location.href,
onSuccess: () => {
// Dismiss success notification in dashboard.
dismissNotification(
AUDIENCE_SEGMENTATION_SETUP_SUCCESS_NOTIFICATION
);
},
onError: () => {
setShowErrorModal( true );
},
onSuccess,
onError,
} );

const setupErrorCode = useSelect( ( select ) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ describe( 'SettingsCardVisitorGroups SetupCTA', () => {
const syncAvailableAudiencesEndpoint = new RegExp(
'^/google-site-kit/v1/modules/analytics-4/data/sync-audiences'
);
const syncAvailableCustomDimensionsEndpoint = new RegExp(
'^/google-site-kit/v1/modules/analytics-4/data/sync-custom-dimensions'
);

beforeEach( () => {
registry = createTestRegistry();
Expand Down Expand Up @@ -157,6 +160,9 @@ describe( 'SettingsCardVisitorGroups SetupCTA', () => {
);

expect( fetchMock ).toHaveFetched( syncAvailableAudiencesEndpoint );
expect( fetchMock ).toHaveFetched(
syncAvailableCustomDimensionsEndpoint
);

await act( waitForDefaultTimeouts );
} );
Expand Down Expand Up @@ -185,6 +191,16 @@ describe( 'SettingsCardVisitorGroups SetupCTA', () => {

describe( 'AudienceErrorModal', () => {
it( 'should show the OAuth error modal when the required scopes are not granted', async () => {
fetchMock.postOnce( syncAvailableAudiencesEndpoint, {
body: [],
status: 200,
} );

fetchMock.postOnce( syncAvailableCustomDimensionsEndpoint, {
body: [],
status: 200,
} );

provideSiteInfo( registry, {
setupErrorCode: 'access_denied',
} );
Expand Down Expand Up @@ -221,13 +237,13 @@ describe( 'SettingsCardVisitorGroups SetupCTA', () => {
);
} );

// Allow the `trackEvent()` promise to resolve.
await waitForDefaultTimeouts();

// Verify the error is an OAuth error variant.
expect(
getByText( /Analytics update failed/i )
).toBeInTheDocument();
// Wait for the error modal to be displayed.
await waitFor( () => {
// Verify the error is an OAuth error variant.
expect(
getByText( /Analytics update failed/i )
).toBeInTheDocument();
} );

// Verify the "Get help" link is displayed.
expect( getByText( /get help/i ) ).toBeInTheDocument();
Expand All @@ -252,7 +268,7 @@ describe( 'SettingsCardVisitorGroups SetupCTA', () => {
data: { reason: ERROR_REASON_INSUFFICIENT_PERMISSIONS },
};

fetchMock.post( syncAvailableAudiencesEndpoint, {
fetchMock.postOnce( syncAvailableAudiencesEndpoint, {
body: errorResponse,
status: 500,
} );
Expand Down Expand Up @@ -292,7 +308,7 @@ describe( 'SettingsCardVisitorGroups SetupCTA', () => {
data: { status: 500 },
};

fetchMock.post( syncAvailableAudiencesEndpoint, {
fetchMock.postOnce( syncAvailableAudiencesEndpoint, {
body: errorResponse,
status: 500,
} );
Expand Down
Loading

0 comments on commit 76e630c

Please sign in to comment.