UNPKG

redux-ab-test

Version:

A/B testing React components with Redux and debug tools. Isomorphic with a simple, universal interface. Well documented and lightweight. Tested in popular browsers and Node.js. Includes helpers for React, Redux, and Segment.io

104 lines (94 loc) 3.52 kB
import Immutable from 'immutable'; import createCacheStore from './create-cache-store'; import selectVariation from './select-variation'; const variation_original = { name: 'Original', weight: 5000, }; const variation_a = { name: 'Variation #A', weight: 5000, }; const variation_b = { name: 'Variation #B', weight: 0, }; const experiment = { name: 'Test-Name', variations: [ variation_original, variation_a, variation_b, ], }; describe('utils/select-variation.js', () => { it('exists', () => { expect(selectVariation).not.toBeUndefined; expect(typeof selectVariation).toEqual('function'); }); it('chooses the active variation from redux store w/o cacheStore', () => { const output = selectVariation({ active: Immutable.fromJS({'Test-Name': 'Variation #B'}), experiment: Immutable.fromJS(experiment), defaultVariationName: null, }); expect(output).not.toBeUndefined; expect(output.toJSON()).toEqual(variation_b); }); it('chooses the active variation from redux store w/cacheStore', () => { const cacheStore = createCacheStore(); const output = selectVariation({ active: Immutable.fromJS({'Test-Name': 'Variation #B'}), experiment: Immutable.fromJS(experiment), defaultVariationName: null, cacheStore: cacheStore, }); expect(output).not.toBeUndefined; expect(output.toJSON()).toEqual(variation_b); // Cache does not have the selected variation, since it is stored in redux state! expect(Object.keys(cacheStore.cache()).length).toEqual(0); }); it('chooses the defaultVariationName variation', () => { const cacheStore = createCacheStore(); const output = selectVariation({ active: Immutable.fromJS({}), experiment: Immutable.fromJS(experiment), defaultVariationName: 'Variation #B', cacheStore: cacheStore, }); expect(output).not.toBeUndefined; expect(output.toJSON()).toEqual(variation_b); // Cache has the selected variation expect(cacheStore.cache().length).not.toEqual(0); expect(cacheStore.cache()[experiment.name]).toEqual(output.toJSON().name); }); it('chooses the active variation from :cacheStore', () => { const cacheStore = createCacheStore(); cacheStore.setItem(experiment.name, variation_b.name); const output = selectVariation({ active: Immutable.fromJS({}), experiment: Immutable.fromJS(experiment), defaultVariationName: null, cacheStore: cacheStore, }); expect(output).not.toBeUndefined; expect(output.toJSON()).toEqual(variation_b); // Cache has the selected variation expect(cacheStore.cache().length).not.toEqual(0); expect(cacheStore.cache()[experiment.name]).toEqual(output.toJSON().name); }); it('randomly assigns a variation, ignoring weight=0 records', () => { const cacheStore = createCacheStore(); const output = selectVariation({ active: Immutable.fromJS({}), experiment: Immutable.fromJS(experiment), defaultVariationName: null, cacheStore, }); expect(output).not.toBeUndefined; expect(output.toJSON()).not.toEqual(variation_b); // Cache has the selected variation expect(cacheStore.cache().length).not.toEqual(0); expect(cacheStore.cache()[experiment.name]).toEqual(output.toJSON().name); }); });