-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract host theme wait logic to host-theme-watcher utility and add t…
…ests
- Loading branch information
1 parent
be34097
commit 3119ebc
Showing
4 changed files
with
107 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {UPDATER_TIMEOUT, waitForThemeToBeProcessed} from './host-theme-watcher.js' | ||
import {AdminSession} from '@shopify/cli-kit/node/session' | ||
import {sleep} from '@shopify/cli-kit/node/system' | ||
import {fetchTheme} from '@shopify/cli-kit/node/themes/api' | ||
import {describe, expect, test, vi} from 'vitest' | ||
|
||
vi.mock('@shopify/cli-kit/node/themes/api') | ||
vi.mock('@shopify/cli-kit/node/system') | ||
|
||
describe('HostThemeWatcher', () => { | ||
const themeId = 12345 | ||
const adminSession: AdminSession = {token: 'token', storeFqdn: 'storeFqdn'} | ||
|
||
test('should wait for the theme to be processed', async () => { | ||
// Given | ||
vi.mocked(sleep).mockResolvedValue() | ||
vi.mocked(fetchTheme) | ||
.mockResolvedValueOnce({ | ||
processing: true, | ||
id: themeId, | ||
name: 'Theme', | ||
role: 'development', | ||
createdAtRuntime: true, | ||
}) | ||
.mockResolvedValue({ | ||
processing: false, | ||
id: themeId, | ||
name: 'Theme', | ||
role: 'development', | ||
createdAtRuntime: true, | ||
}) | ||
|
||
// When | ||
await waitForThemeToBeProcessed(themeId, adminSession, Date.now()) | ||
|
||
// Then | ||
expect(fetchTheme).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
test('should throw an error if the theme is not processed within the timeout', async () => { | ||
// Given | ||
vi.mocked(fetchTheme).mockResolvedValue({ | ||
processing: true, | ||
id: themeId, | ||
name: 'Theme', | ||
role: 'development', | ||
createdAtRuntime: true, | ||
}) | ||
|
||
// When | ||
const promise = waitForThemeToBeProcessed(themeId, adminSession, Date.now() - UPDATER_TIMEOUT) | ||
|
||
// Then | ||
await expect(promise).rejects.toThrowError('Theme Processing Timeout') | ||
}) | ||
}) |
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 @@ | ||
import {AbortError} from '@shopify/cli-kit/node/error' | ||
import {AdminSession} from '@shopify/cli-kit/node/session' | ||
import {sleep} from '@shopify/cli-kit/node/system' | ||
import {fetchTheme} from '@shopify/cli-kit/node/themes/api' | ||
|
||
// 5 minutes | ||
export const UPDATER_TIMEOUT = 5 * 60 * 1000 | ||
|
||
export async function waitForThemeToBeProcessed(themeId: number, adminSession: AdminSession, startTime = Date.now()) { | ||
// Each iteration must wait for the response before the next poll is initiated. | ||
// eslint-disable-next-line no-await-in-loop, no-empty | ||
while (await themeIsProcessing(themeId, adminSession, startTime)) {} | ||
} | ||
|
||
async function themeIsProcessing(themeId: number, session: AdminSession, startTime: number) { | ||
const theme = await fetchTheme(themeId, session) | ||
|
||
if (!theme) { | ||
throw new AbortError('No theme app extension host theme found.', 'Please try again.') | ||
} | ||
|
||
const elapsedTime = Date.now() - startTime | ||
|
||
if (theme.processing && elapsedTime >= UPDATER_TIMEOUT) { | ||
throw new AbortError('Theme Processing Timeout', 'Please try again.') | ||
} | ||
|
||
// Sleep for 3 seconds before polling again | ||
await sleep(3) | ||
|
||
return theme.processing | ||
} |