Files
supabase/apps/studio/lib/configcat.test.ts
Kang Ming cacfe7fb12 fix: use configcat proxy (#36130)
* fix: use configcat proxy

* fix: add fallback to origin

* chore: fix test
2025-06-09 20:19:52 -07:00

52 lines
1.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import * as configcat from 'configcat-js'
import { getFlags } from './configcat'
vi.mock('data/fetchers', () => ({
fetchHandler: vi.fn(),
}))
vi.mock('configcat-js', () => ({
getClient: vi.fn(),
PollingMode: {
AutoPoll: 'AutoPoll',
},
User: vi.fn(),
}))
describe('configcat', () => {
const mockClient = {
getAllValuesAsync: vi.fn(),
}
beforeEach(() => {
vi.clearAllMocks()
;(configcat.getClient as any).mockReturnValue(mockClient)
})
it('should return empty array when no email is provided', async () => {
const result = await getFlags()
expect(result).toEqual([])
})
it('should call getAllValuesAsync with user when email is provided', async () => {
const email = 'test@example.com'
const mockValues = { flag1: true, flag2: false }
mockClient.getAllValuesAsync.mockResolvedValue(mockValues)
const { fetchHandler } = await import('data/fetchers')
const mockFetchHandler = fetchHandler as unknown as ReturnType<typeof vi.fn>
mockFetchHandler.mockResolvedValueOnce(
new Response(JSON.stringify({}), {
status: 200,
})
)
const result = await getFlags(email)
expect(configcat.User).toHaveBeenCalledWith(email)
expect(mockClient.getAllValuesAsync).toHaveBeenCalled()
expect(result).toEqual(mockValues)
})
})