Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance/#8795 - Implement RRM findMatchedPublication() action #9014

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@
export const MODULES_READER_REVENUE_MANAGER = 'modules/reader-revenue-manager';

export const ERROR_CODE_NON_HTTPS_SITE = 'non_https_site';

export const PUBLICATION_ONBOARDING_STATE = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about changing this to PUBLICATION_ONBOARDING_STATES (plural)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that only one state can be referenced at a time for a publication, I thought the singular form was appropriate. I can change it if you prefer the plural.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated ✅

ONBOARDING_COMPLETE: 'ONBOARDING_COMPLETE',
ONBOARDING_ACTION_REQUIRED: 'ONBOARDING_ACTION_REQUIRED',
PENDING_VERIFICATION: 'PENDING_VERIFICATION',
};
37 changes: 35 additions & 2 deletions assets/js/modules/reader-revenue-manager/datastore/publications.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import API from 'googlesitekit-api';
import { commonActions, combineStores } from 'googlesitekit-data';
import { createFetchStore } from '../../../googlesitekit/data/create-fetch-store';
import { MODULES_READER_REVENUE_MANAGER } from './constants';
import {
MODULES_READER_REVENUE_MANAGER,
PUBLICATION_ONBOARDING_STATE,
} from './constants';

const fetchGetPublicationsStore = createFetchStore( {
baseName: 'getPublications',
Expand All @@ -41,7 +44,37 @@ const baseInitialState = {
publications: undefined,
};

const baseActions = {};
const baseActions = {
/**
* Finds a matched publication.
*
* @since n.e.x.t
*
* @return {Object|null} Matched publication; `null` if none found.
*/
*findMatchedPublication() {
const { resolveSelect } = yield commonActions.getRegistry();
const publications = yield commonActions.await(
resolveSelect( MODULES_READER_REVENUE_MANAGER ).getPublications()
);

if ( publications.length === 0 ) {
return null;
}

if ( publications.length === 1 ) {
return publications[ 0 ];
}

const completedOnboardingPublication = publications.find(
( publication ) =>
publication.onboardingState ===
PUBLICATION_ONBOARDING_STATE.ONBOARDING_COMPLETE
);

return completedOnboardingPublication || publications[ 0 ];
},
};

const baseControls = {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import {
untilResolved,
} from '../../../../../tests/js/utils';
import * as fixtures from './__fixtures__';
import { MODULES_READER_REVENUE_MANAGER } from './constants';
import {
MODULES_READER_REVENUE_MANAGER,
PUBLICATION_ONBOARDING_STATE,
} from './constants';

describe( 'modules/reader-revenue-manager publications', () => {
let registry;
Expand Down Expand Up @@ -120,4 +123,71 @@ describe( 'modules/reader-revenue-manager publications', () => {
} );
} );
} );

describe( 'actions', () => {
describe( 'findMatchedPublication', () => {
it( 'should return null if there are no publications', async () => {
registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.receiveGetPublications( [] );

const publication = await registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.findMatchedPublication();

expect( publication ).toBeNull();
} );

it( 'should return the publication if that is the only one in the list', async () => {
registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.receiveGetPublications( [ fixtures.publications[ 0 ] ] );

const publication = await registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.findMatchedPublication();

expect( publication ).toEqual( fixtures.publications[ 0 ] );
} );

it( 'should return the publication with ONBOARDING_COMPLETE if more than one publication exists', async () => {
const completedOnboardingPublication =
fixtures.publications.find(
( publication ) =>
publication.onboardingState ===
PUBLICATION_ONBOARDING_STATE.ONBOARDING_COMPLETE
);

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.receiveGetPublications( fixtures.publications );

const publication = await registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.findMatchedPublication();

expect( publication ).toEqual( completedOnboardingPublication );
} );

it( 'should return the first publication if none have ONBOARDING_COMPLETE', async () => {
const publications = fixtures.publications.map(
( publication ) => ( {
...publication,
onboardingState:
PUBLICATION_ONBOARDING_STATE.PENDING_VERIFICATION,
} )
);

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.receiveGetPublications( publications );

const publication = await registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.findMatchedPublication();

expect( publication ).toEqual( publications[ 0 ] );
} );
} );
} );
} );