Return

All tests / hooks useWeather.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 2/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 494x 4x 4x                       4x                     4x 5x 4x 4x 4x     5x             5x             5x    
import useSWR from 'swr';
import React from 'react';
import { fetcher } from '@/helpers';
 
interface WeatherData {
    city: string;
    name: string;
    precipitation: string;
    humidity: string;
    windSpeed: string;
    grades: string;
    imageUrl?: string;
}
 
const initialValues: WeatherData[] = [
    {
        city: 'moraƱa',
        name: 'Partly cloudy',
        precipitation: '0%',
        humidity: '50%',
        windSpeed: '10 km/h',
        grades: '15',
        imageUrl: '',
    },
];
const useWeather = (cities: string[]) => {
    const url = React.useMemo(() => {
        const url = new URL(`${process.env.NEXT_PUBLIC_DOMAIN}/api/weather`);
        url.searchParams.append('cities', cities.join(','));
        return url;
    }, [cities]);
 
    const { data, error, isLoading } = useSWR(url.toString(), fetcher, {
        dedupingInterval: 5000,
        keepPreviousData: true,
        fallback: initialValues,
        fallbackData: initialValues,
    });
 
    return {
        data: data as WeatherData[] | undefined,
        error,
        loading: isLoading,
    };
};
 
export default useWeather;
export type { WeatherData };