1+ var fs = require ( 'fs' ) ;
2+ var chai = require ( 'chai' ) ;
3+ chai . config . truncateThreshold = 0 ; // disable truncating
4+ var expect = chai . expect ;
5+ var proxyquire = require ( 'proxyquire' ) ;
6+ var quickTemp = require ( 'quick-temp' ) ;
7+ var _ = require ( 'lodash' ) ;
8+ var Project = require ( '../lib/project.js' ) ;
9+ var Experiment = require ( '../lib/experiment.js' ) ;
10+ var Variation = require ( '../lib/variation.js' ) ;
11+ var functionCalls = [ ] ;
12+ /**
13+ * Used to push any function calls and their
14+ * arguments to the funcitonCalls array. We
15+ * can use this function to replace any
16+ * function calls that we aren't trying to
17+ * test.
18+ * @param {String } functionName name of the function being called
19+ * @param {Object } [returnObject] object to return after recording the funciton call
20+ * @return {Function } function that when called will record functionName and it's arguments
21+ */
22+ var recordFunctionCalls = function ( functionName , returnObject ) {
23+ return function ( ) {
24+ var callRecorder = { functionName : functionName } ;
25+ for ( var i = 0 ; i < arguments . length ; i ++ ) {
26+ callRecorder [ i ] = arguments [ i ] ;
27+ }
28+ functionCalls . push ( callRecorder ) ;
29+ return returnObject ;
30+ } ;
31+ } ;
32+
33+ var experiment ;
34+ var folder = '/new-experiment' ;
35+ var description = 'My new experiment' ;
36+ var edit_url = 'http://www.example.com' ;
37+ var project_id = 154321 ;
38+ var directories = { } ;
39+ var experimentData = {
40+ description : description ,
41+ edit_url : edit_url
42+ } ;
43+ quickTemp . makeOrRemake ( directories , 'project' ) ;
44+ var experimentPath = directories . project + folder ;
45+
46+ describe ( 'Experiment Object' , function ( ) {
47+ before ( function ( done ) {
48+ experiment = Experiment . create ( {
49+ description : description ,
50+ edit_url : edit_url
51+ } , experimentPath ) ;
52+ fs . writeFile ( directories . project + '/project.json' , '{"id":"' + project_id + '","include_jquery":"false","project_name":"my project"}' , function ( err ) {
53+ if ( ! err ) {
54+ done ( )
55+ } else {
56+ done ( err )
57+ }
58+ } ) ;
59+ } ) ;
60+ after ( function ( done ) {
61+ quickTemp . remove ( directories , 'experiment' ) ;
62+ done ( ) ;
63+ } )
64+ describe ( '#create()' , function ( ) {
65+ it ( 'Should create an experiment directory' , function ( done ) {
66+ fs . exists ( experimentPath , function ( exists ) {
67+ expect ( exists ) . to . be . true ;
68+ } ) ;
69+ done ( ) ;
70+ } ) ;
71+ it ( 'Should create a global.css file' , function ( done ) {
72+ fs . exists ( experimentPath + '/global.css' , function ( exists ) {
73+ expect ( exists ) . to . be . true ;
74+ done ( ) ;
75+ } )
76+ } ) ;
77+ it ( 'Should create a global.js file' , function ( done ) {
78+ fs . exists ( experimentPath + '/global.js' , function ( exists ) {
79+ expect ( exists ) . to . be . true ;
80+ done ( ) ;
81+ } )
82+ } ) ;
83+ it ( 'Should create an experiment.json file' , function ( done ) {
84+ fs . readFile ( experimentPath + '/experiment.json' , function ( err , data ) {
85+ expect ( err ) . to . be . null ;
86+ expect ( JSON . parse ( data ) ) . to . deep . equal ( {
87+ 'description' : description ,
88+ 'edit_url' : edit_url
89+ } ) ;
90+ done ( ) ;
91+ } ) ;
92+ } ) ;
93+ it ( 'Should return an experiment object' , function ( ) {
94+ expect ( experiment ) . to . be . an . instanceOf ( Experiment ) ;
95+ } ) ;
96+ } ) ;
97+ describe ( '#locateAndLoad()' , function ( ) {
98+ var experiment ;
99+ before ( function ( done ) {
100+ experiment = Experiment . locateAndLoad ( experimentPath ) ;
101+ done ( ) ;
102+ } ) ;
103+ after ( function ( done ) {
104+ done ( ) ;
105+ } )
106+ it ( 'Should locate the experiment.json' , function ( ) {
107+ expect ( experiment ) . to . be . an . instanceOf ( Experiment ) ;
108+ } ) ;
109+ it ( 'Should load the experiment.json' , function ( ) {
110+ expect ( experiment . attributes ) . to . deep . equal ( experimentData ) ;
111+ } ) ;
112+ } ) ;
113+ describe ( '#getJSPath()' , function ( ) {
114+ it ( 'Should return the JS Path' , function ( ) {
115+ var experiment = Experiment . locateAndLoad ( experimentPath ) ;
116+ expect ( experiment . getJSPath ( ) ) . to . equal ( experimentPath + '/global.js' ) ;
117+ } )
118+ } ) ;
119+ describe ( '#getCSSPath()' , function ( ) {
120+ it ( 'Should return the CSS Path' , function ( ) {
121+ var experiment = Experiment . locateAndLoad ( experimentPath ) ;
122+ expect ( experiment . getCSSPath ( ) ) . to . equal ( experimentPath + '/global.css' ) ;
123+ } )
124+ } ) ;
125+ describe ( '#getCSS()' , function ( ) {
126+ it ( 'Should return global.css contents' , function ( done ) {
127+ var mockCSS = '.my-class {background: white;}' ;
128+ fs . writeFile ( experimentPath + '/global.css' , mockCSS , function ( err , data ) {
129+ var experiment = Experiment . locateAndLoad ( experimentPath ) ;
130+ expect ( experiment . getCSS ( ) ) . to . equal ( mockCSS ) ;
131+ done ( ) ;
132+ } ) ;
133+ } ) ;
134+ } ) ;
135+ describe ( '#getJS()' , function ( ) {
136+ it ( 'Should return global.js contents' , function ( done ) {
137+ var mockJS = '$(\'body\').addClass(\'my-class\');' ;
138+ fs . writeFile ( experimentPath + '/global.js' , mockJS , function ( err , data ) {
139+ expect ( err ) . to . be . null ;
140+ var experiment = Experiment . locateAndLoad ( experimentPath ) ;
141+ expect ( experiment . getJS ( ) ) . to . equal ( mockJS ) ;
142+ done ( ) ;
143+ } ) ;
144+ } ) ;
145+ } ) ;
146+ describe ( '#getVariations()' , function ( ) {
147+ before ( function ( done ) {
148+ variationOne = experimentPath + '/variation1' ;
149+ variationTwo = experimentPath + '/variation2' ;
150+ Variation . create ( {
151+ description : description ,
152+ } , variationOne ) ;
153+ Variation . create ( {
154+ description : description ,
155+ } , variationTwo ) ;
156+ done ( ) ;
157+ } )
158+ it ( 'Should return an array with location of variations' , function ( ) {
159+ var experiment = Experiment . locateAndLoad ( experimentPath ) ;
160+ expect ( experiment . getVariations ( ) ) . to . deep . equal ( [ variationOne + '/variation.json' , variationTwo + '/variation.json' ] ) ;
161+ } )
162+ } ) ;
163+ describe ( '#createRemote()' , function ( ) {
164+ it ( 'Should create a remote experiment' , function ( ) {
165+
166+ functionCalls = [ ] ;
167+ var experiment = Experiment . locateAndLoad ( experimentPath ) ;
168+ var client = {
169+ createExperiment : recordFunctionCalls ( 'updateExperiment' ,
170+ { then : recordFunctionCalls ( 'then' ,
171+ {
172+ catch : recordFunctionCalls ( 'catch' )
173+ } )
174+ } )
175+ }
176+ var expArgs = _ . clone ( experiment . attributes ) ;
177+ expArgs [ 'custom_css' ] = experiment . getCSS ( ) ;
178+ expArgs [ 'custom_js' ] = experiment . getJS ( ) ;
179+ expArgs [ 'project_id' ] = String ( project_id ) ;
180+
181+ experiment . createRemote ( client ) ;
182+ expect ( functionCalls [ 0 ] ) . to . have . a . property ( 'functionName' , 'updateExperiment' ) ;
183+ expect ( functionCalls [ 0 ] [ 0 ] ) . to . deep . equal ( expArgs ) ;
184+ } ) ;
185+ } ) ;
186+ describe ( '#updateRemote()' , function ( ) {
187+ it ( 'Should update a remote experiment' , function ( ) {
188+ functionCalls = [ ] ;
189+ var experiment = Experiment . locateAndLoad ( experimentPath ) ;
190+ var client = {
191+ updateExperiment : recordFunctionCalls ( 'updateExperiment' ,
192+ { then : recordFunctionCalls ( 'then' ,
193+ {
194+ catch : recordFunctionCalls ( 'catch' )
195+ } )
196+ } )
197+ } ;
198+ var expArgs = _ . clone ( experiment . attributes ) ;
199+ expArgs [ 'custom_css' ] = experiment . getCSS ( ) ;
200+ expArgs [ 'custom_js' ] = experiment . getJS ( ) ;
201+
202+ experiment . updateRemote ( client ) ;
203+ expect ( functionCalls [ 0 ] ) . to . have . a . property ( 'functionName' , 'updateExperiment' ) ;
204+ expect ( functionCalls [ 0 ] [ 0 ] ) . to . deep . equal ( expArgs ) ;
205+ } ) ;
206+ } ) ;
207+ } ) ;
0 commit comments