Skip to content

Commit

Permalink
fix(refinement list): prevent error when value has a quote (#6526)
Browse files Browse the repository at this point in the history
* fix(refinementlist): prevent error if value has a quote

The quote ends the query selector, therefore throws an error

[CR-7674]

* chore: fix babel
  • Loading branch information
Haroenv authored Jan 17, 2025
1 parent d3e46fe commit 7721896
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 116 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"postinstall": "yarn install --force --cwd scripts/legacy/algoliasearch@5-dependency-container && yarn install --force --cwd scripts/legacy/algoliasearch-v4-dependency-container && cp -r scripts/legacy/algoliasearch@5-dependency-container/node_modules/* node_modules/ && cp -r scripts/legacy/algoliasearch-v4-dependency-container/node_modules/* node_modules/"
},
"devDependencies": {
"@algolia/client-search": "5.1.1",
"@algolia/client-common": "5.1.1",
"@algolia/client-search": "5.1.1",
"@babel/cli": "7.15.4",
"@babel/core": "7.15.5",
"@babel/plugin-proposal-class-properties": "7.14.5",
Expand Down Expand Up @@ -85,9 +85,9 @@
"@wdio/selenium-standalone-service": "5.16.5",
"@wdio/spec-reporter": "5.16.5",
"@wdio/static-server-service": "5.16.5",
"algoliasearch": "5.1.1",
"algoliasearch-v3": "npm:[email protected]",
"algoliasearch-v5": "npm:[email protected]",
"algoliasearch": "5.1.1",
"babel-eslint": "10.1.0",
"babel-jest": "27.4.6",
"babel-plugin-inline-replace-variables": "1.3.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class RefinementList<TTemplates extends Templates> extends Component<
public componentDidUpdate() {
this.listRef.current
?.querySelector<HTMLInputElement>(
`input[value="${this.lastRefinedValue}"]`
`input[value="${this.lastRefinedValue?.replace('"', '\\"')}"]`
)
?.focus();
this.lastRefinedValue = undefined;
Expand Down
63 changes: 37 additions & 26 deletions tests/common/widgets/refinement-list/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,14 @@ export function createOptionsTests(
});

test('keeps focus on toggled input between re-renders', async () => {
const searchClient = createMockedSearchClient();
const searchClient = createMockedSearchClient(
{},
{
Apple: 100,
'2" inch': 200,
Samsung: 300,
}
);

await setup({
instantSearchOptions: {
Expand All @@ -598,7 +605,7 @@ export function createOptionsTests(
expect(document.activeElement).toEqual(document.body);

const initialTargetItem = document.querySelector(
'.ais-RefinementList-checkbox[value="Samsung"]'
'.ais-RefinementList-checkbox[value="2\\" inch"]'
)!;

await act(async () => {
Expand All @@ -615,9 +622,10 @@ export function createOptionsTests(
});

const updatedTargetItem = document.querySelector(
'.ais-RefinementList-checkbox[value="Samsung"]'
'.ais-RefinementList-checkbox[value="2\\" inch"]'
)!;

expect(updatedTargetItem).not.toBeChecked();
expect(document.activeElement).toEqual(updatedTargetItem);
});

Expand Down Expand Up @@ -1531,36 +1539,39 @@ const FACET_HITS = [
},
];

function createMockedSearchClient(parameters: Record<string, any> = {}) {
function createMockedSearchClient(
parameters: Record<string, any> = {},
values: Record<string, number> = {
'Insignia™': 746,
Samsung: 633,
Metra: 591,
HP: 530,
Apple: 442,
GE: 394,
Sony: 350,
Incipio: 320,
KitchenAid: 318,
Whirlpool: 298,
LG: 291,
Canon: 287,
Frigidaire: 275,
Speck: 216,
OtterBox: 214,
Epson: 204,
'Dynex™': 184,
Dell: 174,
'Hamilton Beach': 173,
Platinum: 155,
}
) {
return createSearchClient({
search: jest.fn((requests) => {
return Promise.resolve(
createMultiSearchResponse(
...requests.map(() =>
createSingleSearchResponse({
facets: {
brand: {
'Insignia™': 746,
Samsung: 633,
Metra: 591,
HP: 530,
Apple: 442,
GE: 394,
Sony: 350,
Incipio: 320,
KitchenAid: 318,
Whirlpool: 298,
LG: 291,
Canon: 287,
Frigidaire: 275,
Speck: 216,
OtterBox: 214,
Epson: 204,
'Dynex™': 184,
Dell: 174,
'Hamilton Beach': 173,
Platinum: 155,
},
brand: values,
},
})
)
Expand Down
Loading

0 comments on commit 7721896

Please sign in to comment.