Top Banner
TDD Your AngularJS + Ionic Directives With jQuery Valeri Karpov NodeJS Engineer, MongoDB thecodebarbarian.com github.com/vkarpov15 @code_barbarian
43

MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Aug 08, 2015

Download

Technology

Valeri Karpov
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

TDD Your AngularJS + Ionic Directives With

jQuery

Valeri KarpovNodeJS Engineer, MongoDB

thecodebarbarian.comgithub.com/vkarpov15

@code_barbarian

Page 2: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Who Am I?- NodeJS @ MongoDB- Maintainer for mongoose- + connect-mongodb-session, mongoskin, etc.- Blogger for StrongLoop- Author of Professional AngularJS- And coined the term “MEAN” in 2013

Page 3: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

What’s This Talk About?- Good old-fashioned testing pyramid

Page 4: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

What’s This Talk About?- Unit Test: tests JS only- Fast, but no real DOM integration- E2E Test: test full stack with DOM integration- Slow, flakey, hard to shard, hard to simulate

errors- “Does my UI actually work?”- Fast feedback on development

Page 5: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

What’s This Talk About?- DOM Integration Tests - like Goldilocks

Page 6: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Overview- Directives and Learning from React- Manual bootstrapping in AngularJS >= 1.3.0- Building a form with isomorphic schema- Bonus: compiling for Ionic framework- Lots of high-level concepts - overview of

upcoming EdX course

Page 7: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

My Favorite ReactJS Idea- one view != one controller + one HTML blob- views as component trees

Page 8: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

This App is Wrong- The “flicker”- Code re-use; need to bring HTML

Page 9: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Using Directives- Use directives as components (web or React)- Or like `@Component` in AngularJS 2

Page 10: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Main HTML- See vkarpov15/node-presentation-examples

Page 11: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Directive + HTML- Testing details: `ng` and `$emit()`- Directive bundles HTML and JS- But not CSS, which is useful for Ionic

Page 12: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Main HTML

Page 13: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Why Components?- No more flicker- Cleaner code separation- Remember when your CS professors yelled at

you for putting all your code in `main()` ?- Easier to test...

Page 14: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Part 2: Testing Directives- Karma: browser instrumentation for testing- (IMO) the most important module on npm- See Testing Client-side JavaScript with Karma

Page 15: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Making Frontend Dev Fast- Getting feedback on frontend work is slow- Make change -> F5 in browser -> open

Chrome console -> click a few buttons -> Feedback

- Karma watches files and re-runs tests for you- Simplify it to: make change -> feedback

Page 16: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Installing Karma

Page 17: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Configuring Karma- Tell Karma files to load and browsers to start- Also tell Karma to start static web server

Page 18: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Configuring Karma- Important: karma only serves templates- So you can test on many browsers in parallel

Page 19: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Instrumenting AngularJS- AngularJS >= 1.3 makes compiling HTML easy- Steps: create injector, create scope, $compile() HTML with new scope

Page 20: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Now You Can Test- Tests use jQuery to manipulate DOM- .trigger(‘change’) triggers $apply- $apply is sync, so test is sync

Page 21: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Fast Feedback with TDD- Suppose you wanted to add a ‘clear’ button- You could point and click…- Or you could write a test first

Page 22: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Fast Feedback with TDD- When you save, karma yells at you

Page 23: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Fast Feedback with TDD- When you save, karma yells at you

Page 24: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Fast Feedback with TDD- But when you add a button and a function...

Page 25: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Fast Feedback Takeaways- Tests are light and fast- Also include DOM integration- Best of both worlds between unit and e2e- More detail on my blog: “Testing AngularJS

Directives”

Page 26: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Fast Feedback Takeaways- TDD is hard. Like lifting heavy weights.

“The resistance that you fight physically in the gym and the resistance that you fight in life can only build a strong character.”

- Arnold Schwarzenegger

Page 27: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Part 3: TDDing a Form- Mongoose (NodeJS ODM for MongoDB) is

partially isomorphic as of v4.0.0- More detailed article: “Better AngularJS Form

Validation with Mongoose”- Lets you share validation logic between client

and server- Never maintain two code bases in parallel!

Page 28: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Mongoose 4 in Browser JS

Page 29: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Form Logic in Mongoose

Page 30: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

What Should It Do?

Page 31: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

What Should It Do?

Page 32: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

What Should It Do?

Page 33: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Implementation

Page 34: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Key Takeaways- Validation logic is easy to get wrong- Write once, test carefully

“If you want more effective programmers, you will discover that they should not waste their time debugging, they should not introduce the bugs to start with.”

- Edsger Dijkstra of Dijkstra’s Algorithm

Page 35: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Part 4: Compiling for Ionic- Don’t worry, this section will be short- Ionic = AngularJS on Cordova- Hybrid native apps

Page 36: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

I Hate Native Mobile Dev- Worst of both worlds between Swing and

HTML- Feedback cycle is slow: make change ->

compile -> wait… -> click around in simulator

Page 37: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Directives First with Ionic- Ionic lets you write mobile apps in Angular- Write directives once, re-use them on mobile- Mobile dev without the slow feedback

Page 38: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

What’s Tricky about Ionic?- Mobile apps are downloaded once- Shouldn’t use HTTP to load templates

Page 39: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Directives in Ionic- Use gulp-html2js- Designed to compile template HTML into JS

Page 40: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Directives in Ionic- One gulpfile and directive is ready for Ionic!

Page 41: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Directives in Ionic- Very high-level overview- EdX course goes into much more detail

Page 43: MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQuery

Thanks for Listening!Read more at:

thecodebarbarian.comgithub.com/vkarpov15

@code_barbariangithub.com/vkarpov15/node-presentation-examples