From 2b6fdc1d893c0ce89dc08c791afe565cad6e143a Mon Sep 17 00:00:00 2001 From: Xavier Desoindre Date: Mon, 16 Dec 2024 18:27:24 +0100 Subject: [PATCH] fix: same comparison in transport integration (#761) * fix: same comparison in transport integration * review --- .../overScreens/TransportComparison.tsx | 23 +++++++++++++++++-- teste/distance.ts | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/components/shareable/overScreens/TransportComparison.tsx b/src/components/shareable/overScreens/TransportComparison.tsx index d5f5499df..c7572ce4b 100644 --- a/src/components/shareable/overScreens/TransportComparison.tsx +++ b/src/components/shareable/overScreens/TransportComparison.tsx @@ -1,5 +1,6 @@ import { useTranslations } from 'next-intl' import { Dispatch, SetStateAction, useEffect, useMemo } from 'react' +import { ComputedEquivalent } from 'types/equivalent' import { computedEquivalents } from 'data/categories/computedEquivalents' import { deplacements } from 'data/categories/deplacement' import { getEquivalentWithCarpool } from 'utils/carpool' @@ -9,6 +10,24 @@ import styles from './TransportListParam.module.css' const equivalents = computedEquivalents('transport', deplacements).flatMap(getEquivalentWithCarpool) +const findNextEquivalent = (equivalents: ComputedEquivalent[], comparison: string) => { + const [slug] = comparison.split('+') + let equivalent = equivalents.find((equivalent) => { + const [equivalentSlug] = equivalent.slug.split('+') + return slug !== equivalentSlug + }) + if (equivalent) { + return equivalent.slug + } + + equivalent = equivalents.find((equivalent) => slug !== equivalent.slug) + if (equivalent) { + return equivalent.slug + } + + return equivalents[0].slug +} + const TransportComparison = ({ comparison, setComparison, @@ -47,9 +66,9 @@ const TransportComparison = ({ if (!equivalent1 && !equivalent2) { setComparison([filteredEquivalents[0].slug, filteredEquivalents[1].slug]) } else if (!equivalent1) { - setComparison([filteredEquivalents[0].slug, comparison[1]]) + setComparison([findNextEquivalent(filteredEquivalents, comparison[1]), comparison[1]]) } else if (!equivalent2) { - setComparison([comparison[0], filteredEquivalents[1].slug]) + setComparison([comparison[0], findNextEquivalent(filteredEquivalents, comparison[0])]) } }, [equivalent1, equivalent2]) diff --git a/teste/distance.ts b/teste/distance.ts index 076e82c9b..9b2e049ab 100644 --- a/teste/distance.ts +++ b/teste/distance.ts @@ -94,7 +94,7 @@ export const distanceTest = async (page: Page | FrameLocator, prod?: boolean) => await page.locator('span').filter({ hasText: /^TER$/ }).nth(2).click() await page.getByLabel('Intégrer').getByText('Covoiturage thermique', { exact: true }).click() await expect(page.getByTestId('clipboard-box')).toHaveText( - `` + `` ) await page.getByTestId('text-select-comparison-1').selectOption('velo') await page.getByTestId('text-select-comparison-2').selectOption('moto')