From ec2201fb6e2d4f4ba6671af27e14bd1dab3f0805 Mon Sep 17 00:00:00 2001 From: KaylaBrady <31781298+KaylaBrady@users.noreply.github.com> Date: Fri, 28 Jul 2023 12:17:11 -0400 Subject: [PATCH] feat(SearchResultsByProperty): Message when no results found for properties --- .../mapPage/searchResultsByProperty.tsx | 71 ++++---- assets/src/components/searchResults.tsx | 2 +- .../mapPage/searchResultsByProperty.test.tsx | 158 +++++++++--------- 3 files changed, 118 insertions(+), 113 deletions(-) diff --git a/assets/src/components/mapPage/searchResultsByProperty.tsx b/assets/src/components/mapPage/searchResultsByProperty.tsx index 30a30a1f8e..016c5fc927 100644 --- a/assets/src/components/mapPage/searchResultsByProperty.tsx +++ b/assets/src/components/mapPage/searchResultsByProperty.tsx @@ -8,7 +8,7 @@ import { } from "../../models/searchQuery" import { Vehicle, Ghost } from "../../realtime" import { setPropertyMatchLimit } from "../../state/searchPageState" -import SearchResults from "../searchResults" +import SearchResults, { NoResults } from "../searchResults" import React from "react" import { useLocationSearchResults } from "../../hooks/useLocationSearchResults" import { Card, CardBody } from "../card" @@ -165,38 +165,49 @@ const SearchResultsByProperty = ({ searchPageState.query.properties ) + const searchHasNoResults = Object.values(resultsByProperty) + .filter( + (result): result is LoadingResult | Ok => + result !== null + ) + .every((result) => isOk(result) && result.ok.matchingVehicles.length === 0) + return (
- {Object.entries(searchPageState.query.properties) - .filter(([, limit]) => limit != null) - .map(([property, limit]) => ({ - property: property as SearchProperty, - limit: limit as number, - })) - .sort( - ({ property: first_property }, { property: second_property }) => - searchPropertyDisplayConfig[first_property].order - - searchPropertyDisplayConfig[second_property].order - ) - .map(({ property, limit }) => - property === "location" ? ( - onShowMore(property, limit)} - /> - ) : ( - onShowMore(property, limit)} - /> + {searchHasNoResults ? ( + + ) : ( + Object.entries(searchPageState.query.properties) + .filter(([, limit]) => limit != null) + .map(([property, limit]) => ({ + property: property as SearchProperty, + limit: limit as number, + })) + .sort( + ({ property: first_property }, { property: second_property }) => + searchPropertyDisplayConfig[first_property].order - + searchPropertyDisplayConfig[second_property].order ) - )} + .map(({ property, limit }) => + property === "location" ? ( + onShowMore(property, limit)} + /> + ) : ( + onShowMore(property, limit)} + /> + ) + ) + )}
) } diff --git a/assets/src/components/searchResults.tsx b/assets/src/components/searchResults.tsx index eebff0b748..1c1b21f014 100644 --- a/assets/src/components/searchResults.tsx +++ b/assets/src/components/searchResults.tsx @@ -125,7 +125,7 @@ const ResultsList = ({ ) -const NoResults = () => { +export const NoResults = () => { const [ { searchPageState: { query }, diff --git a/assets/tests/components/mapPage/searchResultsByProperty.test.tsx b/assets/tests/components/mapPage/searchResultsByProperty.test.tsx index adbfea16ee..478cf6406c 100644 --- a/assets/tests/components/mapPage/searchResultsByProperty.test.tsx +++ b/assets/tests/components/mapPage/searchResultsByProperty.test.tsx @@ -11,6 +11,7 @@ import locationSearchResultFactory from "../../factories/locationSearchResult" import { LocationSearchResult } from "../../../src/models/locationSearchResult" import useSearchResultsByProperty from "../../../src/hooks/useSearchResultsByProperty" import { searchQueryLocationFactory } from "../../factories/searchQuery" +import { searchQueryVehicleFactory } from "../../factories/searchQuery" jest.mock("../../../src/hooks/useSearchResultsByProperty", () => ({ __esModule: true, @@ -22,14 +23,43 @@ jest.mock("../../../src/hooks/useLocationSearchResults", () => ({ useLocationSearchResults: jest.fn(() => null), })) -afterEach(() => { - jest.resetAllMocks() -}) - const runMatch = vehicleFactory.build() const operatorMatch = vehicleFactory.build() const vehicleMatch = vehicleFactory.build() +beforeEach(() => { + ;(useSearchResultsByProperty as jest.Mock).mockReturnValue({ + run: { + ok: { + matchingVehicles: [runMatch], + hasMoreMatches: false, + }, + }, + vehicle: { + ok: { + matchingVehicles: [vehicleMatch], + hasMoreMatches: true, + }, + }, + operator: { + ok: { + matchingVehicles: [operatorMatch], + hasMoreMatches: false, + }, + }, + location: { + ok: { + matchingVehicles: [], + hasMoreMatches: false, + }, + }, + }) +}) + +afterEach(() => { + jest.resetAllMocks() +}) + describe("searchResultsByProperty", () => { test("Includes only sections that have results", () => { ;(useSearchResultsByProperty as jest.Mock).mockReturnValue({ @@ -79,32 +109,6 @@ describe("searchResultsByProperty", () => { }) test("only includes results for the properties included in the query", () => { - ;(useSearchResultsByProperty as jest.Mock).mockReturnValue({ - run: { - ok: { - matchingVehicles: [runMatch], - hasMoreMatches: false, - }, - }, - vehicle: { - ok: { - matchingVehicles: [vehicleMatch], - hasMoreMatches: false, - }, - }, - operator: { - ok: { - matchingVehicles: [operatorMatch], - hasMoreMatches: false, - }, - }, - location: { - ok: { - matchingVehicles: [], - hasMoreMatches: false, - }, - }, - }) ;(useLocationSearchResults as jest.Mock).mockImplementation(() => []) render( @@ -138,32 +142,6 @@ describe("searchResultsByProperty", () => { }) test("Includes the sections in the expected order", () => { - ;(useSearchResultsByProperty as jest.Mock).mockReturnValue({ - run: { - ok: { - matchingVehicles: [runMatch], - hasMoreMatches: false, - }, - }, - vehicle: { - ok: { - matchingVehicles: [vehicleMatch], - hasMoreMatches: false, - }, - }, - operator: { - ok: { - matchingVehicles: [operatorMatch], - hasMoreMatches: false, - }, - }, - location: { - ok: { - matchingVehicles: [], - hasMoreMatches: false, - }, - }, - }) ;(useLocationSearchResults as jest.Mock).mockImplementation(() => []) render( @@ -191,32 +169,6 @@ describe("searchResultsByProperty", () => { }) test("Lists the matching vehicles under the appropriate header", () => { - ;(useSearchResultsByProperty as jest.Mock).mockReturnValue({ - run: { - ok: { - matchingVehicles: [runMatch], - hasMoreMatches: false, - }, - }, - vehicle: { - ok: { - matchingVehicles: [vehicleMatch], - hasMoreMatches: false, - }, - }, - operator: { - ok: { - matchingVehicles: [operatorMatch], - hasMoreMatches: false, - }, - }, - location: { - ok: { - matchingVehicles: [], - hasMoreMatches: false, - }, - }, - }) ;(useLocationSearchResults as jest.Mock).mockImplementation(() => []) render( @@ -417,4 +369,46 @@ describe("searchResultsByProperty", () => { }) ).not.toBeInTheDocument() }) + + test("when there are no results for the given properties, display no results message", () => { + ;(useSearchResultsByProperty as jest.Mock).mockReturnValue({ + run: { + ok: { + matchingVehicles: [], + hasMoreMatches: false, + }, + }, + vehicle: { + ok: { + matchingVehicles: [], + hasMoreMatches: true, + }, + }, + operator: null, + location: { + ok: { + matchingVehicles: [], + hasMoreMatches: false, + }, + }, + }) + + render( + + + + ) + expect(screen.getByText("No Search Results")).toBeInTheDocument() + }) })