Skip to content

Commit

Permalink
Merge pull request #2334 from Shopify/fix-tests-autocomplete
Browse files Browse the repository at this point in the history
Fix tests in AutocompletePrompt.test.tsx
  • Loading branch information
matteodepalo authored Jul 3, 2023
2 parents 925dc48 + 451394e commit 3ce92a2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
14 changes: 7 additions & 7 deletions packages/app/src/cli/prompts/generate/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {ExtensionFlavorValue} from '../../services/generate/extension.js'
import themeExtension from '../../models/templates/theme-specifications/theme.js'
import {describe, expect, vi, beforeEach, test} from 'vitest'
import {isShopify, isUnitTest} from '@shopify/cli-kit/node/context/local'
import {renderSelectPrompt, renderTextPrompt} from '@shopify/cli-kit/node/ui'
import {renderAutocompletePrompt, renderSelectPrompt, renderTextPrompt} from '@shopify/cli-kit/node/ui'

vi.mock('@shopify/cli-kit/node/context/local')
vi.mock('@shopify/cli-kit/node/ui')
Expand Down Expand Up @@ -42,14 +42,14 @@ describe('extension prompt', async () => {
const extensionTemplate = findExtensionTemplate('ui_extension', allUITemplates)

// Given
vi.mocked(renderSelectPrompt).mockResolvedValueOnce(answers.extensionType)
vi.mocked(renderAutocompletePrompt).mockResolvedValueOnce(answers.extensionType)
vi.mocked(renderTextPrompt).mockResolvedValue(answers.name)

// When
const got = await generateExtensionPrompts(options)

// Then
expect(renderSelectPrompt).toHaveBeenCalledWith(extensionTypeQuestion)
expect(renderAutocompletePrompt).toHaveBeenCalledWith(extensionTypeQuestion)
expect(renderTextPrompt).toHaveBeenCalledWith(extensionNameQuestion)
expect(got).toEqual({
extensionTemplate,
Expand All @@ -70,13 +70,13 @@ describe('extension prompt', async () => {
const extensionTemplate = findExtensionTemplate('ui_extension', allUITemplates)

// Given
vi.mocked(renderSelectPrompt).mockResolvedValueOnce(answers.extensionType)
vi.mocked(renderAutocompletePrompt).mockResolvedValueOnce(answers.extensionType)

// When
const got = await generateExtensionPrompts(options)

// Then
expect(renderSelectPrompt).toHaveBeenCalledWith(extensionTypeQuestion)
expect(renderAutocompletePrompt).toHaveBeenCalledWith(extensionTypeQuestion)
expect(got).toEqual({
extensionTemplate,
extensionContent: [{name: 'my-special-extension', flavor: undefined, index: 0}],
Expand Down Expand Up @@ -197,13 +197,13 @@ describe('extension prompt', async () => {
message: 'Type of extension?',
choices: buildChoices(allFunctionTemplates),
}
vi.mocked(renderSelectPrompt).mockResolvedValueOnce('product_discounts')
vi.mocked(renderAutocompletePrompt).mockResolvedValueOnce('product_discounts')

// When
const got = await generateExtensionPrompts(options)

// Then
expect(renderSelectPrompt).toHaveBeenCalledWith(functionTypes)
expect(renderAutocompletePrompt).toHaveBeenCalledWith(functionTypes)
expect(got).toEqual({
extensionTemplate,
extensionContent: [{name: 'my-product-discount', index: 0, flavor: 'rust'}],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('AutocompletePrompt', async () => {
)

expect(renderInstance.lastFrame()).toMatchInlineSnapshot(`
"? Associate your project with the org Castile Ventures?
"? Associate your project with the org Castile Ventures? [36m[7mT[27m[2mype to search...[22m[39m
Automations
> first
Expand Down Expand Up @@ -796,7 +796,7 @@ describe('AutocompletePrompt', async () => {
)

expect(renderInstance.lastFrame()).toMatchInlineSnapshot(`
"? Associate your project with the org Castile Ventures?
"? Associate your project with the org Castile Ventures? [36m[7mT[27m[2mype to search...[22m[39m
Automations
> first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ enum PromptState {
}

const PAGE_SIZE = 25
const MIN_NUMBER_OF_ITEMS_FOR_SEARCH = 5

// eslint-disable-next-line react/function-component-definition
function AutocompletePrompt<T>({
Expand All @@ -57,7 +58,7 @@ function AutocompletePrompt<T>({
const [searchTerm, setSearchTerm] = useState('')
const [searchResults, setSearchResults] = useState<SelectItem<T>[]>(paginatedInitialChoices.slice(0, PAGE_SIZE))
const {stdout} = useStdout()
const canSearch = initialChoices.length >= 5
const canSearch = initialChoices.length > MIN_NUMBER_OF_ITEMS_FOR_SEARCH
const [hasMorePages, setHasMorePages] = useState(initialHasMorePages)
const [wrapperHeight, setWrapperHeight] = useState(0)
const [promptAreaHeight, setPromptAreaHeight] = useState(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ function Item<T>({
)
}

const MAX_AVAILABLE_LINES = 25

// eslint-disable-next-line react/function-component-definition
function SelectInputInner<T>(
{
Expand All @@ -132,7 +134,7 @@ function SelectInputInner<T>(
hasMorePages = false,
morePagesMessage,
infoMessage,
availableLines = 25,
availableLines = MAX_AVAILABLE_LINES,
submitWithShortcuts = false,
onSubmit,
}: SelectInputProps<T>,
Expand Down

0 comments on commit 3ce92a2

Please sign in to comment.