11"use strict" ;
22
3- require ( "../base.spec" ) ;
3+ const chalk = require ( "chalk" ) ;
4+ const blessed = require ( "blessed" ) ;
45
6+ const base = require ( "../base.spec" ) ;
57const Dashboard = require ( "../../dashboard" ) ;
68
9+ const mockSetItems = ( ) => {
10+ // Override ListBar fakes from what we do in `base.spec.js`.
11+ // Note that these are **already** stubbed. We're not monkey-patching blessed.
12+ blessed . listbar . returns ( {
13+ selected : "selected" ,
14+ setLabel : base . sandbox . spy ( ) ,
15+ selectTab : base . sandbox . spy ( ) ,
16+ setItems : base . sandbox . stub ( ) . callsFake ( obj => {
17+ // Naively simulate what setItems would do calling each object key.
18+ Object . keys ( obj ) . forEach ( key => obj [ key ] ( ) ) ;
19+ } )
20+ } ) ;
21+ } ;
22+
723describe ( "dashboard" , ( ) => {
824 const options = {
925 color : "red" ,
@@ -25,4 +41,273 @@ describe("dashboard", () => {
2541 expect ( dashboardWithOptions . color ) . to . equal ( "red" ) ;
2642 expect ( dashboardWithOptions . minimal ) . to . be . true ;
2743 } ) ;
44+
45+ describe ( "set* methods" , ( ) => {
46+ let dashboard ;
47+
48+ beforeEach ( ( ) => {
49+ dashboard = new Dashboard ( ) ;
50+ } ) ;
51+
52+ describe ( "setData" , ( ) => {
53+ const dataArray = [ {
54+ type : "progress" ,
55+ value : 0.57
56+ } , {
57+ type : "operations" ,
58+ value : "IDLE"
59+ } ] ;
60+
61+ it ( "can setData" , ( ) => {
62+ expect ( ( ) => dashboard . setData ( dataArray ) ) . to . not . throw ;
63+ } ) ;
64+ } ) ;
65+
66+ describe ( "setOperations" , ( ) => {
67+ const data = {
68+ value : "IDLE"
69+ } ;
70+
71+ it ( "can setOperations" , ( ) => {
72+ expect ( ( ) => dashboard . setOperations ( data ) ) . to . not . throw ;
73+
74+ dashboard . setOperations ( data ) ;
75+ expect ( dashboard . operations . setContent ) . to . have . been . calledWith ( data . value ) ;
76+ } ) ;
77+ } ) ;
78+
79+ describe ( "setStatus" , ( ) => {
80+ const data = {
81+ value : "Success"
82+ } ;
83+
84+ it ( "can setStatus" , ( ) => {
85+ expect ( ( ) => dashboard . setStatus ( data ) ) . to . not . throw ;
86+
87+ dashboard . setStatus ( data ) ;
88+ expect ( dashboard . status . setContent )
89+ . to . have . been . calledWith ( `{green-fg}{bold}${ data . value } {/}` ) ;
90+ } ) ;
91+
92+ it ( "should display a failed status on build failure" , ( ) => {
93+ data . value = "Failed" ;
94+ expect ( ( ) => dashboard . setStatus ( data ) ) . to . not . throw ;
95+
96+ dashboard . setStatus ( data ) ;
97+ expect ( dashboard . status . setContent )
98+ . to . have . been . calledWith ( `{red-fg}{bold}${ data . value } {/}` ) ;
99+ } ) ;
100+
101+ it ( "should display any other status string without coloring" , ( ) => {
102+ data . value = "Unknown" ;
103+ expect ( ( ) => dashboard . setStatus ( data ) ) . to . not . throw ;
104+
105+ dashboard . setStatus ( data ) ;
106+ expect ( dashboard . status . setContent )
107+ . to . have . been . calledWith ( `{bold}${ data . value } {/}` ) ;
108+ } ) ;
109+ } ) ;
110+
111+ describe ( "setProgress" , ( ) => {
112+ const data = {
113+ value : 0.57
114+ } ;
115+
116+ it ( "can setProgress" , ( ) => {
117+ expect ( ( ) => dashboard . setProgress ( data ) ) . to . not . throw ;
118+
119+ dashboard . setProgress ( data ) ;
120+ expect ( dashboard . progressbar . setProgress ) . to . have . been . calledOnce ;
121+ expect ( dashboard . progressbar . setContent ) . to . have . been . called ;
122+ } ) ;
123+
124+ it ( `should call progressbar.setProgress twice if not in minimal mode
125+ and percent is falsy` , ( ) => {
126+ data . value = null ;
127+ expect ( ( ) => dashboard . setProgress ( data ) ) . to . not . throw ;
128+
129+ dashboard . setProgress ( data ) ;
130+ expect ( dashboard . progressbar . setProgress ) . to . have . been . calledTwice ;
131+ } ) ;
132+ } ) ;
133+
134+ describe ( "setStats" , ( ) => {
135+ const data = {
136+ value : {
137+ errors : null ,
138+ data : {
139+ errors : [ ] ,
140+ warnings : [ ]
141+ }
142+ }
143+ } ;
144+
145+ it ( "can setStats" , ( ) => {
146+ expect ( ( ) => dashboard . setStats ( data ) ) . not . to . throw ;
147+
148+ dashboard . setStats ( data ) ;
149+ expect ( dashboard . logText . log ) . to . have . been . called ;
150+ expect ( dashboard . modulesMenu . setLabel )
151+ . to . have . been . calledWith ( chalk . yellow ( "Modules (loading...)" ) ) ;
152+ expect ( dashboard . assets . setLabel )
153+ . to . have . been . calledWith ( chalk . yellow ( "Assets (loading...)" ) ) ;
154+ expect ( dashboard . problemsMenu . setLabel )
155+ . to . have . been . calledWith ( chalk . yellow ( "Problems (loading...)" ) ) ;
156+ } ) ;
157+
158+ it ( "should display stats errors if present" , ( ) => {
159+ data . value . errors = [ "error" ] ;
160+ expect ( ( ) => dashboard . setStats ( data ) ) . not . to . throw ;
161+
162+ dashboard . setStats ( data ) ;
163+ expect ( dashboard . status . setContent )
164+ . to . have . been . calledWith ( "{red-fg}{bold}Failed{/}" ) ;
165+ } ) ;
166+ } ) ;
167+
168+ describe ( "setSizes" , ( ) => {
169+ const data = {
170+ value : {
171+ assets : {
172+ foo : {
173+ meta : {
174+ full : 456
175+ } ,
176+ files : [ {
177+ size : {
178+ full : 123
179+ } ,
180+ fileName : "test.js" ,
181+ baseName : "/home/bar/test.js"
182+ } ]
183+ } ,
184+ bar : {
185+ meta : {
186+ full : 123
187+ } ,
188+ files : [ ]
189+ }
190+ }
191+ }
192+ } ;
193+
194+ const formattedData = [
195+ [ "Name" , "Size" ] ,
196+ [ "foo" , "456 B" ] ,
197+ [ "bar" , "123 B" ] ,
198+ [ "Total" , "579 B" ]
199+ ] ;
200+
201+ it ( "can setSizes" , ( ) => {
202+ expect ( ( ) => dashboard . setSizes ( data ) ) . to . not . throw ;
203+
204+ dashboard . setSizes ( data ) ;
205+ expect ( dashboard . assets . setLabel ) . to . have . been . calledWith ( "Assets" ) ;
206+ expect ( dashboard . assetTable . setData ) . to . have . been . calledWith ( formattedData ) ;
207+ expect ( dashboard . modulesMenu . setLabel ) . to . have . been . calledWith ( "Modules" ) ;
208+ expect ( dashboard . modulesMenu . setItems ) . to . have . been . called ;
209+ expect ( dashboard . modulesMenu . selectTab )
210+ . to . have . been . calledWith ( dashboard . modulesMenu . selected ) ;
211+ expect ( dashboard . screen . render ) . to . have . been . called ;
212+ } ) ;
213+
214+ it ( "should call formatModules" , ( ) => {
215+ // Mock out the call to setItems to force call of formatModules.
216+ mockSetItems ( ) ;
217+ // Discard generic dashboard, create a new one with adjusted mocks.
218+ dashboard = new Dashboard ( ) ;
219+ expect ( ( ) => dashboard . setSizes ( data ) ) . to . not . throw ;
220+ } ) ;
221+ } ) ;
222+
223+ describe ( "setSizesError" , ( ) => {
224+ const err = "error" ;
225+
226+ it ( "can setSizesError" , ( ) => {
227+ expect ( ( ) => dashboard . setSizesError ( err ) ) . to . not . throw ;
228+
229+ dashboard . setSizesError ( err ) ;
230+ expect ( dashboard . modulesMenu . setLabel )
231+ . to . have . been . calledWith ( chalk . red ( "Modules (error)" ) ) ;
232+ expect ( dashboard . assets . setLabel ) . to . have . been . calledWith ( chalk . red ( "Assets (error)" ) ) ;
233+ expect ( dashboard . logText . log )
234+ . to . have . been . calledWith ( chalk . red ( "Could not load module/asset sizes." ) ) ;
235+ expect ( dashboard . logText . log ) . to . have . been . calledWith ( chalk . red ( err ) ) ;
236+ } ) ;
237+ } ) ;
238+
239+ describe ( "setProblems" , ( ) => {
240+ const data = {
241+ value : {
242+ duplicates : {
243+ assets : {
244+ foo : "foo" ,
245+ bar : "bar"
246+ }
247+ } ,
248+ versions : {
249+ assets : {
250+ foo : "1.2.3" ,
251+ bar : "3.2.1"
252+ }
253+ }
254+ }
255+ } ;
256+
257+ it ( "can setProblems" , ( ) => {
258+ expect ( ( ) => dashboard . setProblems ( data ) ) . to . not . throw ;
259+
260+ dashboard . setProblems ( data ) ;
261+ expect ( dashboard . problemsMenu . setLabel ) . to . have . been . calledWith ( "Problems" ) ;
262+ expect ( dashboard . problemsMenu . setItems ) . to . have . been . called ;
263+ expect ( dashboard . problemsMenu . selectTab )
264+ . to . have . been . calledWith ( dashboard . problemsMenu . selected ) ;
265+ expect ( dashboard . screen . render ) . to . have . been . called ;
266+ } ) ;
267+
268+ it ( "should call formatProblems" , ( ) => {
269+ // Mock out the call to setItems to force call of formatProblems.
270+ mockSetItems ( ) ;
271+ // Discard generic dashboard, create a new one with adjusted mocks.
272+
273+ dashboard = new Dashboard ( ) ;
274+ expect ( ( ) => dashboard . setProblems ( data ) ) . to . not . throw ;
275+ } ) ;
276+ } ) ;
277+
278+ describe ( "setProblemsError" , ( ) => {
279+ const err = { stack : "stack" } ;
280+
281+ it ( "can setProblemsError" , ( ) => {
282+ expect ( ( ) => dashboard . setProblemsError ( err ) ) . to . not . throw ;
283+
284+ dashboard . setProblemsError ( err ) ;
285+ expect ( dashboard . problemsMenu . setLabel )
286+ . to . have . been . calledWith ( chalk . red ( "Problems (error)" ) ) ;
287+ expect ( dashboard . logText . log )
288+ . to . have . been . calledWith ( chalk . red ( "Could not analyze bundle problems." ) ) ;
289+ expect ( dashboard . logText . log ) . to . have . been . calledWith ( chalk . red ( err . stack ) ) ;
290+ } ) ;
291+ } ) ;
292+
293+ describe ( "setLog" , ( ) => {
294+ const data = { value : "[{ log: 'log' }]" } ;
295+
296+ it ( "can setLog" , ( ) => {
297+ expect ( ( ) => dashboard . setLog ( data ) ) . not . to . throw ;
298+
299+ dashboard . setLog ( data ) ;
300+ expect ( dashboard . logText . log ) . to . have . been . calledWith ( "[ log: 'log' ]" ) ;
301+ } ) ;
302+
303+ it ( "should return early if the stats object has errors" , ( ) => {
304+ dashboard . stats = { } ;
305+ dashboard . stats . hasErrors = ( ) => true ;
306+ expect ( dashboard . setLog ( data ) ) . to . be . undefined ;
307+
308+ dashboard . setLog ( data ) ;
309+ expect ( dashboard . logText . log ) . to . not . have . been . called ;
310+ } ) ;
311+ } ) ;
312+ } ) ;
28313} ) ;
0 commit comments