|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +const mockIsEnabled = vi.fn(); |
| 4 | +const mockGetVariant = vi.fn(); |
| 5 | +const mockOn = vi.fn(); |
| 6 | +const mockInitialize = vi.fn(() => ({ |
| 7 | + isEnabled: mockIsEnabled, |
| 8 | + getVariant: mockGetVariant, |
| 9 | + on: mockOn, |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('unleash-client', () => ({ |
| 13 | + initialize: mockInitialize, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('@/utils/logger', () => ({ |
| 17 | + default: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, |
| 18 | +})); |
| 19 | + |
| 20 | +describe('unleash', () => { |
| 21 | + beforeEach(() => { |
| 22 | + vi.clearAllMocks(); |
| 23 | + vi.resetModules(); |
| 24 | + delete process.env.UNLEASH_SERVER_API_URL; |
| 25 | + delete process.env.UNLEASH_SERVER_API_TOKEN; |
| 26 | + delete process.env.UNLEASH_SERVER_API_ENVIRONMENT; |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns default when env vars are missing', async () => { |
| 30 | + const { isEnabled, FEATURE_FLAGS } = await import('@/utils/unleash'); |
| 31 | + expect(isEnabled(FEATURE_FLAGS.QUOTES_SUBMIT)).toBe(true); |
| 32 | + expect(isEnabled(FEATURE_FLAGS.QUOTES_ERRORS)).toBe(false); |
| 33 | + expect(mockInitialize).not.toHaveBeenCalled(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('initializes client and checks flags', async () => { |
| 37 | + process.env.UNLEASH_SERVER_API_URL = 'http://unleash'; |
| 38 | + process.env.UNLEASH_SERVER_API_TOKEN = 'test-token'; |
| 39 | + mockIsEnabled.mockReturnValue(true); |
| 40 | + |
| 41 | + const { isEnabled, FEATURE_FLAGS } = await import('@/utils/unleash'); |
| 42 | + const result = isEnabled(FEATURE_FLAGS.QUOTES_SUBMIT); |
| 43 | + |
| 44 | + expect(mockInitialize).toHaveBeenCalledWith( |
| 45 | + expect.objectContaining({ |
| 46 | + url: 'http://unleash/', |
| 47 | + appName: 'quotes-frontend', |
| 48 | + }) |
| 49 | + ); |
| 50 | + expect(result).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it('registers lifecycle listeners', async () => { |
| 54 | + process.env.UNLEASH_SERVER_API_URL = 'http://unleash'; |
| 55 | + process.env.UNLEASH_SERVER_API_TOKEN = 'test-token'; |
| 56 | + mockIsEnabled.mockReturnValue(true); |
| 57 | + |
| 58 | + const { isEnabled, FEATURE_FLAGS } = await import('@/utils/unleash'); |
| 59 | + isEnabled(FEATURE_FLAGS.QUOTES_SUBMIT); |
| 60 | + |
| 61 | + const registeredEvents = mockOn.mock.calls.map((call: unknown[]) => call[0]); |
| 62 | + expect(registeredEvents).not.toContain('impression'); |
| 63 | + expect(registeredEvents).toContain('ready'); |
| 64 | + expect(registeredEvents).toContain('error'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('getVariant returns disabled when no client', async () => { |
| 68 | + const { getVariant, FEATURE_FLAGS } = await import('@/utils/unleash'); |
| 69 | + const variant = getVariant(FEATURE_FLAGS.QUOTES_SUBMIT); |
| 70 | + expect(variant).toEqual({ name: 'disabled', enabled: false }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('getVariant delegates to client when initialized', async () => { |
| 74 | + process.env.UNLEASH_SERVER_API_URL = 'http://unleash'; |
| 75 | + process.env.UNLEASH_SERVER_API_TOKEN = 'test-token'; |
| 76 | + mockGetVariant.mockReturnValue({ name: 'variantA', enabled: true, payload: { type: 'string', value: 'hello' } }); |
| 77 | + mockIsEnabled.mockReturnValue(true); |
| 78 | + |
| 79 | + const { getVariant, isEnabled, FEATURE_FLAGS } = await import('@/utils/unleash'); |
| 80 | + isEnabled(FEATURE_FLAGS.QUOTES_SUBMIT); // trigger init |
| 81 | + const variant = getVariant(FEATURE_FLAGS.QUOTES_SUBMIT); |
| 82 | + |
| 83 | + expect(variant.name).toBe('variantA'); |
| 84 | + expect(variant.enabled).toBe(true); |
| 85 | + }); |
| 86 | +}); |
0 commit comments