Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to unit test a model using PartialModel extension? #30

Open
bsaff opened this issue May 4, 2018 · 3 comments
Open

How to unit test a model using PartialModel extension? #30

bsaff opened this issue May 4, 2018 · 3 comments

Comments

@bsaff
Copy link

bsaff commented May 4, 2018

After extending my model, call it car, from PartialModel I can't run ember unit test. Tests fail with the following error:

No model was found for 'car-extended'

Tried including in unit test dependency like so:

import { moduleForModel, test } from 'ember-qunit';

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

test('it exists', function(assert) {
  var model = this.subject();
  // var store = this.store();
  assert.ok(!!model);
});

but that doesn't work either.

Any suggestions on how to get unit tests working?

Model def:

import DS from 'ember-data';

import { PartialModel, partial } from 'ember-data-partial-model/utils/model';

export default PartialModel.extend({

  name: DS.attr(),

  extended: partial('car', 'extended', {
    status: DS.attr('string'),
  }),

});

versions:
"ember-data": "2.13",
"ember-data-partial-model": "0.4.0",
"ember-source": "~2.15.0",

@Azdaroth
Copy link
Member

Azdaroth commented May 8, 2018

Hi @bsaff

The problem is most likely related to the fact that partial extensions are lazily generated only when using Store.modelFor, so the solution I believe would be to do a similar setup in the test as it is here: https://github.com/BookingSync/ember-data-partial-model/blob/master/addon/mixins/store.js

@bsaff
Copy link
Author

bsaff commented May 8, 2018

Hey @Azdaroth, thank you very much! That was exactly what I needed.

here's my workaround:

// app_name/tests/helpers/partial-model-helper.js

import Ember from 'ember';
import DS from 'ember-data';
const { Model } = DS;

export function generatePartialExtensionModel(factoryName, factory) {
  factory.eachRelationship((relationshipKey, descriptor) => {
    let partialExtensionModelName = `${factoryName}-${relationshipKey}`;
    let dependencyName = `model:${partialExtensionModelName}`;
    if (descriptor.options.isPartialExtension === true) {
      if (!_isDependencyRegistered(this, dependencyName)) {
        let partialExtensionModel = Model.extend(descriptor.options.classHash)
          .reopenClass({ _extendPartialModel: factoryName });
        _registerDependency(this, dependencyName, partialExtensionModel);
      }
    }
  });
}

function _registerDependency(store, dependencyName, dependency) {
  if (Ember.getOwner) {
    Ember.getOwner(store).register(dependencyName, dependency);
  }
}

function _isDependencyRegistered(store, dependencyName) {
  if (Ember.getOwner) {
    return Ember.getOwner(store).hasRegistration(dependencyName);
  }
}
// app_name/tests/unit/models/car.js

import { moduleForModel, test } from 'ember-qunit';
import { generatePartialExtensionModel } from 'market-ember/tests/helpers/partial-model-helper';

moduleForModel('car', 'Unit | Model | car', {
  needs: [],
  beforeEach() {
    const car = this.store().modelFor('car');
    generatePartialExtensionModel.call(this, 'car', car);
  },
});

test('it exists', function(assert) {
  var model = this.subject();
  assert.ok(!!model);
});

@bsaff bsaff closed this as completed May 8, 2018
@bsaff bsaff reopened this May 8, 2018
@Azdaroth
Copy link
Member

Azdaroth commented May 9, 2018

Hmm, this is certainly too complex, it should really be way simpler than that with some extra helper, but glad to hear you've managed to handle it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants