import { describe, expect, vi, it, } from 'vitest'; import fetchPonyfill from 'fetch-ponyfill'; import { read, prefix } from '../https'; vi.mock('fetch-ponyfill'); describe('https', () => { describe('read', () => { it('calls fetch', async () => { const fetch = vi.fn().mockResolvedValueOnce({ ok: true, text: () => Promise.resolve('foo'), }); vi.mocked(fetchPonyfill).mockReturnValueOnce({ fetch, } as unknown as ReturnType); await read('example.com'); expect(fetch).toBeCalled(); }); it('considers response as text data', async () => { const text = vi.fn().mockResolvedValueOnce('foo'); const fetch = vi.fn().mockResolvedValueOnce({ ok: true, text, }); vi.mocked(fetchPonyfill).mockReturnValueOnce({ fetch, } as unknown as ReturnType); await read('example.com'); expect(text).toBeCalled(); }); it('returns text data when request from fetch is successful', async () => { const text = vi.fn().mockResolvedValueOnce('foo'); const fetch = vi.fn().mockResolvedValueOnce({ ok: true, text, }); vi.mocked(fetchPonyfill).mockReturnValueOnce({ fetch, } as unknown as ReturnType); const cards = await read('example.com'); expect(cards).toEqual(expect.arrayContaining([expect.any(String)])); }); it('throws when request from fetch is unsuccessful', async () => { const fetch = vi.fn().mockResolvedValueOnce({ ok: false, text: () => Promise.resolve('foo'), }); vi.mocked(fetchPonyfill).mockReturnValueOnce({ fetch, } as unknown as ReturnType); await expect(() => read('example.com')).rejects.toThrowError(); }); }); it('exports a prefix', () => { expect(prefix).toBeTypeOf('string'); }); });