aboutsummaryrefslogtreecommitdiff
path: root/test/shared/store/index.test.js
blob: 133033bae9d479debd1739a30935733540733e5b (plain) (blame)
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
import { expect } from "chai";
import { createStore } from 'shared/store';

describe("Store class", () => {
  const reducer = (state, action) => {
    if (state == undefined) {
      return 0;
    }
    return state + action;
  };

  describe("#dispatch", () => {
    it('transit status by immediate action', () => {
      let store = createStore(reducer);
      store.dispatch(10);
      expect(store.getState()).to.equal(10);

      store.dispatch(-20);
      expect(store.getState()).to.equal(-10);
    });

    it('returns next state by immediate action', () => {
      let store = createStore(reducer);
      let dispatchedAction = store.dispatch(11);
      expect(dispatchedAction).to.equal(11);
    });

    it('transit status by Promise action', () => {
      let store = createStore(reducer);
      let p1 = Promise.resolve(10);

      return store.dispatch(p1).then(() => {
        expect(store.getState()).to.equal(10);
      }).then(() => {
        store.dispatch(Promise.resolve(-20));
      }).then(() => {
        expect(store.getState()).to.equal(-10);
      });
    });

    it('returns next state by promise action', () => {
      let store = createStore(reducer);
      let dispatchedAction = store.dispatch(Promise.resolve(11));
      return dispatchedAction.then((value) => {
        expect(value).to.equal(11);
      });
    });
  });

  describe("#subscribe", () => {
    it('invoke callback', (done) => {
      let store = createStore(reducer);
      store.subscribe(() => {
        expect(store.getState()).to.equal(15);
        done();
      });
      store.dispatch(15);
    });

    it('propagate sender object', (done) => {
      let store = createStore(reducer);
      store.subscribe((sender) => {
        expect(sender).to.equal('sender');
        done();
      });
      store.dispatch(15, 'sender');
    });
  })

  describe("catcher", () => {
    it('catch an error in reducer on initializing by immediate action', (done) => {
      let store = createStore(() => {
        throw new Error();
      }, (e) => {
        expect(e).to.be.an('error');
        done();
      });
    });

    it('catch an error in reducer on initializing by immediate action', (done) => {
      let store = createStore((state, action) => {
        if (state === undefined) return 0;
        throw new Error();
      }, (e) => {
        expect(e).to.be.an('error');
        done();
      });
      store.dispatch(20);
    });

    it('catch an error in reducer on initializing by promise action', (done) => {
      let store = createStore((state, action) => {
        if (state === undefined) return 0;
        throw new Error();
      }, (e) => {
        expect(e).to.be.an('error');
        done();
      });
      store.dispatch(Promise.resolve(20));
    });

    it('catch an error in promise action', (done) => {
      let store = createStore((state, action) => 0, (e) => {
        expect(e).to.be.an('error');
        done();
      });
      store.dispatch(new Promise(() => { throw new Error() }));
    });
  })
});