|
1 | | -import ABConfiguration from './abConfiguration'; |
| 1 | +import { getABVariants } from './abConfiguration'; |
2 | 2 | import Visitor from './visitor'; |
3 | 3 | import { createConfig } from './test-utils'; |
4 | 4 | import type { Config } from './config'; |
@@ -28,160 +28,111 @@ function createVisitor(config: Config) { |
28 | 28 | return visitor; |
29 | 29 | } |
30 | 30 |
|
31 | | -describe('ABConfiguration', () => { |
32 | | - it('requires a splitName', () => { |
| 31 | +describe('getABVariants()', () => { |
| 32 | + it('logs an error if the split does not have exactly two variants', () => { |
33 | 33 | const config = setupConfig(); |
34 | 34 | const visitor = createVisitor(config); |
35 | 35 |
|
36 | | - expect(() => { |
37 | | - // @ts-expect-error Testing missing required property |
38 | | - new ABConfiguration({ trueVariant: 'red', visitor: visitor }); |
39 | | - }).toThrow('must provide splitName'); |
40 | | - }); |
41 | | - |
42 | | - it('requires an trueVariant', () => { |
43 | | - const config = setupConfig(); |
44 | | - const visitor = createVisitor(config); |
45 | | - |
46 | | - expect(() => { |
47 | | - new ABConfiguration({ splitName: 'button_color', visitor: visitor }); |
48 | | - }).toThrow('must provide trueVariant'); |
49 | | - }); |
| 36 | + getABVariants({ |
| 37 | + splitName: 'element', |
| 38 | + trueVariant: 'water', |
| 39 | + visitor: visitor |
| 40 | + }); |
50 | 41 |
|
51 | | - it('requires a visitor', () => { |
52 | | - expect(() => { |
53 | | - // @ts-expect-error Testing missing required property |
54 | | - new ABConfiguration({ splitName: 'button_color', trueVariant: 'red' }); |
55 | | - }).toThrow('must provide visitor'); |
| 42 | + expect(visitor.logError).toHaveBeenCalledWith('A/B for element configures split with more than 2 variants'); |
56 | 43 | }); |
57 | 44 |
|
58 | | - it('allows a null trueVariant', () => { |
59 | | - const config = setupConfig(); |
| 45 | + it('does not log an error if the split registry is not loaded', () => { |
| 46 | + const config = createConfig(); |
60 | 47 | const visitor = createVisitor(config); |
61 | 48 |
|
62 | | - expect(() => { |
63 | | - new ABConfiguration({ |
64 | | - splitName: 'button_color', |
65 | | - // @ts-expect-error Testing null value |
66 | | - trueVariant: null, |
67 | | - visitor: visitor |
68 | | - }); |
69 | | - }).not.toThrow(); |
| 49 | + getABVariants({ |
| 50 | + splitName: 'element', |
| 51 | + trueVariant: 'water', |
| 52 | + visitor |
| 53 | + }); |
| 54 | + |
| 55 | + expect(visitor.logError).not.toHaveBeenCalled(); |
70 | 56 | }); |
71 | 57 |
|
72 | | - describe('#getVariants()', () => { |
73 | | - it('logs an error if the split does not have exactly two variants', () => { |
| 58 | + describe('true variant', () => { |
| 59 | + it('accepts `true` as a fallback value', () => { |
74 | 60 | const config = setupConfig(); |
75 | | - const visitor = createVisitor(config); |
76 | | - const abConfiguration = new ABConfiguration({ |
77 | | - splitName: 'element', |
78 | | - trueVariant: 'water', |
79 | | - visitor: visitor |
| 61 | + const variants = getABVariants({ |
| 62 | + splitName: 'button_color', |
| 63 | + trueVariant: 'true', |
| 64 | + visitor: createVisitor(config) |
80 | 65 | }); |
81 | 66 |
|
82 | | - abConfiguration.getVariants(); |
83 | | - |
84 | | - expect(visitor.logError).toHaveBeenCalledWith('A/B for element configures split with more than 2 variants'); |
| 67 | + expect(variants.true).toBe('true'); |
85 | 68 | }); |
86 | 69 |
|
87 | | - it('does not log an error if the split registry is not loaded', () => { |
88 | | - const config = createConfig(); |
89 | | - |
90 | | - const visitor = createVisitor(config); |
91 | | - const abConfiguration = new ABConfiguration({ |
92 | | - splitName: 'element', |
93 | | - trueVariant: 'water', |
94 | | - visitor |
| 70 | + it('is true if only one variant in the split', () => { |
| 71 | + const config = setupConfig(); |
| 72 | + const variants = getABVariants({ |
| 73 | + splitName: 'new_feature', |
| 74 | + trueVariant: 'true', |
| 75 | + visitor: createVisitor(config) |
95 | 76 | }); |
96 | 77 |
|
97 | | - abConfiguration.getVariants(); |
98 | | - |
99 | | - expect(visitor.logError).not.toHaveBeenCalled(); |
| 78 | + expect(variants.true).toBe('true'); |
100 | 79 | }); |
101 | 80 |
|
102 | | - describe('true variant', () => { |
103 | | - it('is true if null was passed in during instantiation', () => { |
104 | | - const config = setupConfig(); |
105 | | - const abConfiguration = new ABConfiguration({ |
106 | | - splitName: 'button_color', |
107 | | - // @ts-expect-error Testing null value |
108 | | - trueVariant: null, |
109 | | - visitor: createVisitor(config) |
110 | | - }); |
111 | | - |
112 | | - expect(abConfiguration.getVariants().true).toBe('true'); |
| 81 | + it('is whatever was passed in', () => { |
| 82 | + const config = setupConfig(); |
| 83 | + const variants = getABVariants({ |
| 84 | + splitName: 'button_color', |
| 85 | + trueVariant: 'red', |
| 86 | + visitor: createVisitor(config) |
113 | 87 | }); |
114 | 88 |
|
115 | | - it('is true if only one variant in the split', () => { |
116 | | - const config = setupConfig(); |
117 | | - const abConfiguration = new ABConfiguration({ |
118 | | - splitName: 'new_feature', |
119 | | - // @ts-expect-error Testing null value |
120 | | - trueVariant: null, |
121 | | - visitor: createVisitor(config) |
122 | | - }); |
| 89 | + expect(variants.true).toBe('red'); |
| 90 | + }); |
| 91 | + }); |
123 | 92 |
|
124 | | - expect(abConfiguration.getVariants().true).toBe('true'); |
| 93 | + describe('false variant', () => { |
| 94 | + it('is the variant of the split that is not the true_variant', () => { |
| 95 | + const config = setupConfig(); |
| 96 | + const variants = getABVariants({ |
| 97 | + splitName: 'button_color', |
| 98 | + trueVariant: 'red', |
| 99 | + visitor: createVisitor(config) |
125 | 100 | }); |
126 | 101 |
|
127 | | - it('is whatever was passed in during instantiation', () => { |
128 | | - const config = setupConfig(); |
129 | | - const abConfiguration = new ABConfiguration({ |
130 | | - splitName: 'button_color', |
131 | | - trueVariant: 'red', |
132 | | - visitor: createVisitor(config) |
133 | | - }); |
134 | | - |
135 | | - expect(abConfiguration.getVariants().true).toBe('red'); |
136 | | - }); |
| 102 | + expect(variants.false).toBe('blue'); |
137 | 103 | }); |
138 | 104 |
|
139 | | - describe('false variant', () => { |
140 | | - it('is the variant of the split that is not the true_variant', () => { |
141 | | - const config = setupConfig(); |
142 | | - const abConfiguration = new ABConfiguration({ |
143 | | - splitName: 'button_color', |
144 | | - trueVariant: 'red', |
145 | | - visitor: createVisitor(config) |
146 | | - }); |
147 | | - |
148 | | - expect(abConfiguration.getVariants().false).toBe('blue'); |
| 105 | + it('is false when there is no split_registry', () => { |
| 106 | + const config = createConfig(); |
| 107 | + const variants = getABVariants({ |
| 108 | + splitName: 'button_color', |
| 109 | + trueVariant: 'red', |
| 110 | + visitor: createVisitor(config) |
149 | 111 | }); |
150 | 112 |
|
151 | | - it('is false when there is no split_registry', () => { |
152 | | - const config = createConfig(); |
153 | | - |
154 | | - const abConfiguration = new ABConfiguration({ |
155 | | - splitName: 'button_color', |
156 | | - trueVariant: 'red', |
157 | | - visitor: createVisitor(config) |
158 | | - }); |
| 113 | + expect(variants.false).toBe('false'); |
| 114 | + }); |
159 | 115 |
|
160 | | - expect(abConfiguration.getVariants().false).toBe('false'); |
| 116 | + it('is always the same if the split has more than two variants', () => { |
| 117 | + const config = setupConfig(); |
| 118 | + const variants = getABVariants({ |
| 119 | + splitName: 'element', |
| 120 | + trueVariant: 'earth', |
| 121 | + visitor: createVisitor(config) |
161 | 122 | }); |
162 | 123 |
|
163 | | - it('is always the same if the split has more than two variants', () => { |
164 | | - const config = setupConfig(); |
165 | | - const abConfiguration = new ABConfiguration({ |
166 | | - splitName: 'element', |
167 | | - trueVariant: 'earth', |
168 | | - visitor: createVisitor(config) |
169 | | - }); |
| 124 | + expect(variants.false).toBe('fire'); |
| 125 | + }); |
170 | 126 |
|
171 | | - expect(abConfiguration.getVariants().false).toBe('fire'); |
| 127 | + it('is false if only one variant in the split', () => { |
| 128 | + const config = setupConfig(); |
| 129 | + const variants = getABVariants({ |
| 130 | + splitName: 'new_feature', |
| 131 | + trueVariant: 'true', |
| 132 | + visitor: createVisitor(config) |
172 | 133 | }); |
173 | 134 |
|
174 | | - it('is false if only one variant in the split', () => { |
175 | | - const config = setupConfig(); |
176 | | - const abConfiguration = new ABConfiguration({ |
177 | | - splitName: 'new_feature', |
178 | | - // @ts-expect-error Testing null value |
179 | | - trueVariant: null, |
180 | | - visitor: createVisitor(config) |
181 | | - }); |
182 | | - |
183 | | - expect(abConfiguration.getVariants().false).toBe('false'); |
184 | | - }); |
| 135 | + expect(variants.false).toBe('false'); |
185 | 136 | }); |
186 | 137 | }); |
187 | 138 | }); |
0 commit comments