Skip to content

Commit

Permalink
Add JS test cases forRRM settings validations.
Browse files Browse the repository at this point in the history
  • Loading branch information
hussain-t committed Jan 13, 2025
1 parent cc37e4e commit c027dc8
Showing 1 changed file with 124 additions and 3 deletions.
127 changes: 124 additions & 3 deletions assets/js/modules/reader-revenue-manager/datastore/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ import API from 'googlesitekit-api';
import { createTestRegistry } from '../../../../../tests/js/utils';
import { MODULES_READER_REVENUE_MANAGER } from './constants';
import {
INVARIANT_INVALID_PAYMENT_OPTION,
INVARIANT_INVALID_POST_TYPES,
INVARIANT_INVALID_PRODUCT_ID,
INVARIANT_INVALID_PRODUCT_IDS,
INVARIANT_INVALID_PUBLICATION_ID,
INVARIANT_INVALID_PUBLICATION_ONBOARDING_STATE,
INVARIANT_INVALID_SNIPPET_MODE,
validateCanSubmitChanges,
} from './settings';

Expand All @@ -44,10 +49,21 @@ describe( 'modules/reader-revenue-manager settings', () => {
} );

describe( 'validateCanSubmitChanges', () => {
const validSettings = {
publicationID: 'ABCDEFGH',
publicationOnboardingState: 'ONBOARDING_ACTION_REQUIRED',
publicationOnboardingStateChanged: false,
snippetMode: 'post_types',
postTypes: [ 'post' ],
productID: 'valid-id',
productIDs: [ 'valid' ],
paymentOption: 'valid-option',
};

it( 'should throw invariant error for invalid publication ID of type number', () => {
const settings = {
...validSettings,
publicationID: 12345,
publicationOnboardingState: '',
};

registry
Expand All @@ -61,8 +77,8 @@ describe( 'modules/reader-revenue-manager settings', () => {

it( 'should throw invariant error for invalid publication ID with special chars', () => {
const settings = {
...validSettings,
publicationID: 'ABCD&*12345',
publicationOnboardingState: 'ONBOARDING_ACTION_REQUIRED',
};

registry
Expand All @@ -76,7 +92,7 @@ describe( 'modules/reader-revenue-manager settings', () => {

it( 'should throw invariant error for invalid publication onboarding state', () => {
const settings = {
publicationID: 'ABCDEFGH',
...validSettings,
publicationOnboardingState: 'invalid_state',
};

Expand All @@ -88,5 +104,110 @@ describe( 'modules/reader-revenue-manager settings', () => {
INVARIANT_INVALID_PUBLICATION_ONBOARDING_STATE
);
} );

it( 'should throw invariant error for invalid snippet mode', () => {
const settings = {
...validSettings,
snippetMode: 'invalid-mode',
};

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.setSettings( settings );

expect( () => validateCanSubmitChanges( registry.select ) ).toThrow(
INVARIANT_INVALID_SNIPPET_MODE
);
} );

it( 'should throw invariant error for invalid post types', () => {
const settings = {
...validSettings,
postTypes: 'not-an-array',
};

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.setSettings( settings );

expect( () => validateCanSubmitChanges( registry.select ) ).toThrow(
INVARIANT_INVALID_POST_TYPES
);
} );

it( 'should throw invariant error for post types with non-string elements', () => {
const settings = {
...validSettings,
postTypes: [ 'post', 123, true ],
};

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.setSettings( settings );

expect( () => validateCanSubmitChanges( registry.select ) ).toThrow(
INVARIANT_INVALID_POST_TYPES
);
} );

it( 'should throw invariant error for invalid product ID', () => {
const settings = {
...validSettings,
productID: [ 'not-a-string' ],
};

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.setSettings( settings );

expect( () => validateCanSubmitChanges( registry.select ) ).toThrow(
INVARIANT_INVALID_PRODUCT_ID
);
} );

it( 'should throw invariant error for invalid product IDs', () => {
const settings = {
...validSettings,
productIDs: 'not-an-array',
};

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.setSettings( settings );

expect( () => validateCanSubmitChanges( registry.select ) ).toThrow(
INVARIANT_INVALID_PRODUCT_IDS
);
} );

it( 'should throw invariant error for product IDs with non-string elements', () => {
const settings = {
...validSettings,
productIDs: [ 'valid', 123, true ],
};

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.setSettings( settings );

expect( () => validateCanSubmitChanges( registry.select ) ).toThrow(
INVARIANT_INVALID_PRODUCT_IDS
);
} );

it( 'should throw invariant error for invalid payment option', () => {
const settings = {
...validSettings,
paymentOption: [ 'not-a-string' ],
};

registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.setSettings( settings );

expect( () => validateCanSubmitChanges( registry.select ) ).toThrow(
INVARIANT_INVALID_PAYMENT_OPTION
);
} );
} );
} );

0 comments on commit c027dc8

Please sign in to comment.