Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): boolean value in search shows actual value #7623

Merged
merged 2 commits into from
Oct 16, 2024
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"test": "run-s test:vitest test:jest",
"test:jest": "jest --forceExit",
"test:vitest": "vitest --run",
"test:vitest:watch": "vitest --watch",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a watch command, let me know if y'all are not a fan

"test:e2e": "playwright test",
"test:exports": "turbo run test --filter=@repo/test-exports",
"tsdoc:dev": "sanity-tsdoc dev",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {useMemo} from 'react'
import {styled} from 'styled-components'

import {TextWithTone} from '../../../../../../components'
import {useTranslation} from '../../../../../../i18n'
import {type TFunction, useTranslation} from '../../../../../../i18n'
import {Translate, type TranslateComponentMap} from '../../../../../../i18n/Translate'
import {isRecord} from '../../../../../../util'
import {useSearchState} from '../../contexts/search/useSearchState'
Expand Down Expand Up @@ -80,20 +80,27 @@ export function FilterLabel({filter, fontSize = 1, showContent = true}: FilterLa
t={t}
i18nKey={operator?.descriptionKey}
components={components}
values={getFilterValues(filter)}
values={getFilterValues(filter, t)}
/>
</Flex>
)
}

function getFilterValues(filter: SearchFilter): SearchFilterValues {
function getFilterValues(
filter: SearchFilter,
t: TFunction<'translation', undefined>,
): SearchFilterValues {
const values: SearchFilterValues = {}
if (typeof filter.value === 'number') {
values.count = filter.value
}
if (isStringOrNumber(filter.value)) {
values.value = filter.value
}
if (typeof filter.value === 'boolean') {
// Cast boolean into a string value
values.value = filter.value ? t('search.filter-boolean-true') : t('search.filter-boolean-false')
}
if (isRecord(filter.value) && 'from' in filter.value && isStringOrNumber(filter.value.from)) {
values.from = filter.value.from
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import {render, screen} from '@testing-library/react'
import {type SanityClient} from 'sanity'
import {describe, expect, test} from 'vitest'

import {createMockSanityClient} from '../../../../../../../../../test/mocks/mockSanityClient'
import {createTestProvider} from '../../../../../../../../../test/testUtils/TestProvider'
import {SearchProvider} from '../../../contexts/search/SearchProvider'
import {type SearchFilter} from '../../../types'
import {FilterLabel} from '../FilterLabel'

describe('FilterLabel', () => {
const mockFilter: SearchFilter = {
fieldId: 'boolean-title-boolean-test',
filterName: 'boolean',
operatorType: 'booleanEqual',
value: true,
}

const schema = {
types: [
{
name: 'test',
type: 'document',
fields: [
{
name: 'title',
type: 'boolean',
},
],
},
],
}

const client = createMockSanityClient() as unknown as SanityClient

test('renders the filter label with field, operator, and value', async () => {
const TestProvider = await createTestProvider({
client,
config: {
name: 'default',
projectId: 'test',
dataset: 'test',
schema,
},
})
render(
<TestProvider>
<SearchProvider>
<FilterLabel filter={mockFilter} />
</SearchProvider>
</TestProvider>,
)

expect(screen.getByText('Title')).toBeInTheDocument()
expect(screen.getByText('is')).toBeInTheDocument()
expect(screen.getByText('True')).toBeInTheDocument()
})

test('renders only the field when showContent is false', async () => {
const TestProvider = await createTestProvider({
client,
config: {
name: 'default',
projectId: 'test',
dataset: 'test',
schema,
},
})
render(
<TestProvider>
<SearchProvider>
<FilterLabel filter={mockFilter} showContent={false} />
</SearchProvider>
</TestProvider>,
)

expect(screen.getByText('Title')).toBeInTheDocument()
expect(screen.queryByText('is')).not.toBeInTheDocument()
expect(screen.queryByText('True')).not.toBeInTheDocument()
})

test('handles missing operator descriptionKey', async () => {
const filterWithoutDescription: SearchFilter = {
...mockFilter,
operatorType: 'unknown',
}

const TestProvider = await createTestProvider({
client,
config: {
name: 'default',
projectId: 'test',
dataset: 'test',
schema,
},
})

render(
<TestProvider>
<SearchProvider>
<FilterLabel filter={filterWithoutDescription} />
</SearchProvider>
</TestProvider>,
)

expect(screen.getByText('Title')).toBeInTheDocument()
expect(screen.queryByText('is')).not.toBeInTheDocument()
expect(screen.queryByText('True')).not.toBeInTheDocument()
})
})
4 changes: 2 additions & 2 deletions packages/sanity/test/testUtils/TestProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import {AddonDatasetContext} from 'sanity/_singletons'

import {
CopyPasteProvider,
LocaleProviderBase,
type LocaleResourceBundle,
ResourceCacheProvider,
type SingleWorkspace,
SourceProvider,
usEnglishLocale,
WorkspaceProvider,
} from '../../src/core'
import {studioDefaultLocaleResources} from '../../src/core/i18n/bundles/studio'
import {LocaleProviderBase} from '../../src/core/i18n/components/LocaleProvider'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason the test was failing without this change. I think this probably has to do with some circular dependency somewhere and it's better to avoid barrel imports

import {prepareI18n} from '../../src/core/i18n/i18nConfig'
import {usEnglishLocale} from '../../src/core/i18n/locales'
import {route, RouterProvider} from '../../src/router'
import {getMockWorkspace} from './getMockWorkspaceFromConfig'

Expand Down
Loading