weather_app/tests/lib/api/weather-codes.test.js
2026-07-24 01:53:36 +00:00

120 lines
3.6 KiB
JavaScript

import { describe, it, expect } from 'vitest'
import { getWeatherInfo, getWeatherSeverity, isSevereWeather } from '../../../src/lib/api/weather-codes.js'
describe('weather-codes', () => {
describe('getWeatherInfo', () => {
it('returns correct info for clear sky (0)', () => {
const info = getWeatherInfo(0)
expect(info.icon).toBe('☀️')
expect(info.label).toBe('Clear')
expect(info.desc).toBe('Clear sky')
})
it('returns correct info for partly cloudy (2)', () => {
const info = getWeatherInfo(2)
expect(info.icon).toBe('⛅')
expect(info.label).toBe('Partly Cloudy')
})
it('returns correct info for thunderstorm (95)', () => {
const info = getWeatherInfo(95)
expect(info.icon).toBe('⛈️')
expect(info.label).toBe('Thunderstorm')
})
it('returns fallback for unknown code', () => {
const info = getWeatherInfo(999)
expect(info.icon).toBe('❓')
expect(info.label).toBe('Unknown')
})
it('handles fog codes', () => {
expect(getWeatherInfo(45).icon).toBe('🌫️')
expect(getWeatherInfo(48).icon).toBe('🌫️')
})
it('handles drizzle codes', () => {
expect(getWeatherInfo(51).icon).toBe('🌦️')
expect(getWeatherInfo(53).icon).toBe('🌦️')
expect(getWeatherInfo(55).icon).toBe('🌧️')
})
it('handles rain codes', () => {
expect(getWeatherInfo(61).icon).toBe('🌧️')
expect(getWeatherInfo(63).icon).toBe('🌧️')
expect(getWeatherInfo(65).icon).toBe('🌧️')
})
it('handles snow codes', () => {
expect(getWeatherInfo(71).icon).toBe('🌨️')
expect(getWeatherInfo(73).icon).toBe('🌨️')
expect(getWeatherInfo(75).icon).toBe('❄️')
})
it('handles showers', () => {
expect(getWeatherInfo(80).icon).toBe('🌧️')
expect(getWeatherInfo(81).icon).toBe('🌧️')
expect(getWeatherInfo(82).icon).toBe('⛈️')
})
})
describe('getWeatherSeverity', () => {
it('returns clear for code 0', () => {
expect(getWeatherSeverity(0)).toBe('clear')
})
it('returns cloudy for codes 1-3', () => {
expect(getWeatherSeverity(1)).toBe('cloudy')
expect(getWeatherSeverity(3)).toBe('cloudy')
})
it('returns fog for codes 45-48', () => {
expect(getWeatherSeverity(45)).toBe('fog')
expect(getWeatherSeverity(48)).toBe('fog')
})
it('returns rain for codes 51-67', () => {
expect(getWeatherSeverity(51)).toBe('rain')
expect(getWeatherSeverity(65)).toBe('rain')
})
it('returns snow for codes 71-86', () => {
expect(getWeatherSeverity(71)).toBe('snow')
expect(getWeatherSeverity(86)).toBe('snow')
})
it('returns storm for codes 95-99', () => {
expect(getWeatherSeverity(95)).toBe('storm')
expect(getWeatherSeverity(99)).toBe('storm')
})
})
describe('isSevereWeather', () => {
it('identifies heavy rain as severe', () => {
expect(isSevereWeather(65)).toBe(true)
})
it('identifies heavy snow as severe', () => {
expect(isSevereWeather(75)).toBe(true)
})
it('identifies violent rain showers as severe', () => {
expect(isSevereWeather(82)).toBe(true)
})
it('identifies thunderstorms as severe', () => {
expect(isSevereWeather(95)).toBe(true)
expect(isSevereWeather(96)).toBe(true)
expect(isSevereWeather(99)).toBe(true)
})
it('does not consider clear sky severe', () => {
expect(isSevereWeather(0)).toBe(false)
})
it('does not consider light drizzle severe', () => {
expect(isSevereWeather(51)).toBe(false)
})
})
})