-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hasSanityPackageInImportMap (#6832)
* feat: add hasSanityPackageInImportMap * test: add tests for hasSanityPackageInImportMap * return early if sanity found in import map Co-authored-by: Espen Hovlandsdal <espen@hovlandsdal.com> * chore: update internal tag to include comment --------- Co-authored-by: Espen Hovlandsdal <espen@hovlandsdal.com>
- Loading branch information
1 parent
74f8080
commit 8ea7d8f
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
packages/sanity/src/core/environment/hasSanityPackageInImportMap.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,31 @@ | ||
import {describe, expect, it, jest} from '@jest/globals' | ||
|
||
import {hasSanityPackageInImportMap} from './hasSanityPackageInImportMap' | ||
|
||
const querySelectorAllSpy = jest.spyOn(document, 'querySelectorAll') | ||
|
||
describe('hasSanityPackageInImportMap', () => { | ||
it('should return false if document is undefined', () => { | ||
const documentSpy = jest.spyOn(global, 'document', 'get') | ||
documentSpy.mockReturnValueOnce(undefined as any) | ||
expect(hasSanityPackageInImportMap()).toBe(false) | ||
}) | ||
it('should return false if no script with type importmap is found', () => { | ||
querySelectorAllSpy.mockReturnValue([] as any) | ||
expect(hasSanityPackageInImportMap()).toBe(false) | ||
}) | ||
|
||
it('should return true if script with type importmap is found and contains sanity', () => { | ||
querySelectorAllSpy.mockReturnValue([ | ||
{textContent: JSON.stringify({imports: {sanity: 'path/to/sanity'}})}, | ||
] as any) | ||
expect(hasSanityPackageInImportMap()).toBe(true) | ||
}) | ||
|
||
it('should return false if script with type importmap is found but does not contain sanity', () => { | ||
querySelectorAllSpy.mockReturnValue([ | ||
{textContent: JSON.stringify({imports: {other: 'path/to/other'}})}, | ||
] as any) | ||
expect(hasSanityPackageInImportMap()).toBe(false) | ||
}) | ||
}) |
17 changes: 17 additions & 0 deletions
17
packages/sanity/src/core/environment/hasSanityPackageInImportMap.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,17 @@ | ||
/** | ||
* Returns whether or not there is a `sanity` entry in an import map in the current document, | ||
* which usually means that this studio is "auto updating". | ||
* @internal | ||
*/ | ||
export const hasSanityPackageInImportMap = () => { | ||
if (typeof document === 'undefined' || !('querySelectorAll' in document)) { | ||
return false | ||
} | ||
const importMapEntries = document.querySelectorAll('script[type="importmap"]') | ||
return Array.from(importMapEntries).some((entry) => { | ||
if (!entry.textContent) return false | ||
const importMap = JSON.parse(entry.textContent) | ||
const imports = importMap.imports || {} | ||
return 'sanity' in imports | ||
}) | ||
} |