diff --git a/assets/js/modules/analytics-4/datastore/audiences.js b/assets/js/modules/analytics-4/datastore/audiences.js index c43a639078d..a910222a329 100644 --- a/assets/js/modules/analytics-4/datastore/audiences.js +++ b/assets/js/modules/analytics-4/datastore/audiences.js @@ -657,6 +657,16 @@ const baseResolvers = { }; const baseSelectors = { + /** + * Checks if the audience group setup is in progress. + * + * @since n.e.x.t + * + * @param {Object} state Data store's state. + * @return {boolean} True if the audience group setup is in progress, otherwise false. + */ + isSettingUpAudiences: ( state ) => state.isSettingUpAudiences, + /** * Checks if the given audience is a default audience. * @@ -709,16 +719,6 @@ const baseSelectors = { } ), - /** - * Checks if the audience group setup is in progress. - * - * @since n.e.x.t - * - * @param {Object} state Data store's state. - * @return {boolean} True if the audience group setup is in progress, otherwise false. - */ - isSettingUpAudiences: ( state ) => state.isSettingUpAudiences, - /** * Checks if the given audience is a user-defined audience. * diff --git a/assets/js/modules/analytics-4/datastore/audiences.test.js b/assets/js/modules/analytics-4/datastore/audiences.test.js index 31fee7ee908..331426bbcd1 100644 --- a/assets/js/modules/analytics-4/datastore/audiences.test.js +++ b/assets/js/modules/analytics-4/datastore/audiences.test.js @@ -612,6 +612,60 @@ describe( 'modules/analytics-4 audiences', () => { } ); } ); + it( 'sets `isSettingUpAudiences` to true while the action is in progress', async () => { + fetchMock.postOnce( syncAvailableAudiencesEndpoint, { + body: availableAudiencesFixture, + status: 200, + } ); + + fetchMock.postOnce( audienceSettingsEndpoint, { + body: { + configuredAudiences: [], + isAudienceSegmentationWidgetHidden, + }, + status: 200, + } ); + + const options = registry + .select( MODULES_ANALYTICS_4 ) + .getAudiencesUserCountReportOptions( + [ availableUserAudienceFixture ], + { startDate, endDate: referenceDate } + ); + + registry + .dispatch( MODULES_ANALYTICS_4 ) + .receiveGetReport( {}, { options } ); + + registry + .dispatch( MODULES_ANALYTICS_4 ) + .finishResolution( 'getReport', [ options ] ); + + expect( + registry + .select( MODULES_ANALYTICS_4 ) + .isSettingUpAudiences() + ).toBe( false ); + + const promise = registry + .dispatch( MODULES_ANALYTICS_4 ) + .enableAudienceGroup(); + + expect( + registry + .select( MODULES_ANALYTICS_4 ) + .isSettingUpAudiences() + ).toBe( true ); + + await promise; + + expect( + registry + .select( MODULES_ANALYTICS_4 ) + .isSettingUpAudiences() + ).toBe( false ); + } ); + it( 'syncs `availableAudiences`', async () => { fetchMock.postOnce( syncAvailableAudiencesEndpoint, { body: availableAudiencesFixture, @@ -1575,6 +1629,60 @@ describe( 'modules/analytics-4 audiences', () => { } ); } ); + it( 'sets `isSettingUpAudiences` to true while the action is in progress', async () => { + fetchMock.postOnce( syncAvailableAudiencesEndpoint, { + body: availableAudiencesFixture, + status: 200, + } ); + + fetchMock.postOnce( audienceSettingsEndpoint, { + body: { + configuredAudiences: [], + isAudienceSegmentationWidgetHidden, + }, + status: 200, + } ); + + const options = registry + .select( MODULES_ANALYTICS_4 ) + .getAudiencesUserCountReportOptions( + [ availableUserAudienceFixture ], + { startDate, endDate: referenceDate } + ); + + registry + .dispatch( MODULES_ANALYTICS_4 ) + .receiveGetReport( {}, { options } ); + + registry + .dispatch( MODULES_ANALYTICS_4 ) + .finishResolution( 'getReport', [ options ] ); + + expect( + registry + .select( MODULES_ANALYTICS_4 ) + .isSettingUpAudiences() + ).toBe( false ); + + const promise = registry + .dispatch( MODULES_ANALYTICS_4 ) + .enableSecondaryUserAudienceGroup(); + + expect( + registry + .select( MODULES_ANALYTICS_4 ) + .isSettingUpAudiences() + ).toBe( true ); + + await promise; + + expect( + registry + .select( MODULES_ANALYTICS_4 ) + .isSettingUpAudiences() + ).toBe( false ); + } ); + it( 'does not sync `availableAudiences` for unauthenticated user', async () => { provideUserAuthentication( registry, { authenticated: false,