Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js: How to integration test components that interact with ember-data models

I'm building a relatively straight-foward comment-list component. I want to pass in the commentable model (say a Post) and have the component take care of creating, editing, deleting comments. Right now I pass around all the various actions and it's been extremely brittle.

How do I create a true instance of an Ember Data model in a component integration test?

My immediate thought was to import the model then .create({}) it but that errors with use this.store.createRecord() instead

/* jshint expr:true */
import { assert } from 'chai';
import { describeComponent, it } from 'ember-mocha';
import hbs from 'htmlbars-inline-precompile';
import Post from 'ownersup-client/post/model';

describeComponent( 'comment-list', 'Integration: CommentListComponent', {
    integration: true
  },
  function() {
    it('renders all of the comments', function() {
      const model = Post.create({ title: 'title' });
      model.get('comments').createRecord({ body: 'One Comment' })

      this.render(hbs`{{comment-list model=model}}`);

      assert.lengthOf(this.$('.comment-list-item'), 1);
    });
  }
);

Anyone have any thoughts?

like image 249
mattmcmanus Avatar asked Oct 01 '15 19:10

mattmcmanus


People also ask

How do I run an integration test in Ember?

In order to integration test the Ember application, you need to run the app within your test framework. Set the root element of the application to an arbitrary element you know will exist. It is useful, as an aid to test-driven development, if the root element is visible while the tests run.

Is Ember component based?

Ember knows which subclass powers a component based on its filename. For example, if you have a component called blog-post , you would create a file at app/components/blog-post. js . If your component was called audio-player-controls , the file name would be at app/components/audio-player-controls.

What are ember models?

In Ember Data, models are objects that represent the underlying data that your application presents to the user. Note that Ember Data models are a different concept than the model method on Routes, although they share the same name.

What is Ember mirage?

Ember CLI Mirage is a client side mock server to develop and prototype applications. It uses a library called. Pretender in the background to make this possible. At some point while learning Ember. js you probably have needed to mock some data from your server.


2 Answers

Among all Ember test helpers, the store is only available from moduleForModel.

Here's how this test helper does it (source):

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

You can do the same inside your test. You can also put it into a helper so that you don't have to copy-paste a lot.

Note that it will only work for an integration test. You can turn any test into integration one by starting the app using the startApp test helper that is bundled with your Ember CLI boilerplate.

like image 123
Andrey Mikhaylov - lolmaus Avatar answered Oct 18 '22 20:10

Andrey Mikhaylov - lolmaus


The new ember mocha release 0.8.4 contains a new, public way to inject services such as the store. There's a guides section coming soon with an example (see https://github.com/emberjs/guides/blob/master/source/testing/testing-components.md)

essentially, in your beforeEach you want add the following line: this.inject.service('store');, making the store accessible as this.get('store') in your tests.

Here's a link to the pull request for the new change: https://github.com/switchfly/ember-test-helpers/pull/105

like image 5
Todd Jordan Avatar answered Oct 18 '22 18:10

Todd Jordan