+ All Categories
Home > Technology > Back To The Front - Javascript Test Driven Development is between us (workshop)

Back To The Front - Javascript Test Driven Development is between us (workshop)

Date post: 15-May-2015
Category:
Upload: marco-cedaro
View: 771 times
Download: 1 times
Share this document with a friend
Description:
Javascript & browsers have been for years a complex and unsafe environment for a web developer, now we have the right tools to gain control on what we are distributing in our web applications. During the workshop you will learn first-hand basic Javascript Test Driven Development practices including testing, refactoring and related agile practices such as continuous integration and pair programming. presented at italian Back To The Front conference /w @sirLisko
Popular Tags:
112
back to the front javascript tdd is between us
Transcript
Page 1: Back To The Front - Javascript Test Driven Development is between us (workshop)

back to the frontjavascript tdd is between us

Page 2: Back To The Front - Javascript Test Driven Development is between us (workshop)

Who are we?Marco Cedaro javascript pirate, arr

Spreaker Frontend Developer

Luca Lischetti8-bit lover and super hero

(almost) Shazam Frontend Developer

Page 3: Back To The Front - Javascript Test Driven Development is between us (workshop)

Who are we?Marco Cedaro javascript pirate, arr

Spreaker Frontend Developer

Luca Lischetti8-bit lover and super hero

(almost) Shazam Frontend Developer

The content of this workshop do not necessarily reflect the opinion of authors employers

Page 4: Back To The Front - Javascript Test Driven Development is between us (workshop)

Who are we?Marco Cedaro javascript pirate, arr

Spreaker Frontend Developer

Luca Lischetti8-bit lover and super hero

(almost) Shazam Frontend Developer

The content of this workshop do not necessarily reflect the opinion of authors employersAuthors employers are not responsible in any way of authors bad coding and funny spoken english

Page 5: Back To The Front - Javascript Test Driven Development is between us (workshop)

I believe I can fly

Page 6: Back To The Front - Javascript Test Driven Development is between us (workshop)

Fearless & Unconscious

Page 7: Back To The Front - Javascript Test Driven Development is between us (workshop)

Fearless & Unconscious

Page 8: Back To The Front - Javascript Test Driven Development is between us (workshop)

But Life Goes On

Page 9: Back To The Front - Javascript Test Driven Development is between us (workshop)

TDD is about control

Page 10: Back To The Front - Javascript Test Driven Development is between us (workshop)

and about DESIGN too

Page 11: Back To The Front - Javascript Test Driven Development is between us (workshop)

The curious case of JavaScript unit testing

Page 12: Back To The Front - Javascript Test Driven Development is between us (workshop)

Unit Test and Functional Test

Page 13: Back To The Front - Javascript Test Driven Development is between us (workshop)

Pro/Constest

interaction between libraries

control over

codebase

consistency against

external changes

execution time

test integration

Page 14: Back To The Front - Javascript Test Driven Development is between us (workshop)

That's the browsers, baby

Page 15: Back To The Front - Javascript Test Driven Development is between us (workshop)
Page 16: Back To The Front - Javascript Test Driven Development is between us (workshop)

Unit testing is supposed to test a single atomic “unit”

of functionality without

dependencies on anything else

Page 17: Back To The Front - Javascript Test Driven Development is between us (workshop)

This is where you start to run into

serious dependency problems due to the interrelation HTML

and CSS

Unit testing is supposed to test a single atomic “unit”

of functionality without

dependencies on anything else

Page 18: Back To The Front - Javascript Test Driven Development is between us (workshop)

This is where you start to run into

serious dependency problems due to the interrelation HTML

and CSS

Unit testing is supposed to test a single atomic “unit”

of functionality without

dependencies on anything else

What do you test? Usually how the user interface responds

to user input. Actually, the realm of

functional testing

Page 19: Back To The Front - Javascript Test Driven Development is between us (workshop)

How does it work?

Page 20: Back To The Front - Javascript Test Driven Development is between us (workshop)

have you seen LOST?

Page 21: Back To The Front - Javascript Test Driven Development is between us (workshop)
Page 22: Back To The Front - Javascript Test Driven Development is between us (workshop)

Write a new test

Page 23: Back To The Front - Javascript Test Driven Development is between us (workshop)

Write a new test

Run test & let it fail

Page 24: Back To The Front - Javascript Test Driven Development is between us (workshop)

Write a new test

Run test & let it fail

Write the code

Page 25: Back To The Front - Javascript Test Driven Development is between us (workshop)

Write a new test

Run test & let it fail

Write the code

Run test & let it succeed

Page 26: Back To The Front - Javascript Test Driven Development is between us (workshop)

Write a new test

Refactor your code

Run test & let it fail

Write the code

Run test & let it succeed

Page 27: Back To The Front - Javascript Test Driven Development is between us (workshop)

Coding

Page 28: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 29: Back To The Front - Javascript Test Driven Development is between us (workshop)

pub.. What?

Page 30: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 31: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$.watch("customEvent", function(obj) {! //DO STUFF});

_$.notify("customEvent");_$.notify("customEvent", { prop : "value" });

Page 32: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$.watch("customEvent", function(obj) {! //DO STUFF});

_$.notify("customEvent");_$.notify("customEvent", { prop : "value" });

Page 33: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 34: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 35: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 36: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 37: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 38: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 39: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$.watch("customEvent", function(obj) {! //DO STUFF});

_$.notify("customEvent");_$.notify("customEvent", { prop : "value" });

Page 40: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 41: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 42: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 43: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 44: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 45: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 46: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 47: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function (_) {

return {

notify: function (a, b, c, d) { for (d = -1, c = [].concat(_[a]); c[++d];) c[d](b);

}, watch: function (a, b) {

(_[a] || (_[a] = [])).push(b); }

}

})({});

Page 48: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotify: function(){!!! ! var a = 0;!! ! _$.watch('testNotify', function(){ a = 1; });! ! _$.notify('testNotify');

! ! assertEquals(1, a);! },! ! !! testNotifyWithMemo: function(){! !! ! var a = 0 ;!! ! _$.watch('testNotify', function(memo){ a = memo.test; });! ! _$.notify('testNotify', {test: 1});

! ! assertEquals(1, a);! },[...]

Page 49: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotify: function(){!!! ! var a = 0;!! ! _$.watch('testNotify', function(){ a = 1; });! ! _$.notify('testNotify');

! ! assertEquals(1, a);! },! ! !! testNotifyWithMemo: function(){! !! ! var a = 0 ;!! ! _$.watch('testNotify', function(memo){ a = memo.test; });! ! _$.notify('testNotify', {test: 1});

! ! assertEquals(1, a);! },[...]

Page 50: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotify: function(){!!! ! var a = 0;!! ! _$.watch('testNotify', function(){ a = 1; });! ! _$.notify('testNotify');

! ! assertEquals(1, a);! },! ! !! testNotifyWithMemo: function(){! !! ! var a = 0 ;!! ! _$.watch('testNotify', function(memo){ a = memo.test; });! ! _$.notify('testNotify', {test: 1});

! ! assertEquals(1, a);! },[...]

Page 51: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotify: function(){!!! ! var a = 0;!! ! _$.watch('testNotify', function(){ a = 1; });! ! _$.notify('testNotify');

! ! assertEquals(1, a);! },! ! !! testNotifyWithMemo: function(){! !! ! var a = 0 ;!! ! _$.watch('testNotify', function(memo){ a = memo.test; });! ! _$.notify('testNotify', {test: 1});

! ! assertEquals(1, a);! },[...]

Page 52: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotify: function(){!!! ! var a = 0;!! ! _$.watch('testNotify', function(){ a = 1; });! ! _$.notify('testNotify');

! ! assertEquals(1, a);! },! ! !! testNotifyWithMemo: function(){! !! ! var a = 0 ;!! ! _$.watch('testNotify', function(memo){ a = memo.test; });! ! _$.notify('testNotify', {test: 1});

! ! assertEquals(1, a);! },[...]

Page 53: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotify: function(){!!! ! var a = 0;!! ! _$.watch('testNotify', function(){ a = 1; });! ! _$.notify('testNotify');

! ! assertEquals(1, a);! },! ! !! testNotifyWithMemo: function(){! !! ! var a = 0 ;!! ! _$.watch('testNotify', function(memo){ a = memo.test; });! ! _$.notify('testNotify', {test: 1});

! ! assertEquals(1, a);! },[...]

Page 54: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotify: function(){!!! ! var a = 0;!! ! _$.watch('testNotify', function(){ a = 1; });! ! _$.notify('testNotify');

! ! assertEquals(1, a);! },! ! !! testNotifyWithMemo: function(){! !! ! var a = 0 ;!! ! _$.watch('testNotify', function(memo){ a = memo.test; });! ! _$.notify('testNotify', {test: 1});

! ! assertEquals(1, a);! },[...]

Page 55: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

! testNotifyWithMultipleWhatches: function(){!!! ! var a = 0, b = 0;!! ! _$.watch('testNotify', function(memo){ a = memo.test; });! ! _$.watch('testNotify', function(memo){ b = memo.test; });! ! _$.notify('testNotify', {test: 1});

! ! assertEquals(1, a);! ! assertEquals(1, b);! },[...]

Page 56: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

! testNotifyWithMultipleWhatches: function(){!!! ! var a = 0, b = 0;!! ! _$.watch('testNotify', function(memo){ a = memo.test; });! ! _$.watch('testNotify', function(memo){ b = memo.test; });! ! _$.notify('testNotify', {test: 1});

! ! assertEquals(1, a);! ! assertEquals(1, b);! },[...]

Page 57: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

! testNotifyWithMultipleWhatches: function(){!!! ! var a = 0, b = 0;!! ! _$.watch('testNotify', function(memo){ a = memo.test; });! ! _$.watch('testNotify', function(memo){ b = memo.test; });! ! _$.notify('testNotify', {test: 1});

! ! assertEquals(1, a);! ! assertEquals(1, b);! },[...]

Page 58: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotifyWithMultipleWhatchesNested: function(){!!! ! var a = 0, b = 0, c=0;!! ! _$.watch('testNotify', function(memo){

a = memo.test;! ! ! _$.watch('testNotify', function(memo){! ! ! ! if (b<2){ b = memo.test; }

else { c = memo.test; }! ! ! });! ! });! !! ! _$.notify('testNotify', {test: 1});! ! assertEquals(1, a);! ! assertEquals(0, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 2});! ! assertEquals(2, a);! ! assertEquals(2, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 3});! ! assertEquals(3, a);! ! assertEquals(2, b);! ! assertEquals(3, c);! },[...]

Page 59: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotifyWithMultipleWhatchesNested: function(){!!! ! var a = 0, b = 0, c=0;!! ! _$.watch('testNotify', function(memo){

a = memo.test;! ! ! _$.watch('testNotify', function(memo){! ! ! ! if (b<2){ b = memo.test; }

else { c = memo.test; }! ! ! });! ! });! !! ! _$.notify('testNotify', {test: 1});! ! assertEquals(1, a);! ! assertEquals(0, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 2});! ! assertEquals(2, a);! ! assertEquals(2, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 3});! ! assertEquals(3, a);! ! assertEquals(2, b);! ! assertEquals(3, c);! },[...]

Page 60: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotifyWithMultipleWhatchesNested: function(){!!! ! var a = 0, b = 0, c=0;!! ! _$.watch('testNotify', function(memo){

a = memo.test;! ! ! _$.watch('testNotify', function(memo){! ! ! ! if (b<2){ b = memo.test; }

else { c = memo.test; }! ! ! });! ! });! !! ! _$.notify('testNotify', {test: 1});! ! assertEquals(1, a);! ! assertEquals(0, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 2});! ! assertEquals(2, a);! ! assertEquals(2, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 3});! ! assertEquals(3, a);! ! assertEquals(2, b);! ! assertEquals(3, c);! },[...]

Page 61: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotifyWithMultipleWhatchesNested: function(){!!! ! var a = 0, b = 0, c=0;!! ! _$.watch('testNotify', function(memo){

a = memo.test;! ! ! _$.watch('testNotify', function(memo){! ! ! ! if (b<2){ b = memo.test; }

else { c = memo.test; }! ! ! });! ! });! !! ! _$.notify('testNotify', {test: 1});! ! assertEquals(1, a);! ! assertEquals(0, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 2});! ! assertEquals(2, a);! ! assertEquals(2, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 3});! ! assertEquals(3, a);! ! assertEquals(2, b);! ! assertEquals(3, c);! },[...]

Page 62: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotifyWithMultipleWhatchesNested: function(){!!! ! var a = 0, b = 0, c=0;!! ! _$.watch('testNotify', function(memo){

a = memo.test;! ! ! _$.watch('testNotify', function(memo){! ! ! ! if (b<2){ b = memo.test; } ! ! ! ! else { c = memo.test; }! ! ! });! ! });! !! ! _$.notify('testNotify', {test: 1});! ! assertEquals(1, a);! ! assertEquals(0, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 2});! ! assertEquals(2, a);! ! assertEquals(2, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 3});! ! assertEquals(3, a);! ! assertEquals(2, b);! ! assertEquals(3, c);! },[...]

Page 63: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotifyWithMultipleWhatchesNested: function(){!!! ! var a = 0, b = 0, c=0;!! ! _$.watch('testNotify', function(memo){

a = memo.test;! ! ! _$.watch('testNotify', function(memo){! ! ! ! if (b<2){ b = memo.test; }

else { c = memo.test; }! ! ! });! ! });! !! ! _$.notify('testNotify', {test: 1});! ! assertEquals(1, a);! ! assertEquals(0, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 2});! ! assertEquals(2, a);! ! assertEquals(2, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 3});! ! assertEquals(3, a);! ! assertEquals(2, b);! ! assertEquals(3, c);! },[...]

Page 64: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotifyWithMultipleWhatchesNested: function(){!!! ! var a = 0, b = 0, c=0;!! ! _$.watch('testNotify', function(memo){

a = memo.test;! ! ! _$.watch('testNotify', function(memo){! ! ! ! if (b<2){ b = memo.test; }

else { c = memo.test; }! ! ! });! ! });! !! ! _$.notify('testNotify', {test: 1});! ! assertEquals(1, a);! ! assertEquals(0, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 2});! ! assertEquals(2, a);! ! assertEquals(2, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 3});! ! assertEquals(3, a);! ! assertEquals(2, b);! ! assertEquals(3, c);! },[...]

Page 65: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotifyWithMultipleWhatchesNested: function(){!!! ! var a = 0, b = 0, c=0;!! ! _$.watch('testNotify', function(memo){

a = memo.test;! ! ! _$.watch('testNotify', function(memo){! ! ! ! if (b<2){ b = memo.test; }

else { c = memo.test; }! ! ! });! ! });! !! ! _$.notify('testNotify', {test: 1});! ! assertEquals(1, a);! ! assertEquals(0, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 2});! ! assertEquals(2, a);! ! assertEquals(2, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 3});! ! assertEquals(3, a);! ! assertEquals(2, b);! ! assertEquals(3, c);! },[...]

Page 66: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotifyWithMultipleWhatchesNested: function(){!!! ! var a = 0, b = 0, c=0;!! ! _$.watch('testNotify', function(memo){

a = memo.test;! ! ! _$.watch('testNotify', function(memo){! ! ! ! if (b<2){ b = memo.test; }

else { c = memo.test; }! ! ! });! ! });! !! ! _$.notify('testNotify', {test: 1});! ! assertEquals(1, a);! ! assertEquals(0, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 2});! ! assertEquals(2, a);! ! assertEquals(2, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 3});! ! assertEquals(3, a);! ! assertEquals(2, b);! ! assertEquals(3, c);! },[...]

Page 67: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testNotifyWithMultipleWhatchesNested: function(){!!! ! var a = 0, b = 0, c=0;!! ! _$.watch('testNotify', function(memo){

a = memo.test;! ! ! _$.watch('testNotify', function(memo){! ! ! ! if (b<2){ b = memo.test; }

else { c = memo.test; }! ! ! });! ! });! !! ! _$.notify('testNotify', {test: 1});! ! assertEquals(1, a);! ! assertEquals(0, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 2});! ! assertEquals(2, a);! ! assertEquals(2, b);! ! assertEquals(0, c);! !! ! _$.notify('testNotify', {test: 3});! ! assertEquals(3, a);! ! assertEquals(2, b);! ! assertEquals(3, c);! },[...]

Page 68: Back To The Front - Javascript Test Driven Development is between us (workshop)

There's no parachute

There's a major bug, let's take another look at the code...

Page 69: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 70: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }! ! ! registered[event].push(handler);! ! }

};})();

Page 71: Back To The Front - Javascript Test Driven Development is between us (workshop)

There's no parachute

How would you write a test to check it?

Page 72: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

! testNotifyWithMultipleWhatches: function(){!!! ! var a = 0, b = 0;!! ! _$.watch('testNotify', function(memo){ a = memo.test; });! ! _$.watch('testNotify', function(memo){ b = memo.test; });! ! _$.notify('testNotify', {test: 1});

! ! assertEquals(1, a);! ! assertEquals(1, b);! },[...]

Page 73: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

! testWhatchWithoutHandler: function(){! !! ! var a = 0, b = 0;!! ! _$.watch('testNotify', function(memo){ a = memo.test; });! ! _$.watch('testNotify');! ! _$.watch('testNotify', function(memo){ b = memo.test; });! ! _$.notify('testNotify', {test: 1});

! ! assertEquals(1, a);! ! assertEquals(1, b);! },[...]

Page 74: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function() {var registered = {};

return {

! ! notify: function(event, memo) {! ! ! if (registered[event] instanceof Array){! ! ! ! var handlers = [].concat(registered[event]);

! ! ! ! for (var i=0, handler; (handler = handlers[i]); i++){! ! ! ! ! handler.call(this, memo);! ! ! ! }! ! ! }! ! },! ! !! ! watch: function(event, handler) {! ! ! if (handler) {

if (typeof registered[event] === "undefined"){! ! ! ! registered[event] = [];! ! ! }

! ! ! registered[event].push(handler);}

! ! }

};})();

Page 75: Back To The Front - Javascript Test Driven Development is between us (workshop)

_$ = (function (_) {

return {

notify: function (a, b, c, d) { for (d = -1, c = [].concat(_[a]); c[++d];) c[d](b); },

watch: function (a, b) { if(b)(_[a] || (_[a] = [])).push(b); }

}

})({});

Page 76: Back To The Front - Javascript Test Driven Development is between us (workshop)

Spy, Stub & Mock

Page 77: Back To The Front - Javascript Test Driven Development is between us (workshop)

Fake objects & methods

Page 78: Back To The Front - Javascript Test Driven Development is between us (workshop)

Sinon.js

Page 79: Back To The Front - Javascript Test Driven Development is between us (workshop)
Page 80: Back To The Front - Javascript Test Driven Development is between us (workshop)

a function that records arguments,

return value, the value of this and

exception thrown (if any) for all its calls.

Spies

Page 81: Back To The Front - Javascript Test Driven Development is between us (workshop)

functions (spies) with pre-programmed

behavior.

Stuba function that

records arguments, return value, the value of this and

exception thrown (if any) for all its calls.

Spies

Page 82: Back To The Front - Javascript Test Driven Development is between us (workshop)

functions (spies) with pre-programmed

behavior.

Stuba function that

records arguments, return value, the value of this and

exception thrown (if any) for all its calls.

Spiesfunctions (spies) with

pre-programmed behavior (stubs) as

well as pre-programmed expectations.

Mock

Page 83: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testMyLibRegistersToSystemOnEvent: function(){!!! ! var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! assertTrue(spy.calledWith('SysyemOn'));! },! ! !! testMyLibLoggedNotLogged: function(){! !! ! var stub = sinon.stub(User, 'isLogged');!! ! stub.returns(true);! ! //DO STUFF && ASSERTIONS

! ! stub.returns(false);! ! //DO STUFF && ASSERTIONS

! },[...]

Page 84: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testMyLibRegistersToSystemOnEvent: function(){!!! ! var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! assertTrue(spy.calledWith('SystemOn'));! },! ! !! testMyLibLoggedNotLogged: function(){! !! ! var stub = sinon.stub(User, 'isLogged');!! ! stub.returns(true);! ! //DO STUFF && ASSERTIONS

! ! stub.returns(false);! ! //DO STUFF && ASSERTIONS

! },[...]

Page 85: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

"test MyLib Registers To 'SystemOn' Event": function(){! !! ! var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! assertTrue(spy.calledWith('SystemOn'));! },! ! !! testMyLibLoggedNotLogged: function(){! !! ! var stub = sinon.stub(User, 'isLogged');!! ! stub.returns(true);! ! //DO STUFF && ASSERTIONS

! ! stub.returns(false);! ! //DO STUFF && ASSERTIONS

! },[...]

Page 86: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testMyLibRegistersToSystemOnEvent: function(){!!! ! var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! assertTrue(spy.calledWith('SystemOn'));! },! ! !! testMyLibLoggedNotLogged: function(){! !! ! var stub = sinon.stub(User, 'isLogged');!! ! stub.returns(true);! ! //DO STUFF && ASSERTIONS

! ! stub.returns(false);! ! //DO STUFF && ASSERTIONS

! },[...]

Page 87: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testMyLibRegistersToSystemOnEvent: function(){!!! ! var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! assertTrue(spy.calledWith('SystemOn'));! },! ! !! testMyLibLoggedNotLogged: function(){! !! ! var stub = sinon.stub(User, 'isLogged');!! ! stub.returns(true);! ! //DO STUFF && ASSERTIONS

! ! stub.returns(false);! ! //DO STUFF && ASSERTIONS

! },[...]

Page 88: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testMyLibRegistersToSystemOnEvent: function(){!!! ! var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! assertTrue(spy.calledWith('SystemOn'));! },! ! !! testMyLibLoggedNotLogged: function(){! !! ! var stub = sinon.stub(User, 'isLogged');!! ! stub.returns(true);! ! //DO STUFF && ASSERTIONS

! ! stub.returns(false);! ! //DO STUFF && ASSERTIONS

! },[...]

Page 89: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testMyLibRegistersToSystemOnEvent: function(){!!! ! var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! assertTrue(spy.calledWith('SystemOn'));! },! ! !! testMyLibLoggedNotLogged: function(){! !! ! var stub = sinon.stub(User, 'isLogged');!! ! stub.returns(true);! ! //DO STUFF && ASSERTIONS

! ! stub.returns(false);! ! //DO STUFF && ASSERTIONS

! },[...]

Page 90: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testMyLibRegistersToSystemOnEvent: function(){!!! ! var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! assertTrue(spy.calledWith('SystemOn'));! },! ! !! testMyLibLoggedNotLogged: function(){! !! ! var stub = sinon.stub(User, 'isLogged');!! ! stub.returns(true);! ! //DO STUFF && ASSERTIONS

! ! stub.returns(false);! ! //DO STUFF && ASSERTIONS

! },[...]

Page 91: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testMyLibRegistersToSystemOnEvent: function(){!!! ! var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! assertTrue(spy.calledWith('SystemOn'));! },! ! !! testMyLibLoggedNotLogged: function(){! !! ! var stub = sinon.stub(User, 'isLogged');!! ! stub.returns(true);! ! //DO STUFF && ASSERTIONS

! ! stub.returns(false);! ! //DO STUFF && ASSERTIONS

! },[...]

Page 92: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testMyLibRegistersToSystemOnEvent: function(){!!! ! var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! assertTrue(spy.calledWith('SystemOn'));! },! ! !! testMyLibLoggedNotLogged: function(){! !! ! var stub = sinon.stub(User, 'isLogged');!! ! stub.returns(true);! ! //DO STUFF && ASSERTIONS

! ! stub.returns(false);! ! //DO STUFF && ASSERTIONS

! },[...]

Page 93: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

testMyLibRegistersToSystemOnEvent: function(){!!! ! var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! assertTrue(spy.calledWith('SystemOn'));! },[...]

Page 94: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

//testMyLibRegistersToSystemOnEvent: function(){! !! ! //var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! //assertTrue(spy.calledWith('SysyemOn'));! //},!! testMyLibRegistersToSystemOnEvent: function(){!!! ! var mock = sinon.mock(_$);! ! mock.expect('watch').calledWith('SysyemOn');! ! //DO STUFF TO INIT YOUR LIB! ! mock.verify();! },[...]

Page 95: Back To The Front - Javascript Test Driven Development is between us (workshop)

[...]

//testMyLibRegistersToSystemOnEvent: function(){! !! ! //var spy = sinon.spy(_$, 'watch');!! ! //DO STUFF TO INIT YOUR LIB! ! //assertTrue(spy.calledWith('SysyemOn'));! //},!! testMyLibRegistersToSystemOnEvent: function(){!!! ! var mock = sinon.mock(_$);! ! mock.expect('watch').calledWith('SysyemOn');! ! //DO STUFF TO INIT YOUR LIB! ! mock.verify();! },[...]

Page 96: Back To The Front - Javascript Test Driven Development is between us (workshop)

What are we going to do

Page 97: Back To The Front - Javascript Test Driven Development is between us (workshop)

cheat

config

dist

libs

tools

workshop

build.sh

build.bat

readme.txt

svn co https://svn.dsgn.it/jstdd

Page 98: Back To The Front - Javascript Test Driven Development is between us (workshop)

cheat

config

dist

libs

tools

workshop

build.sh

build.bat

readme.txt

/config/browsers.prop

build.xml

default.prop

svn co https://svn.dsgn.it/jstdd

Page 99: Back To The Front - Javascript Test Driven Development is between us (workshop)

/libs/base.js

sinon.js

cheat

config

dist

libs

tools

workshop

build.sh

build.bat

readme.txt

/config/browsers.prop

build.xml

default.prop

svn co https://svn.dsgn.it/jstdd

Page 100: Back To The Front - Javascript Test Driven Development is between us (workshop)

/libs/base.js

sinon.js

cheat

config

dist

libs

tools

workshop

build.sh

build.bat

readme.txt

/tools/ant

browser

jslint

JsTestDriver

/config/browsers.prop

build.xml

default.prop

svn co https://svn.dsgn.it/jstdd

Page 101: Back To The Front - Javascript Test Driven Development is between us (workshop)

/libs/base.js

sinon.js

cheat

config

dist

libs

tools

workshop

build.sh

build.bat

readme.txt

/tools/ant

browser

jslint

JsTestDriver

/workshop/append

find

syntax

/config/browsers.prop

build.xml

default.prop

svn co https://svn.dsgn.it/jstdd

Page 102: Back To The Front - Javascript Test Driven Development is between us (workshop)

/libs/base.js

sinon.js

cheat

config

dist

libs

tools

workshop

build.sh

build.bat

readme.txt

/tools/ant

browser

jslint

JsTestDriver

/workshop/append

find

syntax

/append/src

append.js

test

append.test.js

jsTestDriver.conf

/config/browsers.prop

build.xml

default.prop

svn co https://svn.dsgn.it/jstdd

Page 103: Back To The Front - Javascript Test Driven Development is between us (workshop)

/libs/base.js

sinon.js

cheat

config

dist

libs

tools

workshop

build.sh

build.bat

readme.txt

/tools/ant

browser

jslint

JsTestDriver

/workshop/append

find

syntax

/append/src

append.js

test

append.test.js

jsTestDriver.conf

/config/browsers.prop

build.xml

default.prop

svn co https://svn.dsgn.it/jstdd

build append | sh build.sh append

Page 104: Back To The Front - Javascript Test Driven Development is between us (workshop)

A simple modular event driven appa javascript code highlighter with 3 components:

syntax highlighterselected word highlighterfinder

Page 105: Back To The Front - Javascript Test Driven Development is between us (workshop)

First step - syntaxwatch a SRC_LOADED event with the memo {file:'{{FILE_SRC}}'}

highlight (wrap in a span with the keyword itself as classname)

function => <span class="function">function</span>

var, if ... else, for, return, ...

notify SRC_READY with the memo {file:'{{EDITED_SRC}}'}

Page 106: Back To The Front - Javascript Test Driven Development is between us (workshop)

Second step - appendwatch previous SRC_READY

create a div[id="src_container"] and fill it with the source

append to document

llisten dblclick event and notify SRC_HIGHLIGHT with this memo: {keyword:'{{HIGHLIGHTED_WORD}}'}

manage the SRC_HIGHLIGHT notification <span class="highlight">{{HIGHLIGHTED_KEYWORKD}}</span>

Page 107: Back To The Front - Javascript Test Driven Development is between us (workshop)

Third step - findwatch to SRC_READY

create a div[id=form] and append inside of it

input[type=text][id=search]

input[type=button][id=submitBtn][value=Search]

append the div to the document

on click on submit notify a SRC_HIGHLIGHT event with memo {keyword:"{{SEARCHED_TEXT}}"}

Page 108: Back To The Front - Javascript Test Driven Development is between us (workshop)

Where are we now?

Page 109: Back To The Front - Javascript Test Driven Development is between us (workshop)

Costs of change

Page 110: Back To The Front - Javascript Test Driven Development is between us (workshop)

I Build So Consistently

Page 111: Back To The Front - Javascript Test Driven Development is between us (workshop)

I Build So Consistently

Identify Build

Share make it Continuous

Page 112: Back To The Front - Javascript Test Driven Development is between us (workshop)

Recommended