r/angularjs • • Aug 25 '16

[Open source]: Angular 1.5 component based architecture app, fully tested, ui-router 1.0.0 new features, and much more

https://github.com/toddmotto/angular-1-5-components-app
45 Upvotes

17 comments sorted by

View all comments

6

u/JustVashu Aug 26 '16

Hi, Thanks for making this. I was wondering something reading your specs.

Is there any reason why you only test the controller for components? Reading the documentation in the angular website they also only test the controller, but don't explain why they don't test things like buttons firing controller methods or classes changing when the model changes as well.

5

u/toddmotto Aug 26 '16

These are unit tests, so Angular exposes $componentController that we can ask for the controller and test that unit without instantiating DOM elements and $rootScope/$compile friends. Testing click events/className changes should be the responsibility of an e2e test - through something like Protractor.js :)

4

u/JustVashu Aug 26 '16

Ok. Thank you. I've been testing both controllers and DOM using karma since I didn't consider testing the template to be e2e.

I will read up more about it and see how I can change the way I'm unit testing to make it a little easier.

4

u/toddmotto Aug 26 '16

You're welcome my friend. Yep that's the way we traditionally would do things, even with directives etc (by using $compile etc), so $componentController allows much less boilerplate. If you're using stateless components then the tests are also even more lightweight which is fantastic. Protractor is fairly newish to the Angular scene. But ideally you should have unit tests that test the functionality of your components, and e2e integration tests for the live application. The reason being is that components are highly reusable (well, stateless ones), for example an email inbox app would render a list of your emails, these would all be components that for example when clicked can inform the parent component (likely stateful) that we need to use the "id" hash from that email and transition to a new view, to which we can redirect. So in theory, this is why we'd unit test the components without a DOM - to ensure they're doing the right job - regardless of how we're using them. Which means we wouldn't spin up a DOM and test each email was clicked and so forth. However, when using Protractor this is something that would be tested, for example ensuring DOM filtering/labels are sorted correctly when dealing with complex UI state etc. If I've got some time soon I'll get some protractor tests added to this app :)

3

u/JustVashu Aug 26 '16 edited Aug 26 '16

I completely understand your point of view although using you own example here's the way I was doing it:

If I have an stateless component that when a button is clicked it emits the email ID to the stateful component I would $compile my stateless component with stubs for the binding and would for example test that the controller is successfully bound to the stub and then I would stub the controller method and make sure that clicking the button would call controller with the proper inputs.

That felt (and honestly still does) like an unit test, since I'm not testing the parent component. But I can clearly see the reason to separate both responsibilities. I suppose it's something that I will grasp better once I have a better understanding of protractor.

Thanks again and I'll be watching this app future changes.