1+ import * as chai from 'chai' ;
2+ import * as spies from 'chai-spies' ;
3+ import * as debuggo from '../dist/index' ;
4+
5+ chai . should ( ) ;
6+ chai . use ( spies ) ;
7+
8+ describe ( 'debuggo' , function ( ) {
9+
10+ describe ( 'createLogger' , function ( ) {
11+
12+ it ( 'create a logger with no context' , function ( ) {
13+ let l = debuggo . createLogger ( 'test' ) ;
14+ l . log . should . be . a ( 'function' ) ;
15+ l . info . should . be . a ( 'function' ) ;
16+ l . warn . should . be . a ( 'function' ) ;
17+ l . error . should . be . a ( 'function' ) ;
18+ l . debug . should . be . a ( 'function' ) ;
19+ l . trace . should . be . a ( 'function' ) ;
20+ } ) ;
21+
22+ it ( 'create a logger with a context' , function ( ) {
23+ let l = debuggo . createLogger ( 'test' , 'bbb' ) ;
24+ l . log . should . be . a ( 'function' ) ;
25+ l . info . should . be . a ( 'function' ) ;
26+ l . warn . should . be . a ( 'function' ) ;
27+ l . error . should . be . a ( 'function' ) ;
28+ l . debug . should . be . a ( 'function' ) ;
29+ l . trace . should . be . a ( 'function' ) ;
30+ } ) ;
31+
32+ it ( 'use console.log to log when available' , function ( ) {
33+ let w = {
34+ console : {
35+ info : chai . spy ( ) ,
36+ warn : chai . spy ( ) ,
37+ error : chai . spy ( )
38+ } as any
39+ } ;
40+ ( < any > global ) . window = w ;
41+ let l1 = debuggo . createLogger ( 'test' ) ;
42+ l1 . info ( 'aaa' ) ;
43+ w . console . info . should . have . been . called . once ;
44+
45+ w . console . debug = chai . spy ( ) ;
46+ let l2 = debuggo . createLogger ( 'test' , 'ccc' ) ;
47+ l2 . debug ( 'aaa' ) ;
48+ l2 . info ( 'aaa' ) ;
49+ l2 . info . log ;
50+ w . console . debug . should . have . been . called . once ;
51+ w . console . info . should . have . been . called . twice ;
52+ } ) ;
53+
54+ } ) ;
55+
56+ describe ( 'namespaces' , function ( ) {
57+
58+ it ( 'should return the namespaces' , function ( ) {
59+ debuggo . namespaces ( ) . should . deep . equal ( [ 'test' ] ) ;
60+ debuggo . createLogger ( 'test2' ) ;
61+ debuggo . namespaces ( ) . should . deep . equal ( [ 'test' , 'test2' ] ) ;
62+ } )
63+
64+ } ) ;
65+
66+ describe ( 'cb' , function ( ) {
67+
68+ it ( 'should return a logging callback' , function ( ) {
69+ let w = {
70+ console : {
71+ info : chai . spy ( ) ,
72+ warn : chai . spy ( ) ,
73+ error : chai . spy ( )
74+ } as any
75+ } ;
76+ ( < any > global ) . window = w ;
77+ let f = debuggo . cb ( 'test' ) ;
78+ f . should . be . a ( 'function' ) ;
79+ f ( null , 'data' ) ;
80+ w . console . info . should . have . been . called . once ;
81+ f ( 'error' ) ;
82+ w . console . error . should . have . been . called . once ;
83+
84+ let f2 = debuggo . cb ( ) ;
85+ f2 . should . be . a ( 'function' ) ;
86+ f2 ( null , 'data' ) ;
87+ w . console . info . should . have . been . called . once ;
88+
89+ } ) ;
90+
91+ } ) ;
92+
93+ describe ( 'promise' , function ( ) {
94+
95+ it ( 'should log a promise' , function ( ) {
96+ let w = {
97+ console : {
98+ info : chai . spy ( ) ,
99+ warn : chai . spy ( ) ,
100+ error : chai . spy ( )
101+ } as any
102+ } ;
103+ ( < any > global ) . window = w ;
104+ return debuggo . promise ( Promise . resolve ( true ) , 'test' ) . then ( ( ) => {
105+ w . console . info . should . have . been . called . once ;
106+ return debuggo . promise ( Promise . reject ( true ) , 'test' ) . then ( ( ) => {
107+ w . console . error . should . have . been . called . once ;
108+ return debuggo . promise ( Promise . reject ( true ) ) . then ( ( ) => {
109+ w . console . error . should . have . been . called . once ;
110+ } ) ;
111+ } ) ;
112+ } ) ;
113+ } )
114+
115+ } ) ;
116+
117+
118+ } ) ;
0 commit comments