samedi 13 janvier 2018

Is 'incrementProperty' an async call?

Looking at the Ember tutorial on testing models, it treats incrementProperty as an async call. Why?

app/models/player.js

import Model from 'ember-data/model';
import { attr } from 'ember-data/model';

export default Model.extend({
  level: attr('number', { defaultValue: 0 }),
  levelName: attr('string', { defaultValue: 'Noob' }),

  levelUp() {
    let newLevel = this.incrementProperty('level');
    if (newLevel === 5) {
      this.set('levelName', 'Professional');
    }
  }
});

tests/unit/models/player-test.js

import { moduleForModel, test } from 'ember-qunit';
import { run } from "@ember/runloop";

moduleForModel('player', 'Unit | Model | player', {
  // Specify the other units that are required for this test.
  needs: []
});

test('should increment level when told to', function(assert) {
  // this.subject aliases the createRecord method on the model
  const player = this.subject({ level: 4 });

  // wrap asynchronous call in run loop
  run(() => player.levelUp());

  assert.equal(player.get('level'), 5, 'level gets incremented');
  assert.equal(player.get('levelName'), 'Professional', 'new level is called professional');
});

The code appears to be synchronous as far as I can tell.




Aucun commentaire:

Enregistrer un commentaire