Emad
Emad

Reputation: 4200

Pass a model into a component test in ember

I have a component that requires an instance of a model. The model name is WorkflowState. How do I create an instance of workflowstate and inject into the component.

I need to create the workflow state using:

WorkflowState.create('some data') 

because the constructor has some logic that need to run.

I cannot get to WorkflowState. I tried adding an import statement like this

import WorkflowState from "../../../app/models/wokrflow-state";

but that didn't compile...

Any idea how I can do that?

My test looks something like this:

  var stateString = 'some data';
  var state = WorkflowState.create(JSON.parse(stateString));

  var component = this.subject({
    workflow: {state: state}
  });
  // do some testing on the component

Help please. Thanks.

Upvotes: 3

Views: 657

Answers (1)

Asgaroth
Asgaroth

Reputation: 4334

Ideally the testing of a component should be isolated from other units, like the model in this case. You can pass a fake model that has the state you need to test specific behavior.

Ember.Object.create({ // the state you need })

And pass that to the component

If you really need to pass a real model you will have to do a lot more things, create a container and set up a store, etc. since you cannot call .create directly on a model, hopefully you can avoid all of that.

Upvotes: 4

Related Questions