|
1 | | -import { apiClient } from './api'; |
2 | | - |
3 | | -// Mock the StoryblokClient to prevent actual HTTP requests |
4 | | -vi.mock('storyblok-js-client', () => { |
5 | | - const StoryblokClientMock = vi.fn().mockImplementation((config) => { |
6 | | - return { |
7 | | - config, |
8 | | - }; |
9 | | - }); |
| 1 | +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { createManagementClient, type StoryblokManagementClientOptions } from './api'; |
| 3 | +import { FetchError } from './utils/fetch'; |
| 4 | +import { http, HttpResponse } from 'msw'; |
| 5 | +import { setupServer } from 'msw/node'; |
10 | 6 |
|
11 | | - return { |
12 | | - default: StoryblokClientMock, |
13 | | - __esModule: true, // Important for ESM modules |
14 | | - }; |
15 | | -}); |
| 7 | +// Setup MSW server |
| 8 | +const server = setupServer(); |
16 | 9 |
|
17 | | -// Mocking the session module |
18 | | -vi.mock('./session', () => { |
19 | | - let _cache: Record<string, any> | null = null; |
20 | | - const session = () => { |
21 | | - if (!_cache) { |
22 | | - _cache = { |
23 | | - state: { |
24 | | - isLoggedIn: true, |
25 | | - password: 'test-token', |
26 | | - region: 'eu', |
27 | | - }, |
28 | | - updateSession: vi.fn(), |
29 | | - persistCredentials: vi.fn(), |
30 | | - initializeSession: vi.fn(), |
31 | | - }; |
32 | | - } |
33 | | - return _cache; |
34 | | - }; |
| 10 | +// Mock response data |
| 11 | +const mockResponse = { |
| 12 | + data: { id: 1, name: 'Test' }, |
| 13 | + meta: { status: 200 }, |
| 14 | +}; |
35 | 15 |
|
36 | | - return { |
37 | | - session, |
| 16 | +describe('storyblok Management Client', () => { |
| 17 | + const mockOptions: StoryblokManagementClientOptions = { |
| 18 | + accessToken: 'test-token', |
| 19 | + region: 'eu', |
38 | 20 | }; |
39 | | -}); |
40 | 21 |
|
41 | | -describe('storyblok API Client', () => { |
42 | | - beforeEach(async () => { |
43 | | - // Reset the module state before each test to ensure test isolation |
44 | | - vi.resetModules(); |
45 | | - vi.clearAllMocks(); |
| 22 | + // Start server before all tests |
| 23 | + beforeAll(() => server.listen()); |
| 24 | + // Reset handlers and client instance after each test |
| 25 | + afterEach(() => { |
| 26 | + server.resetHandlers(); |
46 | 27 | }); |
| 28 | + // Close server after all tests |
| 29 | + afterAll(() => server.close()); |
47 | 30 |
|
48 | | - it('should have a default region of "eu"', () => { |
49 | | - const { region } = apiClient(); |
50 | | - expect(region).toBe('eu'); |
| 31 | + describe('initialization', () => { |
| 32 | + beforeEach(() => { |
| 33 | + // Note: This is a workaround to reset the client instance for the tests |
| 34 | + createManagementClient({ |
| 35 | + accessToken: 'test-token', |
| 36 | + region: 'eu', |
| 37 | + }).reset(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should create a new client instance with valid options', () => { |
| 41 | + const client = createManagementClient(mockOptions); |
| 42 | + expect(client.getClientName()).toBe('management-client'); |
| 43 | + expect(client.endpoint).toBeDefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should throw error when trying to get instance without initialization', () => { |
| 47 | + expect(() => createManagementClient()).toThrow('MAPI Client requires an access token for initialization'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should maintain singleton instance', () => { |
| 51 | + const client1 = createManagementClient(mockOptions); |
| 52 | + const client2 = createManagementClient(); |
| 53 | + expect(client1).toBe(client2); |
| 54 | + }); |
51 | 55 | }); |
52 | 56 |
|
53 | | - it('should return the same client instance when called multiple times without changes', () => { |
54 | | - const api1 = apiClient(); |
55 | | - const client1 = api1.client; |
| 57 | + describe('gET requests', () => { |
| 58 | + it('should make successful GET request', async () => { |
| 59 | + server.use( |
| 60 | + http.get('*/test', () => { |
| 61 | + return HttpResponse.json(mockResponse); |
| 62 | + }), |
| 63 | + ); |
| 64 | + |
| 65 | + const client = createManagementClient(mockOptions); |
| 66 | + const result = await client.get('/test'); |
56 | 67 |
|
57 | | - const api2 = apiClient(); |
58 | | - const client2 = api2.client; |
| 68 | + expect(result).toEqual(mockResponse); |
| 69 | + }); |
59 | 70 |
|
60 | | - expect(client1).toBe(client2); |
| 71 | + it('should handle non-JSON responses', async () => { |
| 72 | + server.use( |
| 73 | + http.get('*/test', () => { |
| 74 | + return new HttpResponse('Not JSON', { |
| 75 | + headers: { |
| 76 | + 'Content-Type': 'text/plain', |
| 77 | + }, |
| 78 | + }); |
| 79 | + }), |
| 80 | + ); |
| 81 | + |
| 82 | + const client = createManagementClient(mockOptions); |
| 83 | + await expect(client.get('/test')).rejects.toThrow(FetchError); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should handle HTTP errors', async () => { |
| 87 | + server.use( |
| 88 | + http.get('*/test', () => { |
| 89 | + return new HttpResponse(null, { |
| 90 | + status: 404, |
| 91 | + statusText: 'Not Found', |
| 92 | + }); |
| 93 | + }), |
| 94 | + ); |
| 95 | + |
| 96 | + const client = createManagementClient(mockOptions); |
| 97 | + await expect(client.get('/test')).rejects.toThrow(FetchError); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should handle network errors', async () => { |
| 101 | + server.use( |
| 102 | + http.get('*/test', () => { |
| 103 | + return HttpResponse.error(); |
| 104 | + }), |
| 105 | + ); |
| 106 | + |
| 107 | + const client = createManagementClient(mockOptions); |
| 108 | + await expect(client.get('/test')).rejects.toThrow(FetchError); |
| 109 | + }); |
61 | 110 | }); |
62 | 111 |
|
63 | | - it('should set the region on the client', () => { |
64 | | - const { setRegion } = apiClient(); |
65 | | - setRegion('us'); |
66 | | - const { region } = apiClient(); |
67 | | - expect(region).toBe('us'); |
| 112 | + describe('pOST requests', () => { |
| 113 | + it('should make successful POST request', async () => { |
| 114 | + server.use( |
| 115 | + http.post('*/test', async ({ request }) => { |
| 116 | + const body = await request.json(); |
| 117 | + return HttpResponse.json({ |
| 118 | + ...mockResponse, |
| 119 | + requestBody: body, |
| 120 | + }); |
| 121 | + }), |
| 122 | + ); |
| 123 | + |
| 124 | + const client = createManagementClient(mockOptions); |
| 125 | + const body = { name: 'Test' }; |
| 126 | + const result = await client.post('/test', body); |
| 127 | + |
| 128 | + expect(result).toEqual({ |
| 129 | + ...mockResponse, |
| 130 | + requestBody: body, |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should handle HTTP errors in POST requests', async () => { |
| 135 | + server.use( |
| 136 | + http.post('*/test', () => { |
| 137 | + return new HttpResponse(null, { |
| 138 | + status: 400, |
| 139 | + statusText: 'Bad Request', |
| 140 | + }); |
| 141 | + }), |
| 142 | + ); |
| 143 | + |
| 144 | + const client = createManagementClient(mockOptions); |
| 145 | + await expect(client.post('/test', {})).rejects.toThrow('API request failed'); |
| 146 | + }); |
68 | 147 | }); |
69 | 148 | }); |
0 commit comments