1 | import selectExperimentVariant from './selectExperimentVariant';
|
2 |
|
3 | describe('selectExperimentVariant', () => {
|
4 | it('returns null if no config is supplied', () => {
|
5 | expect(selectExperimentVariant(null)).toBe(null);
|
6 | });
|
7 |
|
8 | it('returns the previous value if experiment matches the config', () => {
|
9 | const config = {
|
10 | experimentId: 'test',
|
11 | variantWeights: [0.5]
|
12 | };
|
13 |
|
14 | const selectedVariant = 'test.0';
|
15 |
|
16 | expect(selectExperimentVariant(config, selectedVariant)).toBe(
|
17 | selectedVariant
|
18 | );
|
19 | });
|
20 |
|
21 | it('returns a new value if experiment id is correct but variant is out of range', () => {
|
22 | const config = {
|
23 | experimentId: 'test',
|
24 | variantWeights: [1]
|
25 | };
|
26 |
|
27 | const selectedVariant = 'test.2';
|
28 |
|
29 | const newVariant = selectExperimentVariant(config, selectedVariant);
|
30 | expect(newVariant).not.toBe(selectedVariant);
|
31 | expect(newVariant).toBe('test.0');
|
32 | });
|
33 |
|
34 | it('returns a new value if experiment id is wrong', () => {
|
35 | const config = {
|
36 | experimentId: 'other',
|
37 | variantWeights: [1]
|
38 | };
|
39 |
|
40 | const selectedVariant = 'test.0';
|
41 |
|
42 | const newVariant = selectExperimentVariant(config, selectedVariant);
|
43 | expect(newVariant).not.toBe(selectedVariant);
|
44 | expect(newVariant).toBe('other.0');
|
45 | });
|
46 |
|
47 | it('selects a random variant if no previous one was provided', () => {
|
48 | const config = {
|
49 | experimentId: 'test',
|
50 | variantWeights: [0.25, 0.25, 0.25, 0.25]
|
51 | };
|
52 |
|
53 | expect(selectExperimentVariant(config, null)).not.toBe(null);
|
54 | });
|
55 |
|
56 | it('works if sum of variant weights is more than 1', () => {
|
57 | const config = {
|
58 | experimentId: 'test',
|
59 | variantWeights: [1, 1, 1, 1]
|
60 | };
|
61 |
|
62 | expect(selectExperimentVariant(config, null)).not.toBe(null);
|
63 | });
|
64 | });
|