kadirahq / flow-router

Carefully Designed Client Side Router for Meteor
MIT License
1.09k stars 194 forks source link

Stuck at Idempotent Routing -> new ES2015 BDD style test cases failing while old Tinytest cases works (now on right branch) #456

Open tjmonsi opened 8 years ago

tjmonsi commented 8 years ago

I understand what idempotent routing does (which should not re-run the action function when Router.go is called). I just don't get it why it fails to work on the test case when I transferred it to new ES2015 BDD style test case.

Here's the snippet of the test case on the old code.

Tinytest.addAsync('Client - Router - idempotent routing - action',
function(test, done) {
  var rand = Random.id();
  var pathDef = '/' + rand;
  var rendered = 0;

  FlowRouter.route(pathDef, {
    action: function(params) {
      rendered++;
    }
  });

  FlowRouter.go(pathDef);

  Meteor.defer(function() {
    FlowRouter.go(pathDef);

    Meteor.defer(function() {
      test.equal(rendered, 1);
      done();
    });
  });
});

Tinytest.addAsync('Client - Router - idempotent routing - triggers',
function(test, next) {
  var rand = Random.id();
  var pathDef = '/' + rand;
  var runnedTriggers = 0;
  var done = false;

  var triggerFns = [function(params) {
    if (done) return;

    runnedTriggers++;
  }];

  FlowRouter.triggers.enter(triggerFns);

  FlowRouter.route(pathDef, {
    triggersEnter: triggerFns,
    triggersExit: triggerFns
  });

  FlowRouter.go(pathDef);

  FlowRouter.triggers.exit(triggerFns);

  Meteor.defer(function() {
    FlowRouter.go(pathDef);

    Meteor.defer(function() {
      test.equal(runnedTriggers, 2);
      done = true;
      next();
    });
  });
});

and here's the new BDD style

context('Idempotent Routing', () => {
  it('should call action', done => {
    const rand = Random.id();       // var rand = Random.id();
    const pathDef = `/${rand}`;     // var pathDef = '/' + rand;
    let rendered = 0;               // var rendered = 0;

    router.route(pathDef, {         // FlowRouter.route(pathDef, {
      action() {                    //   action: function(params) {
        rendered++;                 //     rendered++;
      }                             //   }
    });                             // });

    router.go(pathDef);             // FlowRouter.go(pathDef);

    Meteor.defer(() => {            // Meteor.defer(function() {
      catchable(done, () => {
        router.go(pathDef);         //   FlowRouter.go(pathDef);

        Meteor.defer(() => {        //   Meteor.defer(function() {
          catchable(done, () => {
            if (Meteor.isClient) expect(rendered).to.be.equal(1);
                                    //     test.equal(rendered, 1);
            done();                 //     done();
          });                       //   });
        });                         // });
      });
    });
  });

  it('should call triggers', done => {
    const rand = Random.id();
    const pathDef = `/${rand}`;
    let runnedTriggers = 0;
    let finish = false;

    const triggerFns = [() => {
      if (finish) return;
      runnedTriggers++;
    }];

    router.triggers.enter(triggerFns);

    router.route(pathDef, {
      triggersEnter: triggerFns,
      triggersExit: triggerFns
    });

    router.go(pathDef);

    router.triggers.exit(triggerFns);

    Meteor.defer(() => {
      catchable(done, () => {
        router.go(pathDef);

        Meteor.defer(() => {
          catchable(done, () => {
            if (Meteor.isClient) expect(runnedTriggers).to.be.equal(2);
            finish = true;
            done();
          });
        });
      });
    });
  });
});

The output turns out this way:

idempotent routing - action - OK
idempotent routing - triggers - OK
...
Idempotent Routing - should call action
fail — assert_equal - message expected 2 to equal 1 - expected 1 - actual 2 - not
Idempotent Routing - should work on triggers
fail — assert_equal - message expected 6 to equal 2 - expected 2 - actual 6 - not 

Can you check what I did wrong?

tjmonsi commented 8 years ago

I am going to go back and add more tests here. :)