From e2f27c18ac948bc1eb8a3b8b3430f1936eba7e8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henry=20Gr=C3=A6sberg?= Date: Thu, 17 Sep 2026 23:49:37 +0200 Subject: [PATCH] add snapshot tests to check altitude sanitizer --- src/hooks/fetchWeather/useGetWeather.ts | 2 +- tests/components/ViewCity.test.tsx | 6 +++--- tests/components/listItem.test.tsx | 4 ++-- tests/components/location-list.test.tsx | 4 ++-- .../__snapshots__/useGetWeather.test.ts.snap | 11 +++++++++++ tests/hooks/fetchWeather/useGetWeather.test.ts | 17 +++++++++++++++++ 6 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 tests/hooks/fetchWeather/__snapshots__/useGetWeather.test.ts.snap create mode 100644 tests/hooks/fetchWeather/useGetWeather.test.ts diff --git a/src/hooks/fetchWeather/useGetWeather.ts b/src/hooks/fetchWeather/useGetWeather.ts index e6cc108..7803d49 100644 --- a/src/hooks/fetchWeather/useGetWeather.ts +++ b/src/hooks/fetchWeather/useGetWeather.ts @@ -1,7 +1,7 @@ import { useQuery, useQueries } from "@tanstack/react-query" import { type City, type WeatherApiResponse } from "./types" -const sanitizeAltitude = (alt?: number) => { +export const sanitizeAltitude = (alt?: number) => { if (alt === undefined) return undefined return Math.min(9000, Math.max(-500, alt)) } diff --git a/tests/components/ViewCity.test.tsx b/tests/components/ViewCity.test.tsx index 8c7e45a..309134d 100644 --- a/tests/components/ViewCity.test.tsx +++ b/tests/components/ViewCity.test.tsx @@ -24,7 +24,7 @@ describe("ViewCity", () => { }) }) - it("renders the current temperature and wind speed", () => { + it("should render the current temperature and wind speed", () => { mockUseGetWeather.mockReturnValue({ isLoading: false, error: null, @@ -52,7 +52,7 @@ describe("ViewCity", () => { expect(mockUseGetWeather).toHaveBeenCalledWith(59.9, 10.7) }) - it("renders a loading message while weather data is loading", () => { + it("should render a loading message while weather data is loading", () => { mockUseGetWeather.mockReturnValue({ isLoading: true, error: null }) render() @@ -60,7 +60,7 @@ describe("ViewCity", () => { expect(screen.getByText("Laster")).toBeTruthy() }) - it("renders an error message when the weather request fails", () => { + it("should render an error message when the weather request fails", () => { mockUseGetWeather.mockReturnValue({ isLoading: false, error: new Error("Request failed"), diff --git a/tests/components/listItem.test.tsx b/tests/components/listItem.test.tsx index c3d6192..0fe4d7f 100644 --- a/tests/components/listItem.test.tsx +++ b/tests/components/listItem.test.tsx @@ -5,7 +5,7 @@ import { describe, expect, it } from "vitest" import ListItem from "../../src/components/listItem" describe("ListItem", () => { - it("renders city weather, forecast temperatures, and a rain icon", () => { + it("should render city weather, forecast temperatures, and a rain icon", () => { render( { expect(screen.getByText("⛅7°")).toBeTruthy() }) - it("shows fallback values when weather data is unavailable", () => { + it("should show fallback values when weather data is unavailable", () => { render() expect(screen.getByText("⛅")).toBeTruthy() diff --git a/tests/components/location-list.test.tsx b/tests/components/location-list.test.tsx index 354f015..74d9a5b 100644 --- a/tests/components/location-list.test.tsx +++ b/tests/components/location-list.test.tsx @@ -31,7 +31,7 @@ describe("LocationList", () => { mockUseGetWeatherForCities.mockReset() }) - it("renders a loading message while city forecasts are loading", () => { + it("should render a loading message while city forecasts are loading", () => { mockUseGetWeatherForCities.mockReturnValue({ isLoading: true, results: [] }) render( undefined} />) @@ -39,7 +39,7 @@ describe("LocationList", () => { expect(screen.getByText("Loading...")).toBeTruthy() }) - it("renders a weather row for every configured city", () => { + it("should render a weather row for every configured city", () => { mockUseGetWeatherForCities.mockReturnValue({ isLoading: false, results: [ diff --git a/tests/hooks/fetchWeather/__snapshots__/useGetWeather.test.ts.snap b/tests/hooks/fetchWeather/__snapshots__/useGetWeather.test.ts.snap new file mode 100644 index 0000000..dc204fb --- /dev/null +++ b/tests/hooks/fetchWeather/__snapshots__/useGetWeather.test.ts.snap @@ -0,0 +1,11 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`should sanitize altitude -500 to -500 1`] = `-500`; + +exports[`should sanitize altitude -1000 to -500 1`] = `-500`; + +exports[`should sanitize altitude 100 to 100 1`] = `100`; + +exports[`should sanitize altitude 9000 to 9000 1`] = `9000`; + +exports[`should sanitize altitude 10000 to 9000 1`] = `9000`; diff --git a/tests/hooks/fetchWeather/useGetWeather.test.ts b/tests/hooks/fetchWeather/useGetWeather.test.ts new file mode 100644 index 0000000..4162919 --- /dev/null +++ b/tests/hooks/fetchWeather/useGetWeather.test.ts @@ -0,0 +1,17 @@ +// @vitest-environment jsdom + +import { expect, it } from "vitest" +import { sanitizeAltitude } from "../../../src/hooks/fetchWeather/useGetWeather" + +it.each([ + { input: 100, expected: 100 }, + { input: -500, expected: -500 }, + { input: 9000, expected: 9000 }, + { input: -1000, expected: -500 }, + { input: 10000, expected: 9000 }, +])("should sanitize altitude $input to $expected", ({ input, expected }) => { + const result = sanitizeAltitude(input) + + expect(result).toEqual(expected) + expect(result).toMatchSnapshot() +})