From 94acf40a4ccfb9b9e5e5ff5c8d239530410f257b Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Mon, 27 May 2024 14:04:02 +0530 Subject: [PATCH] Add unit tests for getNewIndexFromPresets function --- .../global-styles/test/utils.spec.js | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 packages/edit-site/src/components/global-styles/test/utils.spec.js diff --git a/packages/edit-site/src/components/global-styles/test/utils.spec.js b/packages/edit-site/src/components/global-styles/test/utils.spec.js new file mode 100644 index 00000000000000..6b07fffbb955db --- /dev/null +++ b/packages/edit-site/src/components/global-styles/test/utils.spec.js @@ -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 ); + } ); + } ); + } ); +} );