1
- import { expect } from 'chai' ;
2
1
import sinon from 'sinon' ;
3
-
4
2
import { applyMiddleware , createStore } from 'redux' ;
5
3
6
- import { repeat } from 'helpers' ;
7
4
import logger , { createLogger } from '../src' ;
8
5
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
-
17
6
context ( `default logger` , ( ) => {
18
7
describe ( `init` , ( ) => {
19
8
beforeEach ( ( ) => {
@@ -34,27 +23,54 @@ context(`default logger`, () => {
34
23
} ) ;
35
24
36
25
context ( `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' , ( ) => {
38
39
beforeEach ( ( ) => {
39
- sinon . spy ( console , `error` ) ;
40
+ store = createStore ( ( ) => ( { } ) , applyMiddleware ( createLogger ) ) ;
40
41
} ) ;
41
42
42
- afterEach ( ( ) => {
43
- console . error . restore ( ) ;
43
+ it ( 'should log error' , ( ) => {
44
+ sinon . assert . calledOnce ( console . error ) ;
44
45
} ) ;
45
46
46
- it ( `should throw error if passed direct to applyMiddleware` , ( ) => {
47
- const store = createStore ( ( ) => ( { } ) , applyMiddleware ( createLogger ) ) ;
48
-
47
+ it ( 'should create an empty middleware' , ( ) => {
49
48
store . dispatch ( { type : `foo` } ) ;
50
- sinon . assert . calledOnce ( console . error ) ;
49
+ sinon . assert . notCalled ( console . log ) ;
51
50
} ) ;
51
+ } ) ;
52
52
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
+ } ) ;
55
58
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 ) ;
58
74
} ) ;
59
75
} ) ;
60
76
} ) ;
0 commit comments