File size: 4,859 Bytes
c02f046
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { assert, } from 'chai';
import redeuceur from '../../src';

const handlerErr = 
  `Invalid handler. Every handler must have a condition and a nextState.`;
const conditionErr = 
  `Invalid handler condition. ` + 
  `A condition must be a string, an array of strings, or a function.`;

describe(`unit > redeuceur`, function () {
  let actionBar;
  let actionBaz;
  let actionFoo;
  let actionQux;
  let actionTypes;
  let initialState;

  before(function () {
    actionTypes = {
      FOO: `FOO`,
      BAR: `BAR`,
      BAZ: `BAZ`,
      QUX: `QUX`,
    };

    const qux = `qux`;

    actionFoo = { type: actionTypes.FOO, qux, };
    actionBar = { type: actionTypes.BAR, qux, };
    actionBaz = { type: actionTypes.BAZ, qux, };

    initialState = null;
  });

  it(`Throws a TypeError if a handler is not an array.`, function () {
    const invalid = [ true, 1, `a`, {}, () => {}, null, ];

    invalid.forEach(x => {
      assert.throws(() => { redeuceur(true, x ); }, Error, handlerErr);

      assert.throws(() => { 
        const handlers = [ [ `foo`, true, ], x, [ `bar`, false, ], ];

        redeuceur(true, ...handlers);
      }, Error, handlerErr);
    });
  });

  it(`Throws an Error if a handler does not have a condition and a nextState.`, function () {
    assert.throws(() => { redeuceur(true, []); }, Error, handlerErr);
    assert.throws(() => { redeuceur(true, [ [], ]); }, Error, handlerErr);
    assert.throws(() => { redeuceur(true, [ [ `a`, ], ]); }, Error, handlerErr);
  });

  it(`Throws a TypeError if a condition is not a valid type.`, function () {
    const nextState = () => ({});
    const invalid = [ true, 1, {}, null, ];

    invalid.forEach(x => {
      assert.throws(() => { 
        redeuceur(true, [ x, nextState, ]);
      }, TypeError, conditionErr);
    });
  });

  it(`Returns a reducer function.`, function () {
    assert.isFunction(redeuceur(true, [ `x`, true, ]));
  });

  it(`Reducer returns the initial state if called without an action argument.`, function () {
    const handler = [ actionTypes.FOO, true, ];
    const reducer = redeuceur(initialState, handler);
  
    assert.deepEqual(reducer(), initialState);
  });

  it(`Reducer matches a string condition against the action type.`, function () {
    const handler = [ 
      actionTypes.FOO, 
      (state, action) => action.qux, 
    ];

    const reducer = redeuceur(initialState, handler);

    assert.strictEqual(actionFoo.qux, reducer(initialState, actionFoo));
  });

  it(`Reducer matches an array of string conditions against the action type.`, function () {
    const handler = [
      [ actionTypes.BAR, actionTypes.BAZ, ],
      (state, action) => action.qux,
    ];
    const reducer = redeuceur(initialState, handler);

    assert.strictEqual(reducer(initialState, actionBar), actionBar.qux);
    assert.strictEqual(reducer(initialState, actionBaz), actionBaz.qux);
    assert.strictEqual(reducer(initialState, actionQux), initialState);
  });

  it(`Reducer returns nextState if the condition function returns a truthy value.`, function () {
    const handler = [
      (state, action) => action.type === actionTypes.FOO,
      (state, action) => action.qux,
    ];

    const reducer = redeuceur(initialState, handler);
    
    assert.strictEqual(reducer(initialState, actionFoo), actionFoo.qux);
  });

  it(`Reducer does not return nextState if the condition function returns a falsey value.`, function () {
    const handler = [
      (state, action) => action.type === actionTypes.BAR,
      (state, action) => action.qux,
    ];

    const reducer = redeuceur(initialState, handler);
    
    assert.strictEqual(reducer(initialState, actionFoo), initialState);
  });
  
  it(`Reducer returns the result of calling nextState if nextState is a function.`, function () {
    const handler = [
      actionTypes.FOO,
      (state, action) => action.qux,
    ];

    const reducer = redeuceur(initialState, handler);

    assert.strictEqual(reducer(initialState, actionFoo), `qux`);
  });

  it(`Reducer returns the nextState value if nextState is not a function.`, function () {
    const nextState = `foo bar baz`;

    const handler = [
      actionTypes.FOO, 
      nextState,
    ]; 

    const reducer = redeuceur(initialState, handler);

    assert.strictEqual(reducer(initialState, actionFoo), nextState);
  });

  it(`Reducer returns the current state if there's no condition match.`, function () {
    const state = `foo bar baz`;
    const getQux = (state, action) => action.qux;

    const handlers = [
      [ actionTypes.FOO, getQux, ],
      [ actionTypes.BAR, getQux, ],
      [ actionTypes.BAZ, getQux, ],
    ];

    const actionQux = { type: actionTypes.QUX, qux: `qux`, };
      
    const reducer = redeuceur(initialState, handlers);

    assert.strictEqual(reducer(state, actionQux), state);
  });
});