0

Ember JS framework.

I am trying to test my route.js . I created route-test.js as below which is running fine but I need to use mirage mocking to mock foo model in route-test.js.

When I tried mocking, I got below error: Mirage: Error: You're trying to create a foo model and you passed in "[object Object],[object Object],[object Object]" under the boo key, but that key is a HasMany relationship. You must pass in a Collection, PolymorphicCollection, array of Models, or null.

Below are my code files:

// mirage/factories/foo.js

import { Factory, association } from 'ember-cli-mirage';

export default Factory.extend({
    isActive: true,
    user: association(),
});
// route-test.js:

test('model should return expected structure', async function (assert) {
        const foo = {
            boo: [
                { code: 'USD' },
                { code: 'EUR' },
                { code: 'USD' },
            ],
        };

        sinon
            .stub(this.route, 'modelFor')
            .withArgs('admin.foo.edit')
            .returns({ foo });

        const productClasses = [{ id: '1', name: 'class1' }];

        sinon
            .stub(this.store, 'query')
            .withArgs('product-class')
            .resolves(productClasses);

        const model = await this.route.model();

        assert.deepEqual(model.foo, foo, 'foo data is correct');
    });
// route.js:

async model() {
        const { foo } = this.modelFor('admin.foo.edit');
        const { foo } = this.modelFor(
            'admin.foo.edit'
        );

        let currencies = [];
        foo.boo.forEach(boo => {
            if (boo.code) {
                currencies = currencies.concat(
                    boo.code
                );
            }
        });
        uniqueCurrencies = [...new Set(currencies)];
        uniqueCurrencies.sort();

        const productClasses = await this.store.query('product-class');
        return hash({
            foo,
            productClasses,
            uniqueCurrencies,
        });
    }

I tried creating factory for boo as below, but got same error.

// mirage/factories/boo.js

import { Factory, association } from 'ember-cli-mirage';

export default Factory.extend({
    currencyCode(i) {
        return i % 2 === 0 ? 'USD' : 'EUR';
    },
    foo: association(),
});

Note: I am not using mirage models in my project so cannot use that.

Is there any other way to resolve this?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.