1- import { expect } from 'chai' ;
21import sinon from 'sinon' ;
3-
42import { applyMiddleware , createStore } from 'redux' ;
53
6- import { repeat } from 'helpers' ;
74import logger , { createLogger } from '../src' ;
85
9- context ( `Helpers` , ( ) => {
10- describe ( `repeat` , ( ) => {
11- it ( `should repeat a string the number of indicated times` , ( ) => {
12- expect ( repeat ( `teacher` , 3 ) ) . to . equal ( `teacherteacherteacher` ) ;
13- } ) ;
14- } ) ;
15- } ) ;
16-
176context ( `default logger` , ( ) => {
187 describe ( `init` , ( ) => {
198 beforeEach ( ( ) => {
@@ -34,27 +23,54 @@ context(`default logger`, () => {
3423} ) ;
3524
3625context ( `createLogger` , ( ) => {
37- describe ( `init` , ( ) => {
26+ beforeEach ( ( ) => {
27+ sinon . spy ( console , `error` ) ;
28+ sinon . spy ( console , 'log' ) ;
29+ } ) ;
30+
31+ afterEach ( ( ) => {
32+ console . error . restore ( ) ;
33+ console . log . restore ( ) ;
34+ } ) ;
35+
36+ let store ;
37+
38+ context ( 'mistakenly passed directly to applyMiddleware' , ( ) => {
3839 beforeEach ( ( ) => {
39- sinon . spy ( console , `error` ) ;
40+ store = createStore ( ( ) => ( { } ) , applyMiddleware ( createLogger ) ) ;
4041 } ) ;
4142
42- afterEach ( ( ) => {
43- console . error . restore ( ) ;
43+ it ( 'should log error' , ( ) => {
44+ sinon . assert . calledOnce ( console . error ) ;
4445 } ) ;
4546
46- it ( `should throw error if passed direct to applyMiddleware` , ( ) => {
47- const store = createStore ( ( ) => ( { } ) , applyMiddleware ( createLogger ) ) ;
48-
47+ it ( 'should create an empty middleware' , ( ) => {
4948 store . dispatch ( { type : `foo` } ) ;
50- sinon . assert . calledOnce ( console . error ) ;
49+ sinon . assert . notCalled ( console . log ) ;
5150 } ) ;
51+ } ) ;
5252
53- it ( `should be ok` , ( ) => {
54- const store = createStore ( ( ) => ( { } ) , applyMiddleware ( createLogger ( ) ) ) ;
53+ context ( 'options.logger undefined or null' , ( ) => {
54+ beforeEach ( ( ) => {
55+ const logger = createLogger ( { logger : null } ) ;
56+ store = createStore ( ( ) => ( { } ) , applyMiddleware ( logger ) ) ;
57+ } ) ;
5558
56- store . dispatch ( { type : `foo` } ) ;
57- sinon . assert . notCalled ( console . error ) ;
59+ it ( 'should create an empty middleware' , ( ) => {
60+ store . dispatch ( { type : 'foo' } ) ;
61+ sinon . assert . notCalled ( console . log ) ;
62+ } ) ;
63+ } ) ;
64+
65+ context ( 'options.predicate returns false' , ( ) => {
66+ beforeEach ( ( ) => {
67+ const logger = createLogger ( { predicate : ( ) => false } ) ;
68+ store = createStore ( ( ) => ( { } ) , applyMiddleware ( logger ) ) ;
69+ } ) ;
70+
71+ it ( 'should not log' , ( ) => {
72+ store . dispatch ( { type : 'foo' } ) ;
73+ sinon . assert . notCalled ( console . log ) ;
5874 } ) ;
5975 } ) ;
6076} ) ;
0 commit comments