Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember Integration Test for Closure Actions

In Ember-CLI 1.13.1, I have the following integration test in my component:

import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';

moduleForComponent('category-tabs', 'Integration | Component | category tabs', {
  integration: true
});

test('tapping button fires an external action', function(assert) {
  this.on('onTabTouch', function(value) {
    assert.equal(value, 'Expense');
  });

  this.render(hbs`
    {{category-tabs onTabTouch=(action "onTabTouch")}}
  `);

  this.$('button:first').click();
});

Then in my component, I have this action:

import Ember from 'ember';

export default Ember.Component.extend({
  actions: {
    handleTabTouch(tab) {
      this.attrs.onTabTouch(tab);
    }
  }
});

My test keeps on saying this:

An action named 'onTabTouch' was not found in [object Object].

How do I test a closure action? I also tried:

this.set('onTabTouch', function(value) {
  assert.equal(value, 'Expense');
});

And it didn't work.

like image 852
Mikko Paderes Avatar asked Jul 26 '15 05:07

Mikko Paderes


1 Answers

I've managed to figure it out.

Instead of writing:

this.on('onTabTouch', function(value) {
  assert.equal(value, 'Expense');
});

I wrote this:

this.set('actions', {
  onTabTouch(value) {
    assert.equal(value, 'Expense');
  }
});

EDIT:

A better way of doing it now is like this:

this.set('onTabTouch', () => assert.ok(true));

this.render(hbs`
  {{category-tabs onTabTouch=(action onTabTouch)}}
`);

Notice that (action onTabTouch) doesn't have any double quotes.

like image 155
Mikko Paderes Avatar answered Oct 27 '22 19:10

Mikko Paderes