Debian Codegraph @

skins/Vector/tests/jest/AB.test.js

raw symbols SCIP
1 const AB = require( '../../resources/skins.vector.js/AB.js' );
2 const NAME_OF_EXPERIMENT = 'name-of-experiment';
3 const TOKEN = 'token';
4 const MW_EXPERIMENT_PARAM = {
5 name: NAME_OF_EXPERIMENT,
6 enabled: true,
7 buckets: {
8 unsampled: 0.5,
9 control: 0.25,
10 treatment: 0.25
11 }
12 };
13
14 /**
15 * @param {string} token
16 * @return {AB.WebABTest}
17 */
18 function createInstance( token ) {
19 const mergedProps = /** @type {AB.WebABTestProps} */ ( {
20 name: NAME_OF_EXPERIMENT,
21 buckets: {
22 unsampled: {
23 samplingRate: 0.5
24 },
25 control: {
26 samplingRate: 0.25
27 },
28 treatment: {
29 samplingRate: 0.25
30 }
31 }
32 } );
33
34 return AB( mergedProps, token, true );
35 }
36
37 describe( 'AB.js', () => {
38 const bucket = 'treatment';
39 const getBucketMock = jest.fn().mockReturnValue( bucket );
40 mw.experiments.getBucket = getBucketMock;
41
42 afterEach( () => {
43 document.body.removeAttribute( 'class' );
44 } );
45
46 describe( 'initialization when body tag does not contain bucket', () => {
47 let /** @type {jest.SpyInstance} */ hookMock;
48
49 beforeEach( () => {
50 hookMock = jest.spyOn( mw, 'hook' );
51 } );
52
53 it( 'sends data to WikimediaEvents when the bucket is part of sample (e.g. control)', () => {
54 getBucketMock.mockReturnValueOnce( 'control' );
55 createInstance( TOKEN );
56 expect( hookMock ).toHaveBeenCalled();
57 } );
58 it( 'sends data to WikimediaEvents when the bucket is part of sample (e.g. treatment)', () => {
59 getBucketMock.mockReturnValueOnce( 'treatment' );
60 createInstance( TOKEN );
61 expect( hookMock ).toHaveBeenCalled();
62 } );
63 it( 'does not send data to WikimediaEvents when the bucket is unsampled ', () => {
64 getBucketMock.mockReturnValueOnce( 'unsampled' );
65 createInstance( TOKEN );
66 expect( hookMock ).not.toHaveBeenCalled();
67 } );
68 } );
69
70 describe( 'initialization when body tag contains bucket', () => {
71 let /** @type {jest.SpyInstance} */ hookMock;
72
73 beforeEach( () => {
74 hookMock = jest.spyOn( mw, 'hook' );
75 } );
76
77 it( 'sends data to WikimediaEvents when the bucket is part of sample (e.g. control)', () => {
78 document.body.classList.add( 'name-of-experiment-control' );
79 createInstance( TOKEN );
80 expect( hookMock ).toHaveBeenCalled();
81 } );
82 it( 'sends data to WikimediaEvents when the bucket is part of sample (e.g. treatment)', () => {
83 document.body.classList.add( 'name-of-experiment-treatment' );
84 createInstance( TOKEN );
85 expect( hookMock ).toHaveBeenCalled();
86 } );
87 it( 'does not send data to WikimediaEvents when the bucket is unsampled ', () => {
88 document.body.classList.add( 'name-of-experiment-unsampled' );
89 createInstance( TOKEN );
90 expect( hookMock ).not.toHaveBeenCalled();
91 } );
92 } );
93
94 describe( 'initialization when token is undefined', () => {
95 it( 'throws an error', () => {
96 expect( () => {
97 createInstance( undefined );
98 } ).toThrow( 'Tried to call `getBucket`' );
99 } );
100 } );
101
102 describe( 'getBucket when body tag does not contain AB class', () => {
103 it( 'calls mw.experiments.getBucket with config data', () => {
104 const experiment = createInstance( TOKEN );
105
106 expect( getBucketMock ).toBeCalledWith( MW_EXPERIMENT_PARAM, TOKEN );
107 expect( experiment.getBucket() ).toBe( bucket );
108 } );
109 } );
110
111 describe( 'getBucket when body tag contains AB class that is in the sample', () => {
112 it( 'returns the bucket on the body tag', () => {
113 document.body.classList.add( 'name-of-experiment-control' );
114 const experiment = createInstance( TOKEN );
115
116 expect( getBucketMock ).not.toHaveBeenCalled();
117 expect( experiment.getBucket() ).toBe( 'control' );
118 } );
119 } );
120
121 describe( 'getBucket when body tag contains AB class that is not in the sample', () => {
122 it( 'returns the bucket on the body tag', () => {
123 document.body.classList.add( 'name-of-experiment-unsampled' );
124 const experiment = createInstance( TOKEN );
125
126 expect( getBucketMock ).not.toHaveBeenCalled();
127 expect( experiment.getBucket() ).toBe( 'unsampled' );
128 } );
129 } );
130
131 describe( 'isInBucket', () => {
132 it( 'compares assigned bucket with passed in bucket', () => {
133 const experiment = createInstance( TOKEN );
134
135 expect( experiment.isInBucket( 'treatment' ) ).toBe( true );
136 } );
137 } );
138
139 describe( 'isInTreatmentBucket when assigned to unsampled bucket (from server)', () => {
140 it( 'returns false', () => {
141 document.body.classList.add( 'name-of-experiment-unsampled' );
142 const experiment = createInstance( TOKEN );
143
144 expect( experiment.isInTreatmentBucket() ).toBe( false );
145 } );
146 } );
147
148 describe( 'isInTreatmentBucket when assigned to control bucket (from server)', () => {
149 it( 'returns false', () => {
150 document.body.classList.add( 'name-of-experiment-control' );
151 const experiment = createInstance( TOKEN );
152
153 expect( experiment.isInTreatmentBucket() ).toBe( false );
154 } );
155 } );
156
157 describe( 'isInTreatmentBucket when assigned to treatment bucket (from server)', () => {
158 it( 'returns true', () => {
159 document.body.classList.add( 'name-of-experiment-treatment' );
160 const experiment = createInstance( TOKEN );
161
162 expect( experiment.isInTreatmentBucket() ).toBe( true );
163 } );
164 } );
165
166 describe( 'isInTreatmentBucket when assigned to unsampled bucket (from client)', () => {
167 it( 'returns false', () => {
168 getBucketMock.mockReturnValueOnce( 'unsampled' );
169 const experiment = createInstance( TOKEN );
170
171 expect( experiment.isInTreatmentBucket() ).toBe( false );
172 } );
173 } );
174
175 describe( 'isInTreatmentBucket when assigned to control bucket (from client)', () => {
176 it( 'returns false', () => {
177 getBucketMock.mockReturnValueOnce( 'control' );
178 const experiment = createInstance( TOKEN );
179
180 expect( experiment.isInTreatmentBucket() ).toBe( false );
181 } );
182 } );
183
184 describe( 'isInTreatmentBucket when assigned to treatment bucket (from client)', () => {
185 it( 'returns true', () => {
186 getBucketMock.mockReturnValueOnce( 'treatment' );
187 const experiment = createInstance( TOKEN );
188
189 expect( experiment.isInTreatmentBucket() ).toBe( true );
190 } );
191 } );
192
193 describe( 'isInTreatmentBucket when assigned to treatment bucket (is case insensitive)', () => {
194 it( 'returns true', () => {
195 getBucketMock.mockReturnValueOnce( 'StickyHeaderVisibleTreatment' );
196 const experiment = createInstance( TOKEN );
197
198 expect( experiment.isInTreatmentBucket() ).toBe( true );
199 } );
200 } );
201
202 describe( 'isInSample when in unsampled bucket', () => {
203 it( 'returns false', () => {
204 document.body.classList.add( 'name-of-experiment-unsampled' );
205 const experiment = createInstance( TOKEN );
206
207 expect( experiment.isInSample() ).toBe( false );
208 } );
209 } );
210
211 describe( 'isInSample when in control bucket', () => {
212 it( 'returns true', () => {
213 document.body.classList.add( 'name-of-experiment-control' );
214 const experiment = createInstance( TOKEN );
215
216 expect( experiment.isInSample() ).toBe( true );
217 } );
218 } );
219
220 describe( 'isInSample when in treatment bucket', () => {
221 it( 'returns true', () => {
222 document.body.classList.add( 'name-of-experiment-treatment' );
223 const experiment = createInstance( TOKEN );
224
225 expect( experiment.isInSample() ).toBe( true );
226 } );
227 } );
228 } );