Skip to content

Commit

Permalink
Add unit tests for getNewIndexFromPresets function
Browse files Browse the repository at this point in the history
  • Loading branch information
amitraj2203 committed May 27, 2024
1 parent f4f6d5a commit 94acf40
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions packages/edit-site/src/components/global-styles/test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Internal dependencies
*/
import { getNewIndexFromPresets } from '../utils';

const validPresets = {
single: [ { slug: 'preset-1' } ],
multiple: [ { slug: 'preset-1' }, { slug: 'preset-2' } ],
withGaps: [ { slug: 'preset-1' }, { slug: 'preset-3' } ],
mixedPrefixes: [
{ slug: 'preset-1' },
{ slug: 'shadow-2' },
{ slug: 'preset-3' },
],
nonStringSlug: [ { slug: 'preset-1' }, { slug: 2 } ],
emptyArray: [],
};

const invalidPresets = {
noMatch: [ { slug: 'preset-alpha' }, { slug: 'preset-beta' } ],
emptySlug: [ { slug: '' } ],
nullSlug: [ { slug: null } ],
undefinedSlug: [ { slug: undefined } ],
nonObjectElements: [ 'preset-1' ],
};

const getExpectedIndex = ( presetKey, presets ) => {
if ( presetKey === 'withGaps' ) {
return 4;
}
if ( presetKey === 'mixedPrefixes' ) {
return 4;
}
if ( presetKey === 'nonStringSlug' ) {
return 2;
}
return presets.length + 1;
};

describe( 'getNewIndexFromPresets', () => {
Object.entries( validPresets ).forEach( ( [ presetKey, presets ] ) => {
describe( `test valid preset format: ${ presetKey }`, () => {
const newIndex = getNewIndexFromPresets( presets, 'preset-' );
it( `should return correct new index for ${ presetKey }`, () => {
const expectedIndex = getExpectedIndex( presetKey, presets );
expect( newIndex ).toBe( expectedIndex );
} );
} );
} );

Object.entries( invalidPresets ).forEach( ( [ presetKey, presets ] ) => {
describe( `test invalid preset format: ${ presetKey }`, () => {
const newIndex = getNewIndexFromPresets( presets, 'preset-' );
it( `should return 1 for ${ presetKey }`, () => {
expect( newIndex ).toBe( 1 );
} );
} );
} );
} );

0 comments on commit 94acf40

Please sign in to comment.