kadirahq / flow-router

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

Stuck at Idempotent Routing -> new ES2015 BDD style test cases failing while old Tinytest cases works #455

Closed tjmonsi closed 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?

arunoda commented 8 years ago

Hey this is great. But you should this PR to the SSR branch.

arunoda commented 8 years ago

When you send the PR to that. I'll try to see what's going on here. BTW what's the catchable function?

tjmonsi commented 8 years ago

Eeek i put it on main... Shoot. Ill PR this on SSR. Sorry about that

On Fri, 18 Dec 2015 15:25 Arunoda Susiripala notifications@github.com wrote:

Hey this is great. But you should this PR to the SSR branch.

— Reply to this email directly or view it on GitHub https://github.com/kadirahq/flow-router/pull/455#issuecomment-165700516.

tjmonsi commented 8 years ago

catchable function is a wrap around of try/catch to catch the Assertion error and put it on done(e). I just copied it from smithy:describe because we still can't use ES2015 import as of now.