Eunito
Eunito

Reputation: 436

Call asserion for method in emberjs parent class

I have an emberjs file (child.js) that extends from an Abstract js file (Parent.js)

export default Parent.extend({ 
    (...)

    onPress: action(function() {
        if (this.get('isValid') ) {
            this.togglePropertyInParent();
        }
    })

    (...)

})

Inside the parent component we have the boolean property set as false by default and also the togglePropertyInParent method. I want to build a unit test that asserts using sinon that togglePropertyInParent was called once in but I cannot stub it properly...

test('The toggler gets called if the key is pressed', function(assert) {
    const component = this.owner.factoryFor('component:child-component').create();

    // The console logs in the parent are ok, meaning the send worked fine based on the isValid param
    component.send('onPress');

    sinon.assert.calledOnce(this.togglePropertyInParent);
});

How can I stub the parent in order to check if it was called? What am I missing ?

Upvotes: 0

Views: 51

Answers (1)

NullVoxPopuli
NullVoxPopuli

Reputation: 65183

How can I stub the parent in order to check if it was called? What am I missing ?

This is highly advised against.

Components should be tested as the user would interact with them (not just in ember, but this is a growing convention in all frameworks).

So, if you click a button to trigger some state, click that button with click from @ember/test-helpers.

If the visuals of the component change as a result of that click, assert that with assert.dom from qunit-dom.

Upvotes: 0

Related Questions