Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/hooks/fetchWeather/useGetWeather.ts
Original file line number Diff line number Diff line change
@@ -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))
}
Expand Down
6 changes: 3 additions & 3 deletions tests/components/ViewCity.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -52,15 +52,15 @@ 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(<ViewCity city="Oslo" />)

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"),
Expand Down
4 changes: 2 additions & 2 deletions tests/components/listItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<ListItem
city={{
Expand All @@ -27,7 +27,7 @@ describe("ListItem", () => {
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(<ListItem city={{ name: "Trondheim" }} />)

expect(screen.getByText("⛅")).toBeTruthy()
Expand Down
4 changes: 2 additions & 2 deletions tests/components/location-list.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ 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(<LocationList onCitySelect={() => undefined} />)

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: [
Expand Down
Original file line number Diff line number Diff line change
@@ -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`;
17 changes: 17 additions & 0 deletions tests/hooks/fetchWeather/useGetWeather.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})