Skip to content

Commit

Permalink
Add test coverage for isSettingUpAudiences().
Browse files Browse the repository at this point in the history
  • Loading branch information
techanvil committed Sep 5, 2024
1 parent 7d653ed commit 7ce10f7
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 10 deletions.
20 changes: 10 additions & 10 deletions assets/js/modules/analytics-4/datastore/audiences.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
108 changes: 108 additions & 0 deletions assets/js/modules/analytics-4/datastore/audiences.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7ce10f7

Please sign in to comment.