0

I have a component that uses store: Ember.inject.service() as per this answer https://stackoverflow.com/a/29816081/2482265. When the user hits submit a new record in created in the store - e.g:

var address = {
  line1: this.get('line1'),
  line2: this.get('line2'),
  line3: this.get('line3'),
  town: this.get('town'),
  county: this.get('county'),
  postCode: this.get('postCode'),
  account: account
};

var record = this.get('store').createRecord('address', address);

record.save().then(() => { ... });

I now want to write the integration test for this component, I fill out the fields and then trigger a click on the submit button, thats all fine, but how do I check an address was added to the store?

I saw mention of

var container = this.container;
var store = container.lookup('service:store') || container.lookup('store:main');

But couldn't work out the last step to give me a useful assert.

Update Some of my components fire an action in record.save().then() so I can subscribe to this in the test with a normal this.on('assertMyAction', () => { ... }). But some of my components do not need to fire an action back out so I'll need a different way to know store.record.save has forfilled before checking the some of the finer properties on the record.

Community
  • 1
  • 1
Adam Knights
  • 2,141
  • 1
  • 25
  • 48

1 Answers1

2

Well, record was added to the store when you called createRecord. I mean it should be added to live arrays, but with property isNew set to true.

If you want to check if you record was successfully saved you use something like this:

let store = lookup('store:main');
assert.equal(store.all('user').get('length'), 1);

store.find('user', userId).then((user) => {
  assert.equal(user.get('isDirty'), false);
  assert.equal(user.get('isNew'), false);
});

Before save you should check for isNew equal to true. After save you could also check for properties such as isValid, isError. You can find more potentially useful properties here.

Ember 2.0 You will need to use this.container.lookup('service:store'); rather than store:main

Adam Knights
  • 2,141
  • 1
  • 25
  • 48
Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
  • Thankyou! Some of my components fire an action in the `then` of the record save. So I subscribe to this in the test and check isNew is false etc. Some of my components dont fire such an action however, so I can check the length is 1 like `store.all('user').get('length')`, is there a way to wait for the record to finish saving? – Adam Knights Nov 02 '15 at 14:31
  • Because at the moment `user.get('isNew')` comes back true for those components and a simple `setTimeout(() => {...})` isn't cutting it. – Adam Knights Nov 02 '15 at 14:32
  • In my tests `Ember.run.later` did the job, but not with records. I think you have to expose some promises or hooks for your tests and then explicitly subscribe to these methods. You could maybe experiment with using didLoad, didUpdate events of model if you have instance of record before firing request. http://emberjs.com/api/data/classes/DS.Model.html#event_didUpdate – Daniel Kmak Nov 02 '15 at 14:39
  • I couldn't get anything to work with the events, I've ended up adding optional actions to the remaining components and then subscribing to those in the tests, feels like there must be a better way though – Adam Knights Nov 02 '15 at 16:01