Skip to content

Commit

Permalink
fix(Trouver sa CC): mettre le focus sur les résultats quand on fait u…
Browse files Browse the repository at this point in the history
…ne recherche par entreprises (#6425)
  • Loading branch information
carolineBda authored Jan 21, 2025
1 parent 94b6180 commit 58b67a2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const EnterpriseAgreementSearchInput = ({
);
const [enterprises, setEnterprises] = useState<Enterprise[]>();
const [error, setError] = useState("");
const resultRef = useRef<HTMLDivElement>(null);
const resultRef = useRef<HTMLHeadingElement>(null);

const getStateMessage = () => {
switch (searchState) {
Expand Down Expand Up @@ -106,7 +106,6 @@ export const EnterpriseAgreementSearchInput = ({
search.length > 0 && !result.length ? "notFoundSearch" : "noSearch"
);
setEnterprises(result);
resultRef.current?.focus();
} catch (e) {
setSearchState("errorSearch");
setEnterprises(undefined);
Expand All @@ -120,6 +119,10 @@ export const EnterpriseAgreementSearchInput = ({
onSubmit();
}
}, [defaultSearch]);

useEffect(() => {
resultRef.current?.focus();
}, [enterprises]);
return (
<>
<h2 className={fr.cx("fr-h4", "fr-my-2w")}>Précisez votre entreprise</h2>
Expand Down Expand Up @@ -195,8 +198,13 @@ export const EnterpriseAgreementSearchInput = ({

<div>
<div className={fr.cx("fr-mt-2w")}>
{!!enterprises?.length && !loading && (
<h2 className={fr.cx("fr-h5")} tabIndex={-1} ref={resultRef}>
{enterprises && enterprises.length > 0 && !loading && (
<h2
className={fr.cx("fr-h5")}
tabIndex={-1}
ref={resultRef}
data-testid={"result-title"}
>
{enterprises.length}
{enterprises.length > 1
? " entreprises trouvées"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, RenderResult } from "@testing-library/react";
import { render } from "@testing-library/react";
import { UserAction } from "../../../../common";
import React from "react";
import { EnterpriseAgreementSearch } from "../EnterpriseAgreementSearch";
Expand Down Expand Up @@ -26,7 +26,6 @@ jest.mock("next/navigation", () => ({

describe("Trouver sa CC - recherche par nom d'entreprise CC", () => {
describe("Test de l'autocomplete", () => {
let rendering: RenderResult;
let userAction: UserAction;
const enterprise = {
activitePrincipale:
Expand Down Expand Up @@ -60,7 +59,7 @@ describe("Trouver sa CC - recherche par nom d'entreprise CC", () => {
jest.resetAllMocks();
});
it("Vérifier l'affichage de la recherche", async () => {
rendering = render(<EnterpriseAgreementSearch />);
render(<EnterpriseAgreementSearch />);
(searchEnterprises as jest.Mock).mockImplementation(() =>
Promise.resolve([enterprise])
);
Expand All @@ -77,6 +76,9 @@ describe("Trouver sa CC - recherche par nom d'entreprise CC", () => {
category: "enterprise_search",
name: '{"query":"carrefour"}',
});
expect(
ui.enterpriseAgreementSearch.resultTitle.get().textContent
).toEqual("1 entreprise trouvée");
expect(
ui.enterpriseAgreementSearch.resultLines.carrefour.title.query()
).toBeInTheDocument();
Expand Down Expand Up @@ -124,7 +126,7 @@ describe("Trouver sa CC - recherche par nom d'entreprise CC", () => {
});

it("Vérifier l'affichage de l'erreur si aucun résultat", async () => {
rendering = render(<EnterpriseAgreementSearch />);
render(<EnterpriseAgreementSearch />);
(searchEnterprises as jest.Mock).mockImplementation(() =>
Promise.resolve([])
);
Expand All @@ -144,7 +146,7 @@ describe("Trouver sa CC - recherche par nom d'entreprise CC", () => {
});

it("Vérifier l'affichage de la recherche en mode widget", async () => {
rendering = render(<EnterpriseAgreementSearch widgetMode />);
render(<EnterpriseAgreementSearch widgetMode />);
(searchEnterprises as jest.Mock).mockImplementation(() =>
Promise.resolve([
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { byText, byLabelText, byRole } from "testing-library-selector";
import {
byLabelText,
byRole,
byTestId,
byText,
} from "testing-library-selector";

export const ui = {
enterpriseAgreementSearch: {
Expand All @@ -12,6 +17,7 @@ export const ui = {
name: "Particuliers employeurs et emploi à domicile",
}),
},
resultTitle: byTestId("result-title"),
resultLines: {
carrefour: {
title: byText("CARREFOUR PROXIMITE FRANCE (SHOPI-8 A HUIT)"),
Expand Down

0 comments on commit 58b67a2

Please sign in to comment.