How to approve DOM changes to the run loop inside an Ember test

I am writing a test for an Ember application written in Ember 1.6.

Inside the controller, I have a function that executes on a successful promise:

var me = this;

function onSuccess(result) {

    printSuccessMessage();

    Ember.RSVP.all(promises).then(function(value) {
        Ember.run.later(this, function() {
            clearMessages();
        }, 5000);
    });
}

Then, inside the test, I try to claim that a success message appears:

    fillIn('#MyInputField', 'Some text');
    click('#MyButton');

    andThen(function() {
        strictEqual(find('[data-output="info-message"]').text().trim().indexOf('Done!') >= 0, true, 'Expected success message!');
    });

But the problem is that after a click, it andThenwaits for the completion of the execution loop. Therefore, after this press, it andThenwaits 5 seconds, and then executes the statement.

At this point, it is clearMessages()already executed, the div message is cleared, and the test fails.

Any idea how to claim that this message has a specific text?

+4
source share
1 answer

, , Ember , Ember.testing , , . , .

onSuccess Ember.testing:

onSuccess(message) {
  this.printSuccessMessage(message);

  if (Ember.testing) { // <-- HERE
    // during testing
    return; // don't clear the message and assert that it there
  } else {
    // during dev, live in production, or Ember.testing === false
    this.clearMessages(); // clear the message, and assert that it gone
  }  
},

, Ember.testing true , , :

test('setting the message', function(assert) { 
  visit('/messages');
  fillIn('input.text-input', 'Some text');
  click('button.clicker');

  // while Ember.testing is `true` (default), do not remove message
  andThen(() => {
    assert.equal(find('div.info-message').text(),
                 'Done!',
                 'The message was set properly.'); 
  });
});

false Ember.testing, "" . , , :

test('clearing the message', function(assert) { 
  visit('/messages');
  fillIn('input.text-input', 'Some text');

  andThen(() => {
    Ember.testing = false;
  });

  click('button.clicker');

  // while Ember.testing is `false`, remove message, as normal, as in dev or prod
  andThen(() => {
    assert.equal(find('div.info-message').text(), 
                 '', 
                 'The message has been cleared.'); 
  });

  // reset Ember.testing to its default
  andThen(() => {
    Ember.testing = true;
  });
});

, Ember.testing reset true, false . , Ember run .

, unit test. Ember Twiddle, , Medium.

+1

Source: https://habr.com/ru/post/1662266/


All Articles