| 
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 | +  })  | 
 | 43 | + | 
 | 44 | +  it('uses the fallback json method when one is not provided', async () => {  | 
 | 45 | +    // This object is compatible with `Partial<Response>`  | 
 | 46 | +    const responseData = { status: 200, statusText: 'OK' }  | 
 | 47 | +    // Call mockFetch without a `json` property to test the fallback path  | 
 | 48 | +    mockFetch(responseData)  | 
 | 49 | +    const result = await fetcher(DUMMY_URL, DUMMY_TOKEN)  | 
 | 50 | +    // The fallback should resolve with the entire response object  | 
 | 51 | +    expect(result).toEqual(responseData)  | 
 | 52 | +  })  | 
38 | 53 | 
 
  | 
39 | 54 |   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 |  | -  });  | 
 | 55 | +    mockFetch({ json: () => Promise.resolve({ message: 'fail!' }) }, false)  | 
 | 56 | +    await expect(fetcher(DUMMY_URL, DUMMY_TOKEN)).rejects.toThrow('fail!')  | 
 | 57 | +  })  | 
43 | 58 | 
 
  | 
44 | 59 |   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 |  | -});  | 
 | 60 | +    mockFetch({ json: () => Promise.resolve({}) }, false)  | 
 | 61 | +    await expect(fetcher(DUMMY_URL, DUMMY_TOKEN)).rejects.toThrow(/fetch/i)  | 
 | 62 | +  })  | 
 | 63 | + | 
 | 64 | +  it('throws with fallback error if response is not ok and JSON parsing fails', async () => {  | 
 | 65 | +    // Mock a response that fails JSON parsing  | 
 | 66 | +    mockFetch({ json: () => Promise.reject(new Error('Invalid JSON')) }, false)  | 
 | 67 | +    await expect(fetcher(DUMMY_URL, DUMMY_TOKEN)).rejects.toThrow(/fetch/i)  | 
 | 68 | +  })  | 
 | 69 | +})  | 
0 commit comments