CLI for Oblique Strategies.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.9 KiB

  1. import {
  2. describe,
  3. expect,
  4. vi,
  5. it,
  6. } from 'vitest';
  7. import fetchPonyfill from 'fetch-ponyfill';
  8. import { read, prefix } from '../https';
  9. vi.mock('fetch-ponyfill');
  10. describe('https', () => {
  11. describe('read', () => {
  12. it('calls fetch', async () => {
  13. const fetch = vi.fn().mockResolvedValueOnce({
  14. ok: true,
  15. text: () => Promise.resolve('foo'),
  16. });
  17. vi.mocked(fetchPonyfill).mockReturnValueOnce({
  18. fetch,
  19. } as unknown as ReturnType<typeof fetchPonyfill>);
  20. await read('example.com');
  21. expect(fetch).toBeCalled();
  22. });
  23. it('considers response as text data', async () => {
  24. const text = vi.fn().mockResolvedValueOnce('foo');
  25. const fetch = vi.fn().mockResolvedValueOnce({
  26. ok: true,
  27. text,
  28. });
  29. vi.mocked(fetchPonyfill).mockReturnValueOnce({
  30. fetch,
  31. } as unknown as ReturnType<typeof fetchPonyfill>);
  32. await read('example.com');
  33. expect(text).toBeCalled();
  34. });
  35. it('returns text data when request from fetch is successful', async () => {
  36. const text = vi.fn().mockResolvedValueOnce('foo');
  37. const fetch = vi.fn().mockResolvedValueOnce({
  38. ok: true,
  39. text,
  40. });
  41. vi.mocked(fetchPonyfill).mockReturnValueOnce({
  42. fetch,
  43. } as unknown as ReturnType<typeof fetchPonyfill>);
  44. const cards = await read('example.com');
  45. expect(cards).toEqual(expect.arrayContaining([expect.any(String)]));
  46. });
  47. it('throws when request from fetch is unsuccessful', async () => {
  48. const fetch = vi.fn().mockResolvedValueOnce({
  49. ok: false,
  50. text: () => Promise.resolve('foo'),
  51. });
  52. vi.mocked(fetchPonyfill).mockReturnValueOnce({
  53. fetch,
  54. } as unknown as ReturnType<typeof fetchPonyfill>);
  55. await expect(() => read('example.com')).rejects.toThrowError();
  56. });
  57. });
  58. it('exports a prefix', () => {
  59. expect(prefix).toBeTypeOf('string');
  60. });
  61. });