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

118 lines (99 loc) 4.3 kB
'use strict'; var _keys = require('babel-runtime/core-js/object/keys'); var _keys2 = _interopRequireDefault(_keys); var _typeof2 = require('babel-runtime/helpers/typeof'); var _typeof3 = _interopRequireDefault(_typeof2); var _immutable = require('immutable'); var _immutable2 = _interopRequireDefault(_immutable); var _createCacheStore = require('./create-cache-store'); var _createCacheStore2 = _interopRequireDefault(_createCacheStore); var _selectVariation = require('./select-variation'); var _selectVariation2 = _interopRequireDefault(_selectVariation); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } var variation_original = { name: 'Original', weight: 5000 }; var variation_a = { name: 'Variation #A', weight: 5000 }; var variation_b = { name: 'Variation #B', weight: 0 }; var experiment = { name: 'Test-Name', variations: [variation_original, variation_a, variation_b] }; describe('utils/select-variation.js', function () { it('exists', function () { expect(_selectVariation2['default']).not.toBeUndefined; expect(typeof _selectVariation2['default'] === 'undefined' ? 'undefined' : (0, _typeof3['default'])(_selectVariation2['default'])).toEqual('function'); }); it('chooses the active variation from redux store w/o cacheStore', function () { var output = (0, _selectVariation2['default'])({ active: _immutable2['default'].fromJS({ 'Test-Name': 'Variation #B' }), experiment: _immutable2['default'].fromJS(experiment), defaultVariationName: null }); expect(output).not.toBeUndefined; expect(output.toJSON()).toEqual(variation_b); }); it('chooses the active variation from redux store w/cacheStore', function () { var cacheStore = (0, _createCacheStore2['default'])(); var output = (0, _selectVariation2['default'])({ active: _immutable2['default'].fromJS({ 'Test-Name': 'Variation #B' }), experiment: _immutable2['default'].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((0, _keys2['default'])(cacheStore.cache()).length).toEqual(0); }); it('chooses the defaultVariationName variation', function () { var cacheStore = (0, _createCacheStore2['default'])(); var output = (0, _selectVariation2['default'])({ active: _immutable2['default'].fromJS({}), experiment: _immutable2['default'].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', function () { var cacheStore = (0, _createCacheStore2['default'])(); cacheStore.setItem(experiment.name, variation_b.name); var output = (0, _selectVariation2['default'])({ active: _immutable2['default'].fromJS({}), experiment: _immutable2['default'].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', function () { var cacheStore = (0, _createCacheStore2['default'])(); var output = (0, _selectVariation2['default'])({ active: _immutable2['default'].fromJS({}), experiment: _immutable2['default'].fromJS(experiment), defaultVariationName: null, cacheStore: 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); }); });