|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { isReactive, isReadonly } from 'vue' |
| 3 | + |
| 4 | +import { |
| 5 | + ServerFeatureFlag, |
| 6 | + useFeatureFlags |
| 7 | +} from '@/composables/useFeatureFlags' |
| 8 | +import { api } from '@/scripts/api' |
| 9 | + |
| 10 | +// Mock the API module |
| 11 | +vi.mock('@/scripts/api', () => ({ |
| 12 | + api: { |
| 13 | + getServerFeature: vi.fn() |
| 14 | + } |
| 15 | +})) |
| 16 | + |
| 17 | +describe('useFeatureFlags', () => { |
| 18 | + beforeEach(() => { |
| 19 | + vi.clearAllMocks() |
| 20 | + }) |
| 21 | + |
| 22 | + describe('flags object', () => { |
| 23 | + it('should provide reactive readonly flags', () => { |
| 24 | + const { flags } = useFeatureFlags() |
| 25 | + |
| 26 | + expect(isReadonly(flags)).toBe(true) |
| 27 | + expect(isReactive(flags)).toBe(true) |
| 28 | + }) |
| 29 | + |
| 30 | + it('should access supportsPreviewMetadata', () => { |
| 31 | + vi.mocked(api.getServerFeature).mockImplementation( |
| 32 | + (path, defaultValue) => { |
| 33 | + if (path === ServerFeatureFlag.SUPPORTS_PREVIEW_METADATA) |
| 34 | + return true as any |
| 35 | + return defaultValue |
| 36 | + } |
| 37 | + ) |
| 38 | + |
| 39 | + const { flags } = useFeatureFlags() |
| 40 | + expect(flags.supportsPreviewMetadata).toBe(true) |
| 41 | + expect(api.getServerFeature).toHaveBeenCalledWith( |
| 42 | + ServerFeatureFlag.SUPPORTS_PREVIEW_METADATA |
| 43 | + ) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should access maxUploadSize', () => { |
| 47 | + vi.mocked(api.getServerFeature).mockImplementation( |
| 48 | + (path, defaultValue) => { |
| 49 | + if (path === ServerFeatureFlag.MAX_UPLOAD_SIZE) |
| 50 | + return 209715200 as any // 200MB |
| 51 | + return defaultValue |
| 52 | + } |
| 53 | + ) |
| 54 | + |
| 55 | + const { flags } = useFeatureFlags() |
| 56 | + expect(flags.maxUploadSize).toBe(209715200) |
| 57 | + expect(api.getServerFeature).toHaveBeenCalledWith( |
| 58 | + ServerFeatureFlag.MAX_UPLOAD_SIZE |
| 59 | + ) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should return undefined when features are not available and no default provided', () => { |
| 63 | + vi.mocked(api.getServerFeature).mockImplementation( |
| 64 | + (_path, defaultValue) => defaultValue as any |
| 65 | + ) |
| 66 | + |
| 67 | + const { flags } = useFeatureFlags() |
| 68 | + expect(flags.supportsPreviewMetadata).toBeUndefined() |
| 69 | + expect(flags.maxUploadSize).toBeUndefined() |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + describe('featureFlag', () => { |
| 74 | + it('should create reactive computed for custom feature flags', () => { |
| 75 | + vi.mocked(api.getServerFeature).mockImplementation( |
| 76 | + (path, defaultValue) => { |
| 77 | + if (path === 'custom.feature') return 'custom-value' as any |
| 78 | + return defaultValue |
| 79 | + } |
| 80 | + ) |
| 81 | + |
| 82 | + const { featureFlag } = useFeatureFlags() |
| 83 | + const customFlag = featureFlag('custom.feature', 'default') |
| 84 | + |
| 85 | + expect(customFlag.value).toBe('custom-value') |
| 86 | + expect(api.getServerFeature).toHaveBeenCalledWith( |
| 87 | + 'custom.feature', |
| 88 | + 'default' |
| 89 | + ) |
| 90 | + }) |
| 91 | + |
| 92 | + it('should handle nested paths', () => { |
| 93 | + vi.mocked(api.getServerFeature).mockImplementation( |
| 94 | + (path, defaultValue) => { |
| 95 | + if (path === 'extension.custom.nested.feature') return true as any |
| 96 | + return defaultValue |
| 97 | + } |
| 98 | + ) |
| 99 | + |
| 100 | + const { featureFlag } = useFeatureFlags() |
| 101 | + const nestedFlag = featureFlag('extension.custom.nested.feature', false) |
| 102 | + |
| 103 | + expect(nestedFlag.value).toBe(true) |
| 104 | + }) |
| 105 | + |
| 106 | + it('should work with ServerFeatureFlag enum', () => { |
| 107 | + vi.mocked(api.getServerFeature).mockImplementation( |
| 108 | + (path, defaultValue) => { |
| 109 | + if (path === ServerFeatureFlag.MAX_UPLOAD_SIZE) |
| 110 | + return 104857600 as any |
| 111 | + return defaultValue |
| 112 | + } |
| 113 | + ) |
| 114 | + |
| 115 | + const { featureFlag } = useFeatureFlags() |
| 116 | + const maxUploadSize = featureFlag(ServerFeatureFlag.MAX_UPLOAD_SIZE) |
| 117 | + |
| 118 | + expect(maxUploadSize.value).toBe(104857600) |
| 119 | + }) |
| 120 | + }) |
| 121 | +}) |
0 commit comments