aboutsummaryrefslogtreecommitdiff
path: root/src/web/js/dispatcher/__tests__/AppDispatcher-test.js
blob: d3a35fc58ad0d8e11730924ed3ca3b734906a16c (plain)
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
"use strict";

jest.autoMockOff();

describe('AppDispatcher', function() {
  var AppDispatcher;

  beforeEach(function() {
    AppDispatcher = require('../AppDispatcher.js');
  });

  it('sends actions to subscribers', function() {
    var listener = jest.genMockFunction();
    AppDispatcher.register(listener);

    var payload = {};
    AppDispatcher.dispatch(payload);
    expect(listener.mock.calls.length).toBe(1);
    expect(listener.mock.calls[0][0]).toBe(payload);
  });

  it('waits with chained dependencies properly', function() {
    var payload = {};

    var listener1Done = false;
    var listener1 = function(pl) {
      AppDispatcher.waitFor([index2, index4]);
      // Second, third, and fourth listeners should have now been called
      expect(listener2Done).toBe(true);
      expect(listener3Done).toBe(true);
      expect(listener4Done).toBe(true);
      listener1Done = true;
    };
    var index1 = AppDispatcher.register(listener1);

    var listener2Done = false;
    var listener2 = function(pl) {
      AppDispatcher.waitFor([index3]);
      expect(listener3Done).toBe(true);
      listener2Done = true;
    };
    var index2 = AppDispatcher.register(listener2);

    var listener3Done = false;
    var listener3 = function(pl) {
      listener3Done = true;
    };
    var index3 = AppDispatcher.register(listener3);

    var listener4Done = false;
    var listener4 = function(pl) {
      AppDispatcher.waitFor([index3]);
      expect(listener3Done).toBe(true);
      listener4Done = true;
    };
    var index4 = AppDispatcher.register(listener4);

    runs(function() {
      AppDispatcher.dispatch(payload);
    });

    waitsFor(function() {
      return listener1Done;
    }, "Not all subscribers were properly called", 500);

    runs(function() {
      expect(listener1Done).toBe(true);
      expect(listener2Done).toBe(true);
      expect(listener3Done).toBe(true);
    });
  });
});
bgstack15