"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.only('waits with chained dependencies properly', function() {
    var payload = {};

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

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

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

    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);
    });
  });
});

@@ -672,16 +672,24 @@
 (%5Bindex2
+, index4
 %5D, funct
@@ -717,18 +717,27 @@
 cond
- and third
+, third, and fourth
  lis
@@ -847,32 +847,74 @@
 ne).toBe(true);%0A
+        expect(listener4Done).toBe(true);%0A
         listener
@@ -1432,24 +1432,290 @@
 istener3);%0A%0A
+    var listener4Done = false;%0A    var listener4 = function(pl) %7B%0A      return AppDispatcher.waitFor(%5Bindex3%5D, function() %7B%0A        expect(listener3Done).toBe(true);%0A        listener4Done = true;%0A      %7D);%0A    %7D;%0A    var index4 = AppDispatcher.register(listener4);%0A%0A
     runs(fun

Add more complicated fourth listener to AppDispatcher-test