forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for getNewIndexFromPresets function
- Loading branch information
1 parent
f4f6d5a
commit 94acf40
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
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 ); | ||
} ); | ||
} ); | ||
} ); | ||
} ); |