Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmengo committed Aug 31, 2024
1 parent 94bae86 commit 58e0307
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {fetchChecksums} from '@shopify/cli-kit/node/themes/api'
import {buildTheme} from '@shopify/cli-kit/node/themes/factories'
import {ThemeAsset} from '@shopify/cli-kit/node/themes/types'
import {DEVELOPMENT_THEME_ROLE} from '@shopify/cli-kit/node/themes/utils'
import {describe, expect, test, vi} from 'vitest'
import {beforeEach, describe, expect, test, vi} from 'vitest'

vi.mock('@shopify/cli-kit/node/themes/api')
vi.mock('./theme-reconciliation.js')
Expand All @@ -17,20 +17,22 @@ describe('reconcileAndPollThemeEditorChanges', async () => {
const developmentTheme = buildTheme({id: 1, name: 'Theme', role: DEVELOPMENT_THEME_ROLE})!
const adminSession = {token: '', storeFqdn: ''}

beforeEach(() => {
vi.mocked(reconcileJsonFiles).mockResolvedValue({
workPromise: Promise.resolve(),
})
})

test('should call pollThemeEditorChanges with updated checksums if the remote theme was been updated during reconciliation', async () => {
// Given
const files = new Map<string, ThemeAsset>([])
const defaultThemeFileSystem = fakeThemeFileSystem('tmp', files)
const initialRemoteChecksums = [{checksum: '1', key: 'templates/asset.json'}]

vi.mocked(reconcileJsonFiles).mockResolvedValue({
reconciliationFinishedPromise: Promise.resolve(),
readyForReconciliationPromise: Promise.resolve(),
})
vi.mocked(fetchChecksums).mockResolvedValue([{checksum: '2', key: 'templates/asset.json'}])

// When
await reconcileAndPollThemeEditorChanges(
const {workPromise, updatedRemoteChecksumsPromise} = await reconcileAndPollThemeEditorChanges(
developmentTheme,
adminSession,
initialRemoteChecksums,
Expand All @@ -41,6 +43,8 @@ describe('reconcileAndPollThemeEditorChanges', async () => {
only: [],
},
)
await workPromise
await updatedRemoteChecksumsPromise

// Then
expect(pollThemeEditorChanges).toHaveBeenCalledWith(
Expand All @@ -56,34 +60,6 @@ describe('reconcileAndPollThemeEditorChanges', async () => {
)
})

test('should not call reconcileJsonFiles when remote theme contains no files', async () => {
// Given
const files = new Map<string, ThemeAsset>([])
const defaultThemeFileSystem = fakeThemeFileSystem('tmp', files)
const emptyRemoteChecksums: [] = []
const newFileSystem = fakeThemeFileSystem('tmp', new Map<string, ThemeAsset>([]))

vi.mocked(fetchChecksums).mockResolvedValue([])
const readySpy = vi.spyOn(defaultThemeFileSystem, 'ready').mockResolvedValue()

// When
await reconcileAndPollThemeEditorChanges(
developmentTheme,
adminSession,
emptyRemoteChecksums,
defaultThemeFileSystem,
{
noDelete: false,
ignore: [],
only: [],
},
)

// Then
expect(reconcileJsonFiles).not.toHaveBeenCalled()
expect(pollThemeEditorChanges).toHaveBeenCalled()
})

test('should wait for the local theme file system to be ready before reconciling', async () => {
// Given
const files = new Map<string, ThemeAsset>([])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function reconcileAndPollThemeEditorChanges(
outputDebug('Initiating theme asset reconciliation process')
await localThemeFileSystem.ready()

const {workPromise} = await reconcileJsonFiles(remoteChecksums, localThemeFileSystem, targetTheme, session, options)
const {workPromise} = await reconcileJsonFiles(targetTheme, session, remoteChecksums, localThemeFileSystem, options)

const updatedRemoteChecksumsPromise = workPromise.then(async () => {
const updatedRemoteChecksums = await fetchChecksums(targetTheme.id, session)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ describe('setupDevServer', () => {
},
}
vi.mocked(reconcileAndPollThemeEditorChanges).mockResolvedValue({
updatedRemoteChecksums: [],
readyForReconciliationPromise: Promise.resolve(),
reconciliationFinishedPromise: Promise.resolve(),
updatedRemoteChecksumsPromise: Promise.resolve([]),
workPromise: Promise.resolve(),
})

// When
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ vi.mock('./theme-fs.js')
vi.mock('./theme-downloader.js')
vi.mock('./theme-uploader.js')

describe('reconcileThemeFiles', () => {
describe('reconcileJsonFiles', () => {
const developmentTheme = buildTheme({id: 1, name: 'Theme', role: DEVELOPMENT_THEME_ROLE})!
const adminSession = {token: '', storeFqdn: ''}
const remoteChecksums: Checksum[] = []
const remoteChecksums: Checksum[] = [{checksum: '1', key: 'config/settings_schema.json'}]
const files = new Map<string, ThemeAsset>([])
const defaultThemeFileSystem = fakeThemeFileSystem('tmp', files)
const defaultOptions = {noDelete: false, only: [], ignore: []}
Expand Down Expand Up @@ -199,7 +199,11 @@ describe('reconcileThemeFiles', () => {
)

// Then
expect(renderSelectPrompt).not.toHaveBeenCalled()
expect(renderSelectPrompt).not.toHaveBeenCalledWith(
expect.any(Array),
'The files listed below are only present locally. What would you like to do?',
expect.any(Object),
)
expect(spy).not.toHaveBeenCalledWith()
})
})
Expand Down Expand Up @@ -245,9 +249,28 @@ describe('reconcileThemeFiles', () => {
expect(fetchThemeAsset).not.toHaveBeenCalled()
})
})

test('should not perform any work when remote checksums are empty', async () => {
// Given
const files = new Map<string, ThemeAsset>([])
const defaultThemeFileSystem = fakeThemeFileSystem('tmp', files)
const emptyRemoteChecksums: Checksum[] = []

// When
await reconcileAndWaitForReconciliationFinish(
developmentTheme,
adminSession,
emptyRemoteChecksums,
defaultThemeFileSystem,
defaultOptions,
)

// Then
expect(renderSelectPrompt).not.toHaveBeenCalled()
})
})

async function reconcileAndWaitForReconciliationFinish(...args: Parameters<typeof reconcileJsonFiles>) {
const {reconciliationFinishedPromise} = await reconcileJsonFiles(...args)
return reconciliationFinishedPromise
const {workPromise} = await reconcileJsonFiles(...args)
return workPromise
}

0 comments on commit 58e0307

Please sign in to comment.