TwoLeggedMammal
TwoLeggedMammal

Reputation: 53

Ember CLI + Mirage: When are objects saved to the store

I'm writing some tests where I create a bunch of objects which rely on each other. My code looks like:

let translations =
        [server.create('translation', { key: 'positive.callRating', value: 'How would you rate your call with %agentFirstName%?' }),
         server.create('translation', { key: 'negative.sorry', value: 'What could %agentFirstName% have done better?' }),
         server.create('translation', { key: 'social.ratingGiven', value: 'I just rated %agentFirstName% %stars%!' })];

let profile = server.create('profile', { first_name: 'Andy' });
let employee = server.create('employee', { profile: profile });
let company = server.create('company', { handle: 'lendingtree', translations: translations });
let bootstrap = server.create('bootstrap', { stars: 5, company: company, employee: employee });

And I have a service which is supposed to know about some of these objects. When I call:

this.get('store').peekAll('translation')

from the service I get no results, but all of my other objects, retrieved the same way, exist in the store; profile ,employee, company and bootstrap.

I'm sure I have to tweak my model or serializer or factory somehow to make this work but it'd be more useful to know about the fundamentals.

What causes an object created via Mirage to enter the store? Are there certain requirements they must meet? Does it depend on their relation to other objects?

Upvotes: 0

Views: 239

Answers (1)

Sam Selikoff
Sam Selikoff

Reputation: 12704

server.create will make objects in Mirage's database. Mirage's server is a mock server, so it knows absolutely nothing about your app; all it knows how to do is respond to HTTP requests. That means in order to get the mock data into your Ember app, your app needs to make HTTP requests, typically via store.findAll.

So, in an acceptance test, when you visit(/some/path), the model hook for that path will make a GET request, Mirage will respond with the appropriate data, and then you'll have data in your store.

Upvotes: 2

Related Questions