Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/js/components/ManageMenu/CommunityCloud/CloudSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { LoadingStatusNotices } from '../../common/LoadingStatusNotices'
import { SnippetViewToggle } from '../../common/SnippetViewToggle'
import { CloudSnippetAuthor, CloudSnippetCard } from '../../common/cloud/CloudSnippetCard'
import { CloudSnippetsTable } from './CloudSnippetsTable'
import { useCloudSearch } from './WithCloudSearchContext'
import { parseSearchMethod, useCloudSearch } from './WithCloudSearchContext'
import { SearchFilters } from './SearchFilters'
import type { CloudSearchResults } from './WithCloudSearchContext'
import type { TableNavProps } from '../../common/ListTable/TableNavigation'
Expand All @@ -37,10 +37,10 @@ const SearchBox = () => {
<select
id={searchMethodId}
value={searchParams.method}
onChange={event =>
updateSearchParams({ method: 'codevault' === event.target.value ? 'codevault' : 'term' })}
onChange={({ target: { value } }) => updateSearchParams({ method: parseSearchMethod(value) })}
>
<option value="term">{__('Search by keyword', 'code-snippets')}</option>
<option value="ai">{__('Search with AI', 'code-snippets')}</option>
<option value="codevault">{__('Name of library', 'code-snippets')}</option>
</select>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const updateSearchQueryParams = (params: CloudSearchParams) => {
[SEARCH_PARAM_VARS.type]: params.type || undefined,
[SEARCH_PARAM_VARS.query]: params.query || undefined,
[SEARCH_PARAM_VARS.status]: params.status || undefined,
[SEARCH_PARAM_VARS.method]: 'codevault' === params.method ? 'codevault' : undefined,
[SEARCH_PARAM_VARS.method]: 'term' === params.method ? undefined : params.method,
[SEARCH_PARAM_VARS.category]: params.category || undefined
})
}
Expand Down
33 changes: 33 additions & 0 deletions tests/e2e/code-snippets-community-featured.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const isFeaturedRequest = (url: URL): boolean =>
url.pathname.includes('/cloud/snippets/featured') ||
true === url.searchParams.get('rest_route')?.includes('/cloud/snippets/featured')

const isSearchRequest = (url: URL): boolean =>
url.pathname.endsWith('/cloud/snippets') ||
true === url.searchParams.get('rest_route')?.endsWith('/cloud/snippets')

const isSnippetDownloadRequest = (url: URL): boolean =>
url.pathname.includes('/cloud/snippets/501/download') ||
true === url.searchParams.get('rest_route')?.includes('/cloud/snippets/501/download')
Expand Down Expand Up @@ -139,6 +143,35 @@ test.describe('Community Cloud Featured Snippets', () => {
await expect(page.locator('.cloud-snippets-heading', { hasText: 'Featured Snippets' })).not.toBeVisible()
})

test('Searches with AI and keeps the method after a reload', async ({ page }) => {
const searchMethods: (string | null)[] = []

await page.route(isFeaturedRequest, route => route.fulfill({
contentType: 'application/json',
body: JSON.stringify(makeFeaturedResponse())
}))
await page.route(isSearchRequest, route => {
searchMethods.push(new URL(route.request().url()).searchParams.get('searchMethod'))
return route.fulfill({
contentType: 'application/json',
body: JSON.stringify(makeFeaturedResponse())
})
})
await openCommunityCloud(page)

const searchMethod = page.getByLabel('Search method')
await searchMethod.selectOption({ label: 'Search with AI' })
await page.locator('#cloud-search-query').fill('disable comments')
await page.locator('.cloud-search-form').getByRole('button', { name: /Search Cloud Library/i }).click()

await expect.poll(() => searchMethods).toEqual(['ai'])
await expect(page).toHaveURL(/[?&]by=ai(?:&|$)/)

await page.reload()
await expect(searchMethod).toHaveValue('ai')
await expect.poll(() => searchMethods).toEqual(['ai', 'ai'])
})

test('Announces loading and error states while featured snippets resolve', async ({ page }) => {
let releaseRequest: VoidFunction = () => undefined
const requestPending = new Promise<void>(resolve => {
Expand Down
Loading