Skip to content

Commit

Permalink
feat: respect the enabled flag even if the selected variant is alread…
Browse files Browse the repository at this point in the history
…y stored
  • Loading branch information
mateuszkulpa committed Mar 23, 2024
1 parent 9ff0685 commit 56e1d3c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/runtime/composables/useABTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export function useABTest<TVariantValue extends JSONValue>(
): ABTestResult<TVariantValue> {
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

Expand Down
5 changes: 0 additions & 5 deletions src/runtime/core/variants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import type { ABTest, ABTestResult, JSONValue } from '~/src/runtime/types'
export function resolveABTestVariant<TVariantValue extends JSONValue>(
abTest: ABTest<TVariantValue>
): ABTestResult<TVariantValue> {
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,
Expand Down
5 changes: 3 additions & 2 deletions test/variants.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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' },
Expand All @@ -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' },
Expand Down

0 comments on commit 56e1d3c

Please sign in to comment.