11var fs = require ( 'fs' ) ;
2- var expect = require ( 'chai' ) . expect ;
2+ var chai = require ( 'chai' ) ;
3+ chai . config . truncateThreshold = 0 ; // disable truncating
4+ var expect = chai . expect ;
35var proxyquire = require ( 'proxyquire' ) ;
46var 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' ) ;
511var functionCalls = [ ] ;
6-
712/**
813 * Used to push any function calls and their
914 * arguments to the funcitonCalls array. We
@@ -25,88 +30,178 @@ var recordFunctionCalls = function(functionName, returnObject){
2530 } ;
2631} ;
2732
28- var directories , experimentData , ExperimentTwo , experiment ;
29- var folder = 'new-experiment' ;
33+ var experiment ;
34+ var folder = '/ new-experiment' ;
3035var description = 'My new experiment' ;
3136var edit_url = 'http://www.example.com' ;
32-
33- var createExperimentWithDefaults = function ( ) {
34- directories = { }
35- quickTemp . makeOrRemake ( directories , 'experiment' ) ;
36- experimentData = {
37- description : description ,
38- edit_url : edit_url
39- } ;
40- ExperimentTwo = require ( '../lib/experiment.js' ) ;
41- ExperimentTwo . create ( experimentData , directories . experiment ) ;
37+ var project_id = 154321 ;
38+ var directories = { } ;
39+ var experimentData = {
40+ description : description ,
41+ edit_url : edit_url
4242} ;
43+ quickTemp . makeOrRemake ( directories , 'project' ) ;
44+ var experimentPath = directories . project + folder ;
4345
44-
45- describe ( 'Experiment' , function ( ) {
46-
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+ } )
4764 describe ( '#create()' , function ( ) {
48- var Experiment ;
49- before ( function ( done ) {
50- var fileUtil = {
51- writeDir : recordFunctionCalls ( 'writeDir' ) ,
52- writeText : recordFunctionCalls ( 'writeText' ) ,
53- writeJSON : recordFunctionCalls ( 'writeJSON' )
54- } ;
55- var optcliBase = require ( '../lib/optcli-base.js' ) ;
56- optcliBase . prototype
57- Experiment = proxyquire ( '../lib/experiment.js' , {
58- './file-util' : fileUtil
65+ it ( 'Should create an experiment directory' , function ( done ) {
66+ fs . exists ( experimentPath , function ( exists ) {
67+ expect ( exists ) . to . be . true ;
5968 } ) ;
60- functionCalls = [ ] ;
61- experiment = Experiment . create ( {
62- description : description ,
63- edit_url : edit_url
64- } , folder ) ;
6569 done ( ) ;
6670 } ) ;
67- after ( function ( ) {
68- console . log ( functionCalls . length ) ;
69- } )
70- it ( 'Should create an experiment directory' , function ( ) {
71- expect ( functionCalls [ 0 ] ) . to . deep . equal ( { 'functionName' :'writeDir' , 0 : folder } ) ;
72- } ) ;
73- it ( 'Should create a global.css file' , function ( ) {
74- expect ( functionCalls [ 1 ] ) . to . deep . equal ( { 'functionName' :'writeText' , 0 : folder + '/global.css' } ) ;
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+ } )
7576 } ) ;
76- it ( 'Should create a global.js file' , function ( ) {
77- expect ( functionCalls [ 2 ] ) . to . deep . equal ( { 'functionName' :'writeText' , 0 : folder + '/global.js' } ) ;
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+ } )
7882 } ) ;
79- it ( 'Should create an experiment.json file' , function ( ) {
80- expect ( functionCalls [ 3 ] ) . to . deep . equal ( {
81- 'functionName' :'writeJSON' ,
82- 0 : folder + '/experiment.json' ,
83- 1 : {
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 ( {
8487 'description' : description ,
8588 'edit_url' : edit_url
86- }
89+ } ) ;
90+ done ( ) ;
8791 } ) ;
8892 } ) ;
8993 it ( 'Should return an experiment object' , function ( ) {
9094 expect ( experiment ) . to . be . an . instanceOf ( Experiment ) ;
9195 } ) ;
9296 } ) ;
9397 describe ( '#locateAndLoad()' , function ( ) {
98+ var experiment ;
9499 before ( function ( done ) {
95- createExperimentWithDefaults ( ) ;
96- console . log ( functionCalls . length ) ;
97- experiment = ExperimentTwo . locateAndLoad ( directories . experiment ) ;
100+ experiment = Experiment . locateAndLoad ( experimentPath ) ;
98101 done ( ) ;
99102 } ) ;
100103 after ( function ( done ) {
101- quickTemp . remove ( directories , 'experiment' ) ;
102104 done ( ) ;
103105 } )
104106 it ( 'Should locate the experiment.json' , function ( ) {
105- // expect(experiment).to.be.an.instanceOf(Experiment);
107+ expect ( experiment ) . to . be . an . instanceOf ( Experiment ) ;
106108 } ) ;
107109 it ( 'Should load the experiment.json' , function ( ) {
108- //expect(experiment.attributes).to.deep.equal(experimentData);
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 ) ;
109205 } ) ;
110206 } ) ;
111- describe ( '#getCSS' , function ( ) { } )
112207} ) ;
0 commit comments