I have the following model
and afterModel
hooks in the route:
export default Route.extend(AuthenticatedRouteMixin, {
currentShop: service(),
async model() {
return {
events: await this.store.query('event', { country_id: this.get('currentShop.shop.country.id')}),
shopEvents: await this.store.query('shop-event', { shop_identifier: this.get('currentShop.shop.identifier') })
}
},
afterModel(model) {
let currentShopEvents = this.get('currentShop.shop.shopEvents');
console.log("currentShopEvents: " + currentShopEvents);
if(currentShopEvents) {
currentShopEvents.clear();
}
this.get('currentShop.shop').set('shopEvents', model.shopEvents);
},
...
It works as needed in the app. The problem comes in an acceptance test:
TypeError: currentShopEvents.clear is not a function
The difference seems to be in what kind of object is this.get('currentShop.shop.shopEvents');
:
- when running the test, it is
collection:shop-event(1)
- when running the app, it is
<DS.ManyArray:ember2198>
After looking in the documentation of EC Mirage for Collection
, it DOES not contain clear
method compared to ED MutableArray one.
Does anybody have an idea what to use in this case to make it work both in tests and the app? Here is the failing test:
module('Acceptance | Country events', function(hooks) {
setupWindowMock(hooks);
setupApplicationTest(hooks);
setupMirage(hooks);
hooks.beforeEach(function() {
let country = this.server.create('country');
let event = this.server.create('event', { country });
let currentShop = this.owner.lookup('service:current-shop');
let shop = this.server.create('shop', { country });
this.server.create('shopEvent', {
shop: shop,
event: event
});
currentShop.setShop(shop);
});
test('Authenticated users can visit /country-events', async function(assert) {
this.server.create('user');
await authenticateSession({
access_token: 'azerty',
token_type: 'Bearer'
});
await visit('/country-events');
assert.equal(currentURL(), '/country-events', 'user is on the Events page');
});
});
I tried to change little bit to not use clear
method:
afterModel(model) {
let currentShopEvents = this.get('currentShop.shop.shopEvents');
if(currentShopEvents) {
currentShopEvents.set('shopEvents', []);
}
currentShopEvents.pushObjects(model.shopEvents);
},
but time EC Mirage raises TypeError: currentShopEvents.set is not a function
. I think it will not like pushObjects
either
Thank you!