|
1 | | -import { describe, it, expect, vi } from 'vitest'; |
2 | | -import { fetcher } from '../../src/api/fetcher'; |
| 1 | +import { describe, it, expect, vi } from 'vitest' |
| 2 | +import { fetcher } from '../../src/api/fetcher' |
3 | 3 |
|
4 | | -const DUMMY_URL = 'https://api.example.com/test'; |
5 | | -const DUMMY_TOKEN = 'dummy-token'; |
| 4 | +const DUMMY_URL = 'https://api.example.com/test' |
| 5 | +const DUMMY_TOKEN = 'dummy-token' |
6 | 6 |
|
7 | 7 | // Helper to mock fetch |
8 | | -function mockFetch(response: Partial<Response> & { json?: () => any }, ok = true) { |
| 8 | +function mockFetch( |
| 9 | + response: Partial<Response> & { json?: () => any }, |
| 10 | + ok = true |
| 11 | +) { |
9 | 12 | global.fetch = vi.fn().mockResolvedValue({ |
10 | 13 | ok, |
11 | 14 | json: response.json || (() => Promise.resolve(response)), |
12 | 15 | ...response, |
13 | | - }) as any; |
| 16 | + }) as any |
14 | 17 | } |
15 | 18 |
|
16 | 19 | describe('fetcher', () => { |
17 | 20 | afterEach(() => { |
18 | | - vi.resetAllMocks(); |
19 | | - }); |
| 21 | + vi.resetAllMocks() |
| 22 | + }) |
20 | 23 |
|
21 | 24 | it('throws if token is missing', async () => { |
22 | | - await expect(fetcher(DUMMY_URL, '')) |
23 | | - .rejects.toThrow(/token/i); |
24 | | - }); |
| 25 | + await expect(fetcher(DUMMY_URL, '')).rejects.toThrow(/token/i) |
| 26 | + }) |
25 | 27 |
|
26 | 28 | it('returns JSON on success', async () => { |
27 | | - const data = { foo: 'bar' }; |
28 | | - mockFetch({ json: () => Promise.resolve(data) }); |
29 | | - const result = await fetcher(DUMMY_URL, DUMMY_TOKEN); |
30 | | - expect(result).toEqual(data); |
31 | | - expect(global.fetch).toHaveBeenCalledWith(DUMMY_URL, expect.objectContaining({ |
32 | | - headers: expect.objectContaining({ |
33 | | - 'X-FIGMA-TOKEN': DUMMY_TOKEN, |
34 | | - 'Content-Type': 'application/json', |
35 | | - }), |
36 | | - })); |
37 | | - }); |
| 29 | + const data = { foo: 'bar' } |
| 30 | + mockFetch({ json: () => Promise.resolve(data) }) |
| 31 | + const result = await fetcher(DUMMY_URL, DUMMY_TOKEN) |
| 32 | + expect(result).toEqual(data) |
| 33 | + expect(global.fetch).toHaveBeenCalledWith( |
| 34 | + DUMMY_URL, |
| 35 | + expect.objectContaining({ |
| 36 | + headers: expect.objectContaining({ |
| 37 | + 'X-FIGMA-TOKEN': DUMMY_TOKEN, |
| 38 | + 'Content-Type': 'application/json', |
| 39 | + }), |
| 40 | + }) |
| 41 | + ) |
| 42 | + }) |
38 | 43 |
|
39 | 44 | it('throws with error message if response is not ok and error message exists', async () => { |
40 | | - mockFetch({ json: () => Promise.resolve({ message: 'fail!' }) }, false); |
41 | | - await expect(fetcher(DUMMY_URL, DUMMY_TOKEN)).rejects.toThrow('fail!'); |
42 | | - }); |
| 45 | + mockFetch({ json: () => Promise.resolve({ message: 'fail!' }) }, false) |
| 46 | + await expect(fetcher(DUMMY_URL, DUMMY_TOKEN)).rejects.toThrow('fail!') |
| 47 | + }) |
43 | 48 |
|
44 | 49 | it('throws with fallback error if response is not ok and no message', async () => { |
45 | | - mockFetch({ json: () => Promise.resolve({}) }, false); |
46 | | - await expect(fetcher(DUMMY_URL, DUMMY_TOKEN)).rejects.toThrow(/fetch/i); |
47 | | - }); |
48 | | -}); |
| 50 | + mockFetch({ json: () => Promise.resolve({}) }, false) |
| 51 | + await expect(fetcher(DUMMY_URL, DUMMY_TOKEN)).rejects.toThrow(/fetch/i) |
| 52 | + }) |
| 53 | + |
| 54 | + it('throws with fallback error if response is not ok and JSON parsing fails', async () => { |
| 55 | + // Mock a response that fails JSON parsing |
| 56 | + mockFetch({ json: () => Promise.reject(new Error('Invalid JSON')) }, false) |
| 57 | + await expect(fetcher(DUMMY_URL, DUMMY_TOKEN)).rejects.toThrow(/fetch/i) |
| 58 | + }) |
| 59 | +}) |
0 commit comments