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

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.

3

u/SarSha Aug 25 '16

Awesome.

Waiting for the build process video as part of your awesome Ultimate course !

2

u/toddmotto Aug 25 '16

Fully recorded this evening, production over next few days :)! Includes all the firebase setup, config, and deploying right at the end :D stay tuned on the slack thread.

3

u/PixelJonas Aug 26 '16

I was just looking for a in-depth guide to angular. I'll definitely check this out.

1

u/toddmotto Aug 26 '16

Great to hear, enjoy :)!

2

u/sweetLew2 Aug 26 '16

I like that the spec is included in each component file. Thanks for making this!

2

u/toddmotto Aug 26 '16

Thanks! Was super easy using gulp to filter the tests out and the production files. Thanks for posting :)

1

u/jmcunningham Aug 26 '16 edited Aug 26 '16

Nice! When I describe to others what I think the right architecture is, you throw something up on Github that has real examples that I can say "look at this for a perfect example of what we can do." Plus seeing how popular your repo's are, it makes it easier to get adoption of these ideas.

2 questions though, for things I like to do. What are your thoughts (or reasons for not including):

  • component specific .sass files within the component dir they belong to. That way the component is truly modular and portable. Helpful when you realize a component might need to be shared among projects. Otherwise you have to dig through the big .sass file to figure out what styles to pull out with the component.

  • I kinda like the idea of having a "pages" directory alongside the same level as components. So page-level components (smart components that wireup the smaller components) are a bit separate from the smaller, reusable (dumb) components.

4

u/toddmotto Aug 26 '16

Hey Jeff! Thanks for the kind feedback, glad this has been of some value to you/team. So, right now Angular 1.x doesn't have an encapsulated styles luxury that Angular 2 and React bring, however I'm looking at writing a small script that adds a styles: [] property to the .component() method for solving this and internally adding a scoped style. Might be a bit of work though :D I try not to think of "pages" so much and think about "routed components", i.e. a root level component that is dynamically swapped out on view state changes. Technically a "page", but still a component. More on routed components here: https://github.com/toddmotto/angular-styleguide hope that helps somewhat. For styles, I think we need a solution.

1

u/jmcunningham Aug 26 '16

thanks for following up. I'll checkout out your routed-components idea again.

As for styles, I was just thinking about (for now), putting the .sass files with the component, but you'll still have the sass compiler bundle them into one file. That be would a middle-ground solution for now.

Longer term, I'm also trying to figure out what is the right way to handle css, that would also migrate to ng 2. I'm wondering if CSS Modules might be the answer, but haven't had time to dig in deep enough to try them out.

-6

u/techmaster242 Aug 25 '16

You should look into using webpack. I learned it and haven't touched gulp since.

8

u/[deleted] Aug 26 '16

OP is Todd Motto, a Google developer expert and widely-respected JS developer. Pretty sure he knows about Webpack.

4

u/toddmotto Aug 26 '16

Web...pack? Haha, I decided to roll with gulp as the entry level's a little easier and I would rather spend more time creating goodies than fluffing around with new tech (not saying Webpack isn't great, it is but gulp is super fast to just roll something up). Take care friends and thanks for the comments :)