mardi 30 juin 2015

Simple Ember component test with computed properties

I know this should be simple, I'm just not doing something right, and haven't found the example to mimic nor I guess do I fully understand what should go inside an Ember.run()

Here is my component code:

import Ember from 'ember';
export default Ember.Component.extend({
  isGreen: function() {
    if (!status) { return ''; }
    return status.toUpperCase() === 'G';
  }.property('status')
});

My component template:

<div class="btn-group" data-toggle="buttons">
  <label class="btn btn-green {{if isGreen 'active'}}">
    <input checked="checked" name="status" value="G" type="radio"/> Green
  </label>
  <label class="btn btn-yellow {{if isYellow 'active'}}">
      <input name="status" value="Y" type="radio"/> Yellow
  </label>
  <label class="btn btn-red {{if isRed 'active'}}">
      <input name="status" value="R" type="radio"/> Red
  </label>
</div>

And in my test:

test('status sets active class on the correct button', function(assert) {
  expect(3);
  var component = this.subject();

  //Green status
  Ember.run(function() {
    component.set('status', 'G');
  })
  equal(this.$().find('.btn-green.active').length, 1, 'When status is green, the green button has the active class');

I have three of these tests, one for each of three different statuses. If I have just one test, I don't need the to wrap component.set() in an Ember.run() in order for the test to pass. But if I have all three tests, I get this error:

You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in a run

But if I do put each of my component.set() calls in a run loop, then my tests fail due to the equal assertion expecting 1 and getting 0. I'm sure this is due to a lack of understanding on my part, but either the set() isn't running, or else the component gets re-rendered in the assertion and thus doesn't know that the status property was already set. I've been reading docs and googling for an hour, and haven't found a solution yet (and more beneficially, an explanation).

Aucun commentaire:

Enregistrer un commentaire