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

feat(react-instantsearch-hooks web): rename <Stats> translation #5756

Merged
merged 4 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions packages/react-instantsearch-hooks-web/src/ui/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type StatsProps = React.ComponentProps<'div'> & {
areHitsSorted?: boolean;
classNames?: Partial<StatsClassNames>;
translations: {
stats: StatsTranslations;
rootElementText: StatsTranslations;
};
};

Expand Down Expand Up @@ -49,7 +49,7 @@ export function Stats({
className={cx('ais-Stats', classNames.root, props.className)}
>
<span className="ais-Stats-text">
{translations.stats(translationOptions)}
{translations.rootElementText(translationOptions)}
</span>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Stats', () => {
areHitsSorted: false,
nbSortedHits: 100,
translations: {
stats: ({
rootElementText: ({
nbHits,
processingTimeMS,
nbSortedHits,
Expand Down Expand Up @@ -153,7 +153,9 @@ describe('Stats', () => {
test('renders with translations', () => {
const translationFn = ({ areHitsSorted }: StatsTranslationOptions) =>
areHitsSorted ? 'Sorted' : 'Unsorted';
let props = createProps({ translations: { stats: translationFn } });
let props = createProps({
translations: { rootElementText: translationFn },
});

const { getByText, rerender } = render(<Stats {...props} />);

Expand All @@ -163,7 +165,7 @@ describe('Stats', () => {
areHitsSorted: true,
nbSortedHits: 1,
nbHits: 2,
translations: { stats: translationFn },
translations: { rootElementText: translationFn },
});

rerender(<Stats {...props} />);
Expand Down
18 changes: 15 additions & 3 deletions packages/react-instantsearch-hooks-web/src/widgets/Stats.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { useStats } from 'react-instantsearch-hooks';
import { useStats, warn } from 'react-instantsearch-hooks';

import { Stats as StatsUiComponent } from '../ui/Stats';

Expand All @@ -19,14 +19,26 @@ type UiProps = Pick<
>;

export type StatsProps = Omit<StatsUiComponentProps, keyof UiProps> &
UseStatsProps & { translations?: Partial<UiProps['translations']> };
UseStatsProps & {
translations?: Partial<UiProps['translations']> & {
/**
* @deprecated Use `rootElementText` instead.
*/
stats?: Partial<UiProps['translations']>['rootElementText'];
};
};

export function Stats({ translations, ...props }: StatsProps) {
const { nbHits, nbSortedHits, processingTimeMS, areHitsSorted } = useStats(
undefined,
{ $$widgetType: 'ais.stats' }
);

warn(
!translations?.stats,
'The `stats` translation is deprecated. Please use `rootElementText` instead.'
);

const statsTranslation = (options: StatsTranslationOptions): string =>
options.areHitsSorted
? `${options.nbSortedHits!.toLocaleString()} relevant results sorted out of ${options.nbHits.toLocaleString()} found in ${options.processingTimeMS.toLocaleString()}ms`
Expand All @@ -38,7 +50,7 @@ export function Stats({ translations, ...props }: StatsProps) {
processingTimeMS,
areHitsSorted,
translations: {
stats: statsTranslation,
rootElementText: translations?.stats || statsTranslation,
...translations,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('Stats', () => {
<InstantSearchHooksTestWrapper searchClient={client}>
<Stats
translations={{
stats: () => 'Nice stats',
rootElementText: () => 'Nice stats',
}}
/>
</InstantSearchHooksTestWrapper>
Expand All @@ -168,6 +168,45 @@ describe('Stats', () => {
});
});

test('renders with deprecated translations (with a deprecation warning)', async () => {
const client = createMockedSearchClient();
let result: ReturnType<typeof render> | undefined = undefined;

expect(async () => {
sarahdayan marked this conversation as resolved.
Show resolved Hide resolved
result = render(
<InstantSearchHooksTestWrapper searchClient={client}>
<Stats
translations={{
stats: () => 'Nice stats',
}}
/>
</InstantSearchHooksTestWrapper>
);
}).toWarnDev(
'[InstantSearch] The `stats` translation is deprecated. Please use `rootElementText` instead.'
);

await waitFor(() => {
expect(client.search).toHaveBeenCalledTimes(1);
});

const { container } = result!;

await waitFor(() => {
expect(container.querySelector('.ais-Stats')).toMatchInlineSnapshot(`
<div
class="ais-Stats"
>
<span
class="ais-Stats-text"
>
Nice stats
</span>
</div>
`);
});
});

test('forwards custom class names and `div` props to the root element', () => {
const { container } = render(
<InstantSearchHooksTestWrapper>
Expand Down
1 change: 1 addition & 0 deletions packages/react-instantsearch-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export * from './connectors/useStats';
export * from './connectors/useToggleRefinement';
export * from './hooks/useConnector';
export * from './hooks/useInstantSearch';
export * from './lib/warn';