diff --git a/src/runtime/composables/useABTest.ts b/src/runtime/composables/useABTest.ts index 0e4c904..59a5c91 100644 --- a/src/runtime/composables/useABTest.ts +++ b/src/runtime/composables/useABTest.ts @@ -9,6 +9,11 @@ export function useABTest( ): ABTestResult { const storageId = `${STORAGE_KEY_PREFIX}:${abTest.id}` + if (abTest.enabled === false) { + const defaultVariant = abTest.variants.find(({ id }) => id === abTest.default) + return { enabled: false, result: defaultVariant } + } + const runtimeConfig = useRuntimeConfig() const { persistVariants, variantMaxAge } = runtimeConfig.public.abTesting diff --git a/src/runtime/core/variants.ts b/src/runtime/core/variants.ts index 62ddc50..8818691 100644 --- a/src/runtime/core/variants.ts +++ b/src/runtime/core/variants.ts @@ -3,11 +3,6 @@ import type { ABTest, ABTestResult, JSONValue } from '~/src/runtime/types' export function resolveABTestVariant( abTest: ABTest ): ABTestResult { - if (abTest.enabled === false) { - const defaultVariant = abTest.variants.find(({ id }) => id === abTest.default) - return { enabled: false, result: defaultVariant } - } - if (abTest.variants.every(variant => variant.weight === undefined)) { return { enabled: true, diff --git a/test/variants.test.ts b/test/variants.test.ts index 637f91d..50545a0 100644 --- a/test/variants.test.ts +++ b/test/variants.test.ts @@ -1,4 +1,5 @@ import { describe, expect, test } from 'vitest' +import { useABTest } from '~/src/runtime/composables/useABTest' import { resolveABTestVariant } from '~/src/runtime/core/variants' describe('useABTest', () => { @@ -32,7 +33,7 @@ describe('useABTest', () => { ) test('returns default variant when ab test is not enabled', () => { - const result = resolveABTestVariant({ + const result = useABTest({ id: 'test-id', variants: [ { id: 'a', value: 'a' }, @@ -46,7 +47,7 @@ describe('useABTest', () => { }) test('returns no result when default value is not defined', () => { - const result = resolveABTestVariant({ + const result = useABTest({ id: 'test-id', variants: [ { id: 'a', value: 'a' },