My team does a great job at testing, we have acceptance, integration, and unit tests for our Ember App. Sometimes we have found it necessary to modify our coding practices to make the code more easily testable, which I think is a good thing. It’s hard to test actions properly, so we have settled on this paradigm for using actions in a way that is more testable (we are using Babel to make use of the spread operator):
// awesome-component.hbs template
<a {{action "doSomething"}}>Click me!</a>
// awesome-component.js code
actions: {
doSomething() {
this.doSomething(...arguments);
},
},
doSomething() {
// code goes here
},
Now the action is just passing the arguments to a property on the component. This gives us much easier access in testing, and we can more easily stub the method in the tests. It also allows us to reuse the same component more often without dealing with lots of edge cases. If someplace in our app needs the same component, but wants doSomething
to do something different, we can overrided doSomething
by passing a method with the same name into the component as a property when it is called in the template.
{{awesome-component
doSomething=(action doSomethingElse)
}}
If I add the quotes to the name of the method in the action, it looks for the method in the actions hash. If I don’t, it uses whatever the property is that has that name. If I don’t use quotes, and access the method directly as a property of the component, I need to use the (action)
helper to bind the context of the method.
Ok, so that is the situation. My question is, why should we ever use the actions hash? Why not just use the component properties directly? For example:
// awesome-component.hbs template
<a {{action (action doSomething)}}>Click me!</a>
// awesome-component.js code
doSomething() {
// code goes here
},
There are so many different ways to call and use and pass actions, I think it would be better if we just always did it this way and never used the actions hash. Am I missing something, or should we consider deprecating the actions hash?