-
Notifications
You must be signed in to change notification settings - Fork 427
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
14 changed files
with
154 additions
and
54 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
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
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
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
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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
import {describe, expect, it} from '@jest/globals' | ||
|
||
import {resolveBundlePerspective, resolvePerspectiveOptions} from '../resolvePerspective' | ||
|
||
describe('resolveBundlePerspective', () => { | ||
it('returns the perspective with the `bundle.` prefix removed', () => { | ||
expect(resolveBundlePerspective('bundle.x')).toBe('x') | ||
}) | ||
|
||
it('returns `undefined` if the provided perspective has no `bundle.` prefix', () => { | ||
expect(resolveBundlePerspective('x')).toBeUndefined() | ||
}) | ||
|
||
it('returns `undefined` if no perspective is provided', () => { | ||
expect(resolveBundlePerspective()).toBeUndefined() | ||
}) | ||
}) | ||
|
||
describe('resolvePerspectiveOptions', () => { | ||
it('includes the `bundlePerspective` property if a bundle is provided', () => { | ||
expect(resolvePerspectiveOptions('bundle.x')).toHaveProperty('bundlePerspective') | ||
expect(resolvePerspectiveOptions('bundle.x')).not.toHaveProperty('perspective') | ||
}) | ||
|
||
it('includes the `perspective` property if a system perspective is provided', () => { | ||
expect(resolvePerspectiveOptions('x')).toHaveProperty('perspective') | ||
expect(resolvePerspectiveOptions('x')).not.toHaveProperty('bundlePerspective') | ||
}) | ||
|
||
it(`removes the bundle prefix if it exists`, () => { | ||
expect(resolvePerspectiveOptions('bundle.x').bundlePerspective).toEqual('x') | ||
expect(resolvePerspectiveOptions('x').perspective).toEqual('x') | ||
}) | ||
|
||
it('allows the extracted perspectives to be transformed', () => { | ||
expect(resolvePerspectiveOptions('x', () => ['y'])).toEqual({ | ||
perspective: 'y', | ||
}) | ||
}) | ||
|
||
it('passes the perspective to the `transformPerspectives` function', () => { | ||
expect.assertions(2) | ||
|
||
resolvePerspectiveOptions('x', (perspectives) => { | ||
expect(perspectives).toEqual(['x']) | ||
return perspectives | ||
}) | ||
|
||
resolvePerspectiveOptions('bundle.x', (perspectives) => { | ||
expect(perspectives).toEqual(['x']) | ||
return perspectives | ||
}) | ||
}) | ||
|
||
it('passes the perspective type to the `transformPerspectives` function', () => { | ||
expect.assertions(2) | ||
|
||
resolvePerspectiveOptions('x', (perspectives, isSystemPerspective) => { | ||
expect(isSystemPerspective).toBe(true) | ||
return perspectives | ||
}) | ||
|
||
resolvePerspectiveOptions('bundle.x', (perspectives, isSystemPerspective) => { | ||
expect(isSystemPerspective).toBe(false) | ||
return perspectives | ||
}) | ||
}) | ||
|
||
it('produces a correctly formatted list of perspectives', () => { | ||
expect(resolvePerspectiveOptions('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
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,42 @@ | ||
/** | ||
* If the provided perspective has the `bundle.` prefix, returns it with this | ||
* prefix removed. | ||
* | ||
* @internal | ||
*/ | ||
export function resolveBundlePerspective(perspective?: string): string | undefined { | ||
return perspective?.split(/^bundle./).at(1) | ||
} | ||
|
||
/** | ||
* Given a system perspective, or a bundle name prefixed with `bundle.`, returns | ||
* an object with either `perspective` or `bundlePerspective` properties that | ||
* may be submitted directly to Content Lake APIs. | ||
* | ||
* @internal | ||
*/ | ||
export function resolvePerspectiveOptions( | ||
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 = resolveBundlePerspective(perspective) | ||
|
||
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
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
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
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
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
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