Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Click test helper doesn't fire .on('click') handlers #10495

Closed
jamesarosen opened this issue Feb 20, 2015 · 10 comments
Closed

Click test helper doesn't fire .on('click') handlers #10495

jamesarosen opened this issue Feb 20, 2015 · 10 comments

Comments

@jamesarosen
Copy link

I have a component:

export default Ember.Component.extend({
  tagName: 'button',
  count: 0,
  _increment: function() {
    this.set('count', this.get('count') + 1);
  }.on('click')
});

I'm trying to write a test for it:

moduleForComponent('increment-button', 'IncrementButtonComponent', {
  setup: function() {
    App = startApp();
    component = this.subject();
    $component = this.append();
  },

  teardown: function() {
    Ember.run(App, 'destroy');
    App = null;
  }
});

test('clicking increments the count', function() {
  click('button');
  andThen(function() {
    equal(component.get('count'), 1);
  });
});

The test fails with 0 !== 1. I added a console.log statement to _increment and found that it never gets called.

@jamesarosen
Copy link
Author

I also tried replacing click('button') with $component.find('.something-that-exists').click(). No change.

@kimroen
Copy link
Contributor

kimroen commented Feb 22, 2015

This test passes:

var application;

moduleForComponent('increment-button', {
  beforeEach: function() {
    application = startApp();
  },

  afterEach: function() {
    Ember.run(application, 'destroy');
    application = null;
  }
});

test('it works', function(assert) {
  assert.expect(2);

  var component = this.subject();
  this.render();

  assert.equal(component.get('count'), 0, 'Count is 0 before clicking');
  click('button');
  andThen(function() {
    assert.equal(component.get('count'), 1, 'Count is 1 after clicking');
  });
});

Moving the call to this.subject() up to beforeEach (previously setup) works, but if I move this.render() (previously this.append()) too, the test fails.

It seems to me like any bug here is not with ember, but with the ember test helpers.

As an aside, it feels a bit strange to start an app like this for a component test, and I see some people agree with that - here's an issue: emberjs/ember-test-helpers#3

@jamesarosen
Copy link
Author

@kimroen great research!

it feels a bit strange to start an app like this for a component test

I agree, though I don't know how else to test the behavior of event handlers. I suspect the app-start is required to set up Ember's (global) event dispatcher.

@wagenet wagenet removed the Bug label Feb 28, 2015
@wagenet
Copy link
Member

wagenet commented Feb 28, 2015

@jamesarosen why are you booting up the app to test the component?

@jamesarosen
Copy link
Author

I don't care about the app booting. I care about the test helpers like click(...).then(...). The former seemed to be the only way to get the latter.

@wagenet
Copy link
Member

wagenet commented Feb 28, 2015

@jamesarosen so, to clarify, this doesn't work if you don't boot the app?

@wagenet
Copy link
Member

wagenet commented Feb 28, 2015

I don't think booting the app should be a requirement for testing components, so if it is, that seems like a bug to me.

@rwjblue
Copy link
Member

rwjblue commented Feb 28, 2015

@wagenet - The helpers that are needed here are specifically added to window when calling app.setupForTesting, and as such require an app. I completely agree that it would be nice to be able to use them while unit testing a component. I'll try to dig into it a bit more to see how we could support both worlds...

@kimroen
Copy link
Contributor

kimroen commented Mar 3, 2015

I came over this today - we do actually recommend doing it this way in the guide for testing components:

We would use Integration Test Helpers to interact with the rendered component and test its behavior

http://emberjs.com/guides/testing/testing-components/

@rwjblue
Copy link
Member

rwjblue commented Apr 13, 2016

This is being addressed by emberjs/rfcs#119. Please track progress over there....

@rwjblue rwjblue closed this as completed Apr 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants