-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shadow Panel: Generates unique shadow slugs by finding max suffix and…
… incrementing it (#61997) * Add function to generate unique shadow slugs by finding max suffix and incrementing it * Created a generic function getNewIndexFromPresets that returns the next available index. * Adds JSdoc to getNewIndexFromPresets * Add unit tests for getNewIndexFromPresets function * Refactor getNewIndexFromPresets function to improve readability. Co-authored-by: amitraj2203 <[email protected]> Co-authored-by: t-hamano <[email protected]> Co-authored-by: talldan <[email protected]> Co-authored-by: vk17-starlord <[email protected]>
- Loading branch information
1 parent
8dac7db
commit e8bec8d
Showing
3 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/edit-site/src/components/global-styles/test/utils.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters