Skip to content

Commit

Permalink
Merge pull request #10055 from google/enhancement/9952-update-publica…
Browse files Browse the repository at this point in the history
…tion-settings
  • Loading branch information
nfmohit authored Jan 22, 2025
2 parents 70b3a8f + 737c5ff commit c514853
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 7 deletions.
61 changes: 54 additions & 7 deletions assets/js/modules/reader-revenue-manager/datastore/publications.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
PUBLICATION_ONBOARDING_STATES,
} from './constants';
import { actions as errorStoreActions } from '../../../googlesitekit/data/create-error-store';
import { isFeatureEnabled } from '../../../features';

const fetchGetPublicationsStore = createFetchStore( {
baseName: 'getPublications',
Expand Down Expand Up @@ -235,17 +236,63 @@ const baseActions = {
);
} );
},
// `publicationId` is the identifier used by the API.
// eslint-disable-next-line sitekit/acronym-case
function* ( { publicationId: publicationID, onboardingState } ) {
function* ( {
// `publicationId` is the identifier used by the API.
// eslint-disable-next-line sitekit/acronym-case
publicationId: publicationID,
onboardingState,
paymentOptions,
products,
} ) {
const registry = yield commonActions.getRegistry();

const settings = {
publicationID,
publicationOnboardingState: onboardingState,
};

if ( isFeatureEnabled( 'rrmModuleV2' ) ) {
settings.productIDs = [];
settings.paymentOption = '';

if ( paymentOptions ) {
const paymentOption = Object.keys( paymentOptions ).find(
( key ) => !! paymentOptions[ key ]
);

if ( paymentOption ) {
settings.paymentOption = paymentOption;
}
}

if ( products ) {
settings.productIDs = products.reduce(
( ids, { name } ) => {
if ( ! name ) {
return ids;
}

const productIDSeparatorIndex = name.indexOf( ':' );

if ( productIDSeparatorIndex !== -1 ) {
return [
...ids,
name.substring(
productIDSeparatorIndex + 1
),
];
}

return ids;
},
[]
);
}
}

return registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.setSettings( {
publicationID,
publicationOnboardingState: onboardingState,
} );
.setSettings( settings );
}
),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
READER_REVENUE_MANAGER_MODULE_SLUG,
PUBLICATION_ONBOARDING_STATES,
} from './constants';
import { setEnabledFeatures } from '../../../../../tests/js/test-utils';

describe( 'modules/reader-revenue-manager publications', () => {
let registry;
Expand Down Expand Up @@ -317,6 +318,175 @@ describe( 'modules/reader-revenue-manager publications', () => {
.getPublicationOnboardingState()
).toEqual( onboardingState );
} );

describe( 'with the rrmModuleV2 feature flag enabled', () => {
beforeEach( () => {
setEnabledFeatures( [ 'rrmModuleV2' ] );
} );

it( 'should set the product IDs in state when products are provided', () => {
const products = [
{ name: 'ABC:product-1' },
{ name: 'DEF:product-2' },
];
registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.selectPublication( {
publicationId: 'publication-id',
onboardingState: 'onboarding-state',
products,
} );

expect(
registry
.select( MODULES_READER_REVENUE_MANAGER )
.getProductIDs()
).toEqual( [ 'product-1', 'product-2' ] );
} );

it( 'should set an empty product IDs array when products array is empty', () => {
const products = [];
registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.selectPublication( {
publicationId: 'publication-id',
onboardingState: 'onboarding-state',
products,
} );

expect(
registry
.select( MODULES_READER_REVENUE_MANAGER )
.getProductIDs()
).toEqual( [] );
} );

it( 'should handle products with a missing name property', () => {
const products = [
{ name: 'ABC:product-1' },
{}, // Missing name
{ name: 'DEF:product-2' },
];
registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.selectPublication( {
publicationId: 'publication-id',
onboardingState: 'onboarding-state',
products,
} );

expect(
registry
.select( MODULES_READER_REVENUE_MANAGER )
.getProductIDs()
).toEqual( [ 'product-1', 'product-2' ] );
} );

it( 'should handle products with invalid name format', () => {
const products = [
{ name: 'ABC:product-1' },
{ name: 'invalid-format' }, // No separator
{ name: 'DEF:product-2' },
];
registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.selectPublication( {
publicationId: 'publication-id',
onboardingState: 'onboarding-state',
products,
} );

expect(
registry
.select( MODULES_READER_REVENUE_MANAGER )
.getProductIDs()
).toEqual( [ 'product-1', 'product-2' ] );
} );

it( 'should set the payment option in state when a payment option is provided', () => {
registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.selectPublication( {
publicationId: 'publication-id',
onboardingState: 'onboarding-state',
paymentOptions: {
contributions: null,
subscriptions: true,
noPayment: null,
thankStickers: null,
},
} );

expect(
registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPaymentOption()
).toEqual( 'subscriptions' );
} );

it( 'should set the first true payment option when multiple options are provided', () => {
const paymentOptions = {
subscriptions: null,
contributions: true,
noPayment: true,
thankStickers: null,
};
registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.selectPublication( {
publicationId: 'publication-id',
onboardingState: 'onboarding-state',
paymentOptions,
} );

expect(
registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPaymentOption()
).toEqual( 'contributions' );
} );

it( 'should default to an empty string when all payment options are null', () => {
const paymentOptions = {
subscriptions: null,
contributions: null,
noPayment: null,
thankStickers: null,
};

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.selectPublication( {
publicationId: 'publication-id',
onboardingState: 'onboarding-state',
paymentOptions,
} );

expect(
registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPaymentOption()
).toEqual( '' );
} );

it( 'should default to an empty string when the payment options object is empty', () => {
const paymentOptions = {};

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.selectPublication( {
publicationId: 'publication-id',
onboardingState: 'onboarding-state',
paymentOptions,
} );

expect(
registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPaymentOption()
).toEqual( '' );
} );
} );
} );
} );

Expand Down

0 comments on commit c514853

Please sign in to comment.