Skip to content

Commit

Permalink
Optimize tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitrox committed Jul 19, 2024
1 parent 9586f22 commit 909ea55
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ export const PUBLICATION_ONBOARDING_STATES = {
UNSPECIFIED: 'ONBOARDING_STATE_UNSPECIFIED',
};

export const UI_KEY_SHOW_RRM_PUBLICATION_APPROVED_NOTIFICATION =
'show-rrm-publication-approved-notification';
export const UI_KEY_READER_REVENUE_MANAGER_SHOW_PUBLICATION_APPROVED_NOTIFICATION =
'READER_REVENUE_MANAGER_SHOW_PUBLICATION_APPROVED_NOTIFICATION';
28 changes: 16 additions & 12 deletions assets/js/modules/reader-revenue-manager/datastore/publications.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
MODULES_READER_REVENUE_MANAGER,
MODULE_SLUG,
PUBLICATION_ONBOARDING_STATES,
UI_KEY_SHOW_RRM_PUBLICATION_APPROVED_NOTIFICATION,
UI_KEY_READER_REVENUE_MANAGER_SHOW_PUBLICATION_APPROVED_NOTIFICATION,
} from './constants';

const fetchGetPublicationsStore = createFetchStore( {
Expand Down Expand Up @@ -89,32 +89,36 @@ const baseActions = {
.select( MODULES_READER_REVENUE_MANAGER )
.getPublications();

// If there are no publications, do not attempt to sync the onboarding state.
if ( ! publications ) {
return;
}

const publication = publications.find(
// eslint-disable-next-line sitekit/acronym-case
( { publicationId } ) => publicationId === publicationID
);

// If the publications is not found, do not attempt to sync the onboarding state.
if ( ! publication ) {
return;
}

const { onboardingState } = publication;
const currentOnboardingState = registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPublicationOnboardingState();

let settings = registry
const settings = registry
.select( MODULES_READER_REVENUE_MANAGER )
.getSettings();

if ( onboardingState !== currentOnboardingState ) {
settings = {
...settings,
publicationOnboardingState: onboardingState,
};
settings.publicationOnboardingState = onboardingState;
}

settings = {
...settings,
/* eslint-disable-next-line sitekit/no-direct-date */
publicationOnboardingStateLastSyncedAtMs: Date.now(),
};
// eslint-disable-next-line sitekit/no-direct-date
settings.publicationOnboardingStateLastSyncedAtMs = Date.now();

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
Expand All @@ -131,7 +135,7 @@ const baseActions = {
registry
.dispatch( CORE_UI )
.setValue(
UI_KEY_SHOW_RRM_PUBLICATION_APPROVED_NOTIFICATION,
UI_KEY_READER_REVENUE_MANAGER_SHOW_PUBLICATION_APPROVED_NOTIFICATION,
true
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
MODULES_READER_REVENUE_MANAGER,
MODULE_SLUG,
PUBLICATION_ONBOARDING_STATES,
UI_KEY_SHOW_RRM_PUBLICATION_APPROVED_NOTIFICATION,
UI_KEY_READER_REVENUE_MANAGER_SHOW_PUBLICATION_APPROVED_NOTIFICATION,
} from './constants';

describe( 'modules/reader-revenue-manager publications', () => {
Expand Down Expand Up @@ -100,6 +100,12 @@ describe( 'modules/reader-revenue-manager publications', () => {
} );

it( 'should update the settings and call the saveSettings endpoint', async () => {
const originalDateNow = Date.now;

// Mock the date to be an arbitrary time.
const mockNow = new Date( '2020-01-01 12:30:00' ).getTime();
Date.now = jest.fn( () => mockNow );

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.receiveGetPublications( fixtures.publications );
Expand All @@ -113,12 +119,12 @@ describe( 'modules/reader-revenue-manager publications', () => {
publicationOnboardingStateLastSyncedAtMs: 0,
};

fetchMock.post( settingsEndpoint, {
fetchMock.postOnce( settingsEndpoint, {
body: {
...settings,
publicationOnboardingState:
PUBLICATION_ONBOARDING_STATES.UNSPECIFIED,
publicationOnboardingStateLastSyncedAtMs: Date.now(),
publicationOnboardingStateLastSyncedAtMs: mockNow,
},
status: 200,
} );
Expand Down Expand Up @@ -153,13 +159,12 @@ describe( 'modules/reader-revenue-manager publications', () => {
.select( MODULES_READER_REVENUE_MANAGER )
.getPublicationOnboardingStateLastSyncedAtMs();

// Restore Date.now method.
Date.now = originalDateNow;

// Ensure that the sync time is set.
expect( syncTimeMs ).not.toBe( 0 );

// Ensure that date is within the last 5 seconds.
expect( syncTimeMs ).toBeLessThanOrEqual( Date.now() );

expect( syncTimeMs ).toBeGreaterThan( Date.now() - 5000 );
expect( syncTimeMs ).toBe( mockNow );
} );

it( 'should set UI_KEY_SHOW_RRM_PUBLICATION_APPROVED_NOTIFICATION to true in CORE_UI store', async () => {
Expand All @@ -177,12 +182,12 @@ describe( 'modules/reader-revenue-manager publications', () => {
publicationOnboardingStateLastSyncedAtMs: 0,
};

fetchMock.post( settingsEndpoint, {
fetchMock.postOnce( settingsEndpoint, {
body: {
...settings,
publicationOnboardingState:
PUBLICATION_ONBOARDING_STATES.ONBOARDING_COMPLETE,
publicationOnboardingStateLastSyncedAtMs: Date.now(),
publicationOnboardingStateLastSyncedAtMs: Date.now(), // This is set purely for illustrative purposes, the actual value will be calculated at the point of dispatch.
},
status: 200,
} );
Expand All @@ -191,25 +196,26 @@ describe( 'modules/reader-revenue-manager publications', () => {
.dispatch( MODULES_READER_REVENUE_MANAGER )
.receiveGetSettings( settings );

const uiKeyBeforeAction = registry
.select( CORE_UI )
.getValue(
UI_KEY_SHOW_RRM_PUBLICATION_APPROVED_NOTIFICATION
);
expect(
registry
.select( CORE_UI )
.getValue(
UI_KEY_READER_REVENUE_MANAGER_SHOW_PUBLICATION_APPROVED_NOTIFICATION
)
).toBeUndefined();

await registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.syncPublicationOnboardingState();

const uiKeyAfterAction = registry
.select( CORE_UI )
.getValue(
UI_KEY_SHOW_RRM_PUBLICATION_APPROVED_NOTIFICATION
);

// Ensure that the UI key is set to true.
expect( uiKeyBeforeAction ).toBe( undefined );
expect( uiKeyAfterAction ).toBe( true );
expect(
registry
.select( CORE_UI )
.getValue(
UI_KEY_READER_REVENUE_MANAGER_SHOW_PUBLICATION_APPROVED_NOTIFICATION
)
).toBe( true );
} );
} );

Expand Down

0 comments on commit 909ea55

Please sign in to comment.