Testing if a record and it's relations have been destroyed


#1

I’ve created an Ember service with a method to destroy a record and it’s related data (defined by relationships in the model).

This is my delete-user.js service.

import Promise from 'rsvp';
import Service, { inject as service } from '@ember/service';

export default Service.extend({
  store: service(),

  deleteUserAndAssociatedData(userRecord) {
    return userRecord.get('posts')
      .then(postRecords => this._deletePosts(postRecords))
      .then(() => userRecord.destroyRecord());
  },

  _deletePosts(postRecords) {
    return Promise
      .all(postRecords.invoke('destroyRecord'));
  }
});

My question is how am I supposed to test this functionality? I would like to verify that the user and it’s posts have been marked as destroyed.

This is what I have so far in delete-user-test.js:

import { moduleFor, test } from 'ember-qunit';
import { run } from '@ember/runloop';
import { A } from '@ember/array';
import EmberObject from '@ember/object';

moduleFor('service:delete-user', 'Integration | Service | delete-user', {
  integration: true
});

let generateDummyUser = container => {
  const store = container.lookup('service:store');

  return run(() => {
    return store.createRecord('user', { name: 'Dummy User'});
  });
};

let generateDummyPost = container => {
  const store = container.lookup('service:store');

  return run(() => {
    return store.createRecord('post');
  });
};

test('it exists', function (assert) {
  let service = this.subject();
  assert.ok(service);
});

test('should delete the user', function (assert) {
  let service = this.subject();

  run(() => {
    const userRecord = generateDummyUser(this.container);
    service.deleteUserAndAssociatedData(userRecord);
    assert.equal(userRecord.get('isDeleted'), true, 'the user is deleted');
  });
});

test('should delete posts of the user', function (assert) {
  let service = this.subject();

  run(() => {
    const userRecord = generateDummyUser(this.container);
    const postRecord = generateDummyPost(this.container);
    userRecord.get('posts').pushObject(postRecord);

    return run(() => {
      service.deleteUserAndAssociatedData(userRecord);
      assert.equal(postRecord.get('isDeleted'), true, 'the post is deleted');
    })
  });
});

But the tests fail on the first assertion already with the following error:

Assertion Failed: calling set on destroyed object: <webapp@adapter:application::ember250>.isSynced = false"

Is there anyone who can point me in the right direction? Maybe I should stub the adapter? Spy on the destroyRecord method? I’m not sure where to go from here.

Any help is very much appreciated! Thanks, Niels