Skip to content

Commit

Permalink
Update tests to match new rendering system
Browse files Browse the repository at this point in the history
  • Loading branch information
amcaplan committed Jan 9, 2025
1 parent 90f10c3 commit 4ce4ba3
Showing 1 changed file with 49 additions and 14 deletions.
63 changes: 49 additions & 14 deletions packages/app/src/cli/services/info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import {DeveloperPlatformClient} from '../utilities/developer-platform-client.js
import {describe, expect, vi, test} from 'vitest'
import {checkForNewVersion} from '@shopify/cli-kit/node/node-package-manager'
import {joinPath} from '@shopify/cli-kit/node/path'
import {TokenizedString, stringifyMessage, unstyled} from '@shopify/cli-kit/node/output'
import {OutputMessage, TokenizedString, stringifyMessage, unstyled} from '@shopify/cli-kit/node/output'
import {inTemporaryDirectory, writeFileSync} from '@shopify/cli-kit/node/fs'
import {InlineToken, renderInfo} from '@shopify/cli-kit/node/ui'
import {CLI_KIT_VERSION} from '@shopify/cli-kit/common/version'

type CustomSection = Exclude<Parameters<typeof renderInfo>[0]['customSections'], undefined>[number]

vi.mock('../prompts/dev.js')
vi.mock('@shopify/cli-kit/node/node-package-manager')
vi.mock('../utilities/developer-platform-client.js')
Expand Down Expand Up @@ -116,7 +119,7 @@ describe('info', () => {
vi.mocked(selectOrganizationPrompt).mockResolvedValue(ORG1)

// When
const result = await info(app, remoteApp, {...infoOptions(), webEnv: true})
const result = (await info(app, remoteApp, {...infoOptions(), webEnv: true})) as OutputMessage

// Then
expect(unstyled(stringifyMessage(result))).toMatchInlineSnapshot(`
Expand All @@ -136,7 +139,7 @@ describe('info', () => {
vi.mocked(selectOrganizationPrompt).mockResolvedValue(ORG1)

// When
const result = await info(app, remoteApp, {...infoOptions(), format: 'json', webEnv: true})
const result = (await info(app, remoteApp, {...infoOptions(), format: 'json', webEnv: true})) as OutputMessage

// Then
expect(unstyled(stringifyMessage(result))).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -184,18 +187,28 @@ describe('info', () => {
vi.mocked(selectOrganizationPrompt).mockResolvedValue(ORG1)

// When
const result = await info(app, remoteApp, infoOptions())
const result = (await info(app, remoteApp, infoOptions())) as CustomSection[]
const uiData = tabularDataSectionFromInfo(result, 'ui_extension_external')
const checkoutData = tabularDataSectionFromInfo(result, 'checkout_ui_extension_external')

// Then
expect(result).toContain('Extensions with errors')

// Doesn't use the type as part of the title
expect(result).not.toContain('📂 ui_extension')
// Shows handle in title
expect(result).toContain('📂 handle-for-extension-1')
expect(JSON.stringify(uiData)).not.toContain('📂 ui_extension')

// Shows handle as title
const uiExtensionTitle = uiData[0]![0]
expect(uiExtensionTitle).toBe('📂 handle-for-extension-1')
// Displays errors
const uiExtensionErrorsRow = errorRow(uiData)
expect(uiExtensionErrorsRow[1]).toStrictEqual({error: 'Mock error with ui_extension'})

// Shows default handle derived from name when no handle is present
expect(result).toContain('📂 extension-2')
expect(result).toContain('! Mock error with ui_extension')
expect(result).toContain('! Mock error with checkout_ui_extension')
const checkoutExtensionTitle = checkoutData[0]![0]
expect(checkoutExtensionTitle).toBe('📂 extension-2')
// Displays errors
const checkoutExtensionErrorsRow = errorRow(checkoutData)
expect(checkoutExtensionErrorsRow[1]).toStrictEqual({error: 'Mock error with checkout_ui_extension'})
})
})

Expand All @@ -222,11 +235,14 @@ describe('info', () => {
vi.mocked(selectOrganizationPrompt).mockResolvedValue(ORG1)

// When
const result = await info(app, remoteApp, infoOptions())
const result = (await info(app, remoteApp, infoOptions())) as CustomSection[]
const uiExtensionsData = tabularDataSectionFromInfo(result, 'ui_extension_external')
const relevantExtension = extensionTitleRow(uiExtensionsData, 'handle-for-extension-1')
const irrelevantExtension = extensionTitleRow(uiExtensionsData, 'point_of_sale')

// Then
expect(result).toContain('📂 handle-for-extension-1')
expect(result).not.toContain('📂 point_of_sale')
expect(relevantExtension).toBeDefined()
expect(irrelevantExtension).not.toBeDefined()
})
})

Expand Down Expand Up @@ -293,3 +309,22 @@ function mockApp({
...(app ? app : {}),
})
}

function tabularDataSectionFromInfo(info: CustomSection[], title: string): InlineToken[][] {
const section = info.find((section) => section.title === title)
if (!section) throw new Error(`Section ${title} not found`)
if (!(typeof section.body === 'object' && 'tabularData' in section.body)) {
throw new Error(`Expected to be a table: ${JSON.stringify(section.body)}`)
}
return section.body.tabularData
}

function errorRow(data: InlineToken[][]): InlineToken[] {
const row = data.find((row: InlineToken[]) => typeof row[0] === 'object' && 'error' in row[0])!
if (!row) throw new Error('Error row not found')
return row
}

function extensionTitleRow(data: InlineToken[][], title: string): InlineToken[] | undefined {
return data.find((row) => typeof row[0] === 'string' && row[0].match(new RegExp(title)))
}

0 comments on commit 4ce4ba3

Please sign in to comment.