Return

All tests / src/hooks useNews.ts

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

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 454x 4x 4x 4x     4x   4x             6x 6x 6x 6x     6x                             6x             6x  
import useSWR from 'swr';
import React from 'react';
import createFetcher from '@/helpers/createFetcher';
import { newsSchema } from '../types/schemas';
import type { NewsData } from '../types/api';
 
const fetchNews = createFetcher(newsSchema, '/api/news');
 
const useNews = (
    city: string
): {
    data: NewsData | undefined;
    error: Error | undefined;
    loading: boolean;
} => {
    const memoUrl = React.useMemo(() => {
        const url = new URL(`${process.env.NEXT_PUBLIC_DOMAIN}/api/news`);
        url.searchParams.set('city', city);
        return url.toString();
    }, [city]);
 
    const { data, error, isLoading } = useSWR<NewsData, Error>(memoUrl, fetchNews, {
        keepPreviousData: true,
        dedupingInterval: 5000,
        fallbackData: {
            news: [
                {
                    link: '',
                    title: '',
                    published: '',
                    description: '',
                },
            ],
        },
    });
 
    return {
        data,
        error,
        loading: isLoading,
    };
};
 
export default useNews;