-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sanity): move perspective resolution to
resolvePerspective
fun…
…ction
- Loading branch information
Showing
4 changed files
with
102 additions
and
25 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
60 changes: 60 additions & 0 deletions
60
packages/sanity/src/core/util/__tests__/resolvePerspectives.test.ts
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,60 @@ | ||
import {describe, expect, it} from '@jest/globals' | ||
|
||
import {resolvePerspective} from '../resolvePerspective' | ||
|
||
describe('resolvePerspectives', () => { | ||
it('includes the `bundlePerspective` property if a bundle is provided', () => { | ||
expect(resolvePerspective('bundle.x')).toHaveProperty('bundlePerspective') | ||
expect(resolvePerspective('bundle.x')).not.toHaveProperty('perspective') | ||
}) | ||
|
||
it('includes the `perspective` property if a system perspective is provided', () => { | ||
expect(resolvePerspective('x')).toHaveProperty('perspective') | ||
expect(resolvePerspective('x')).not.toHaveProperty('bundlePerspective') | ||
}) | ||
|
||
it(`removes the bundle prefix if it exists`, () => { | ||
expect(resolvePerspective('bundle.x').bundlePerspective).toEqual('x') | ||
expect(resolvePerspective('x').perspective).toEqual('x') | ||
}) | ||
|
||
it('allows the extracted perspectives to be transformed', () => { | ||
expect(resolvePerspective('x', () => ['y'])).toEqual({ | ||
perspective: 'y', | ||
}) | ||
}) | ||
|
||
it('passes the perspective to the `transformPerspectives` function', () => { | ||
expect.assertions(2) | ||
|
||
resolvePerspective('x', (perspectives) => { | ||
expect(perspectives).toEqual(['x']) | ||
return perspectives | ||
}) | ||
|
||
resolvePerspective('bundle.x', (perspectives) => { | ||
expect(perspectives).toEqual(['x']) | ||
return perspectives | ||
}) | ||
}) | ||
|
||
it('passes the perspective type to the `transformPerspectives` function', () => { | ||
expect.assertions(2) | ||
|
||
resolvePerspective('x', (perspectives, isSystemPerspective) => { | ||
expect(isSystemPerspective).toBe(true) | ||
return perspectives | ||
}) | ||
|
||
resolvePerspective('bundle.x', (perspectives, isSystemPerspective) => { | ||
expect(isSystemPerspective).toBe(false) | ||
return perspectives | ||
}) | ||
}) | ||
|
||
it('produces a correctly formatted list of perspectives', () => { | ||
expect(resolvePerspective('x', (perspectives) => perspectives.concat('y'))).toEqual({ | ||
perspective: 'x,y', | ||
}) | ||
}) | ||
}) |
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,32 @@ | ||
/** | ||
* Given a system perspective, or a bundle name prefixed with `bundle.`, returns | ||
* the provided `options` object with either `perspective` or `bundlePerspective` | ||
* applied. | ||
* | ||
* @internal | ||
*/ | ||
export function resolvePerspective( | ||
perspective: string | undefined, | ||
transformPerspectives: (perspectives: string[], isSystemPerspective: boolean) => string[] = ( | ||
perspectives, | ||
) => perspectives, | ||
): | ||
| {perspective: string; bundlePerspective?: never} | ||
| {perspective?: never; bundlePerspective: string} | ||
| Record<PropertyKey, never> { | ||
if (typeof perspective === 'undefined') { | ||
return {} | ||
} | ||
|
||
const bundlePerspective = perspective.split(/^bundle./).at(1) | ||
|
||
if (typeof bundlePerspective === 'string') { | ||
return { | ||
bundlePerspective: transformPerspectives([bundlePerspective], false).join(','), | ||
} | ||
} | ||
|
||
return { | ||
perspective: transformPerspectives([perspective], true).join(','), | ||
} | ||
} |
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