|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { MOCK_SPEAKERS } from '@/utils/speakersMock'; |
| 3 | +import type { SpeakerType } from '@/types/speaker'; |
| 4 | + |
| 5 | +const createFetchResponse = (overrides: Partial<Response> = {}): Response => |
| 6 | + ({ |
| 7 | + ok: true, |
| 8 | + status: 200, |
| 9 | + statusText: 'OK', |
| 10 | + json: () => Promise.resolve([]), |
| 11 | + text: () => Promise.resolve(''), |
| 12 | + ...overrides, |
| 13 | + }) as Response; |
| 14 | + |
| 15 | +const mockEnv = (token: string): { env: Record<string, string> } => ({ |
| 16 | + env: { |
| 17 | + SPEAKERS_API_URL: 'https://api.example.com/speakers', |
| 18 | + SPEAKERS_API_TOKEN: token, |
| 19 | + }, |
| 20 | +}); |
| 21 | + |
| 22 | +describe('getSpeakers', () => { |
| 23 | + beforeEach(() => { |
| 24 | + vi.resetModules(); |
| 25 | + vi.stubEnv('NODE_ENV', 'development'); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + vi.unstubAllEnvs(); |
| 30 | + vi.restoreAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns parsed API data on success', async () => { |
| 34 | + const apiSpeaker: SpeakerType = { |
| 35 | + id: 1, |
| 36 | + name: 'John', |
| 37 | + surname: 'Doe', |
| 38 | + slug: 'john-doe', |
| 39 | + image: 'https://example.com/john.jpg', |
| 40 | + events_count: 5, |
| 41 | + url: 'https://example.com', |
| 42 | + }; |
| 43 | + |
| 44 | + global.fetch = vi.fn(() => |
| 45 | + Promise.resolve( |
| 46 | + createFetchResponse({ |
| 47 | + json: () => Promise.resolve([apiSpeaker]), |
| 48 | + }), |
| 49 | + ), |
| 50 | + ); |
| 51 | + |
| 52 | + vi.doMock('@/env', () => mockEnv('real-token')); |
| 53 | + |
| 54 | + const { getSpeakers } = await import('@/utils/getSpeakers'); |
| 55 | + const result = await getSpeakers(); |
| 56 | + |
| 57 | + expect(result).toHaveLength(1); |
| 58 | + expect(result[0]?.name).toBe('John'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns mock speakers when token is placeholder in development', async () => { |
| 62 | + vi.doMock('@/env', () => mockEnv('<your_token>')); |
| 63 | + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 64 | + |
| 65 | + const { getSpeakers } = await import('@/utils/getSpeakers'); |
| 66 | + const result = await getSpeakers(); |
| 67 | + |
| 68 | + expect(result).toEqual(MOCK_SPEAKERS); |
| 69 | + expect(warnSpy).toHaveBeenCalledWith( |
| 70 | + expect.stringContaining('returning mock data for development'), |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns empty array when token is placeholder in production', async () => { |
| 75 | + vi.unstubAllEnvs(); |
| 76 | + vi.stubEnv('NODE_ENV', 'production'); |
| 77 | + vi.doMock('@/env', () => mockEnv('<your_token>')); |
| 78 | + |
| 79 | + const { getSpeakers } = await import('@/utils/getSpeakers'); |
| 80 | + const result = await getSpeakers(); |
| 81 | + |
| 82 | + expect(result).toEqual([]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('returns mock speakers on HTML (Cloudflare) response in development', async () => { |
| 86 | + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 87 | + global.fetch = vi.fn(() => |
| 88 | + Promise.resolve( |
| 89 | + createFetchResponse({ |
| 90 | + ok: false, |
| 91 | + status: 403, |
| 92 | + statusText: 'Forbidden', |
| 93 | + text: () => Promise.resolve('<!DOCTYPE html><html>...</html>'), |
| 94 | + }), |
| 95 | + ), |
| 96 | + ); |
| 97 | + |
| 98 | + vi.doMock('@/env', () => mockEnv('real-token')); |
| 99 | + |
| 100 | + const { getSpeakers } = await import('@/utils/getSpeakers'); |
| 101 | + const result = await getSpeakers(); |
| 102 | + |
| 103 | + expect(result).toEqual(MOCK_SPEAKERS); |
| 104 | + expect(warnSpy).toHaveBeenCalledWith( |
| 105 | + expect.stringContaining('Cloudflare/WAF'), |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('returns empty array on API error in production', async () => { |
| 110 | + vi.unstubAllEnvs(); |
| 111 | + vi.stubEnv('NODE_ENV', 'production'); |
| 112 | + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 113 | + global.fetch = vi.fn(() => |
| 114 | + Promise.resolve( |
| 115 | + createFetchResponse({ |
| 116 | + ok: false, |
| 117 | + status: 500, |
| 118 | + statusText: 'Internal Server Error', |
| 119 | + text: () => Promise.resolve('server error'), |
| 120 | + }), |
| 121 | + ), |
| 122 | + ); |
| 123 | + |
| 124 | + vi.doMock('@/env', () => mockEnv('real-token')); |
| 125 | + |
| 126 | + const { getSpeakers } = await import('@/utils/getSpeakers'); |
| 127 | + const result = await getSpeakers(); |
| 128 | + |
| 129 | + expect(result).toEqual([]); |
| 130 | + expect(errorSpy).toHaveBeenCalled(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments