Skip to content

Commit

Permalink
Fix: JS tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitrox committed Oct 9, 2024
1 parent 85ff7c8 commit 0ce70d5
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ describe( 'PublicationOnboardingStateNotice', () => {
.receiveGetSettings( {
publicationID: 'QRSTUVWX',
publicationOnboardingState: ONBOARDING_ACTION_REQUIRED,
publicationOnboardingStateLastSyncedAtMs: 0,
publicationOnboardingStateChanged: false,
} );

fetchMock.getOnce( publicationsEndpoint, {
Expand Down Expand Up @@ -214,7 +214,7 @@ describe( 'PublicationOnboardingStateNotice', () => {
data: {
publicationID: 'QRSTUVWX',
publicationOnboardingState: ONBOARDING_COMPLETE,
publicationOnboardingStateLastSyncedAtMs: Date.now(),
publicationOnboardingStateChanged: false,
},
},
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,6 @@ describe( 'PublicationSelect', () => {

await waitForRegistry();

// Verify that the onboarding state last synced timestamp is
// not set at this point.
expect(
registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPublicationOnboardingStateLastSyncedAtMs()
).toBeUndefined();

// Click the label to expose the elements in the menu.
fireEvent.click( container.querySelector( '.mdc-floating-label' ) );
// Click this element to select it and fire the onChange event.
Expand All @@ -152,14 +144,6 @@ describe( 'PublicationSelect', () => {

expect( publicationID ).toEqual( newPublicationID );
expect( onboardingState ).toEqual( newOnboardingState );

// Verify that the onboarding state last synced timestamp is
// set to a positive integer after selection.
expect(
registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPublicationOnboardingStateLastSyncedAtMs()
).toBeGreaterThan( 0 );
}
);
} );
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ export default function PublicationApprovedOverlayNotification() {
const viewContext = useViewContext();
const isViewOnly = useViewOnly();
const dashboardType = useDashboardType();
const { dispatch } = useDispatch();
const { saveSettings, setPublicationOnboardingStateChanged } = useDispatch(
MODULES_READER_REVENUE_MANAGER
);
const { publicationOnboardingState, publicationOnboardingStateChanged } =
useSelect( ( select ) =>
select( MODULES_READER_REVENUE_MANAGER ).getSettings()
useSelect(
( select ) =>
select( MODULES_READER_REVENUE_MANAGER ).getSettings() || {}
);
const initialPublicationOnboardingStateChanged = useRef(
publicationOnboardingStateChanged
Expand Down Expand Up @@ -122,13 +125,10 @@ export default function PublicationApprovedOverlayNotification() {
// In useEffect, set publicationOnboardingStateChanged to false using setPublicationOnboardingStateChanged method and save the setting using saveSettings action. This effect should be run only once when component is mounted.
useEffect( () => {
if ( initialPublicationOnboardingStateChanged.current === true ) {
dispatch(
MODULES_READER_REVENUE_MANAGER
).setPublicationOnboardingStateChanged( false );

dispatch( MODULES_READER_REVENUE_MANAGER ).saveSettings();
setPublicationOnboardingStateChanged( false );
saveSettings();
}
}, [ dispatch ] );
}, [ saveSettings, setPublicationOnboardingStateChanged ] );

return (
<OverlayNotification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ import {
} from '../../../../googlesitekit/constants';
import * as tracking from '../../../../util/tracking';
import { Provider as ViewContextProvider } from '../../../../components/Root/ViewContextContext';
import { UI_KEY_READER_REVENUE_MANAGER_SHOW_PUBLICATION_APPROVED_NOTIFICATION } from '../../datastore/constants';
import {
MODULES_READER_REVENUE_MANAGER,
UI_KEY_READER_REVENUE_MANAGER_SHOW_PUBLICATION_APPROVED_NOTIFICATION,
} from '../../datastore/constants';
import { CORE_USER } from '../../../../googlesitekit/datastore/user/constants';

const mockTrackEvent = jest.spyOn( tracking, 'trackEvent' );
Expand All @@ -49,6 +52,10 @@ describe( 'PublicationApprovedOverlayNotification', () => {
'^/google-site-kit/v1/core/user/data/dismiss-item'
);

const settingsEndpoint = new RegExp(
'^/google-site-kit/v1/modules/reader-revenue-manager/data/settings'
);

beforeEach( () => {
mockTrackEvent.mockClear();
registry = createTestRegistry();
Expand All @@ -58,6 +65,20 @@ describe( 'PublicationApprovedOverlayNotification', () => {
UI_KEY_READER_REVENUE_MANAGER_SHOW_PUBLICATION_APPROVED_NOTIFICATION,
true
);

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.receiveGetSettings( {
publicationOnboardingState: 'ONBOARDING_COMPLETE',
publicationOnboardingStateChanged: true,
} );

fetchMock.postOnce( settingsEndpoint, ( _url, opts ) => {
const { data } = JSON.parse( opts.body );

// Return the same settings passed to the API.
return { body: data, status: 200 };
} );
} );

it( 'should render the component with correct title and description', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,20 +229,14 @@ describe( 'RRMSetupSuccessSubtleNotification', () => {
);

it( 'should sync onboarding state when the window is refocused 15 seconds after clicking the CTA', 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 );

jest.useFakeTimers();

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.receiveGetSettings( {
publicationID: 'QRSTUVWX',
publicationOnboardingState: ONBOARDING_ACTION_REQUIRED,
publicationOnboardingStateLastSyncedAtMs: 0,
publicationOnboardingStateChanged: false,
} );

fetchMock.getOnce( publicationsEndpoint, {
Expand Down Expand Up @@ -304,7 +298,7 @@ describe( 'RRMSetupSuccessSubtleNotification', () => {
data: {
publicationID: 'QRSTUVWX',
publicationOnboardingState: ONBOARDING_COMPLETE,
publicationOnboardingStateLastSyncedAtMs: Date.now(),
publicationOnboardingStateChanged: false,
},
},
} );
Expand Down Expand Up @@ -332,8 +326,5 @@ describe( 'RRMSetupSuccessSubtleNotification', () => {
'Your Reader Revenue Manager account was successfully set up, but your publication still requires further setup in Reader Revenue Manager.'
)
).not.toBeInTheDocument();

// Restore Date.now method.
Date.now = originalDateNow;
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,6 @@ const baseActions = {
return;
}

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
// The "last synced" value should reflect the real time this action
// was performed, so we don't use the reference date here.
// eslint-disable-next-line sitekit/no-direct-date
.setPublicationOnboardingStateLastSyncedAtMs( Date.now() );

yield commonActions.await(
registry.dispatch( MODULES_READER_REVENUE_MANAGER ).saveSettings()
);
Expand Down
Loading

0 comments on commit 0ce70d5

Please sign in to comment.