why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/why can_t i...

121
Greg Moeck Why Can’t I Test My Javascript?

Upload: hoangcong

Post on 24-Jan-2019

244 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Greg MoeckWhy Can’t I Test My Javascript?

Page 2: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

@gregmoeck

Page 3: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Quick Poll

Page 4: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Question1

Page 5: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Practice TDD/BDD In Ruby/Rails

Page 6: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Practiced TDD Before

Ruby

Page 7: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Question 2

Page 8: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Work Regularly In JavaScript

Page 9: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Practice TDD/BDD In JavaScript

Page 10: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

TDD/BDD In JavaScript(No DOM)

Page 11: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Why You Can’t Test JavaScript

Page 12: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

What Is The

Problem?

Page 13: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

I’ve Heard Many

Excuses

Page 14: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Tools Suck

Page 15: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Browsers Suck

Page 16: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

DOM Sucks

Page 17: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

There Is Another

Issue

Page 18: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Our Tests Are Talking

To Us

Page 19: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

“All Of The Pain That We Feel When Writing Unit Tests

Points At Underlying Design Problems.

Michael Feathers, The Deep Synergy Between Good

Design and Testability

Page 20: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Story Time

Page 21: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

<?php...<div id= “vault_items”> ... $query1 = "SELECT * FROM storage_access_vault_items WHERE access_id = {$_GET[pid]}"; $result1 = mysql_query($query1); $inner_vault_items = array(); while($this_item = mysql_fetch_assoc($result1))   { ?> <div class= “vault_item”> <?= $this_item[‘description’] ?> ... </div><?php } ...</div>?>

Page 22: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

What Do You Say?

Page 23: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

You Need Structure

Page 24: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

JS≠

CSS

Page 25: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

How To Test

JavaScript

Page 26: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Simple Answer

Page 27: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Same As Everything

Else

Page 28: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

What We Want In

Our Tests

Page 29: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Key Question

Page 30: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

What Do Your Tests

Do?

Page 31: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

A. Catch Bugs

Page 32: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

B. Ensure Value

Page 33: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Ensure Value

Page 34: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Two Types Of Value

Page 35: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

External Value

Page 36: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Internal Value

Page 37: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

External Value

End-To-End Acceptance Tests

Page 38: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Running Them

Does It Meet The External Value?

Page 39: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Writing Them

Do We Understand External Value?

Page 40: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Acceptance Tests

Internal Quality?X

Page 41: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Internal Value

Isolated Unit Tests

Page 42: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Writing Them

Feedback On The Quality Of Code

Page 43: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Running Them

Tells Us We Haven’t Broken Any Object

Page 44: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Unit Tests

System Works Together?

X

Page 45: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

All Objects Work Together

Acceptance Tests

Page 46: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

All Objects Work Individually

Unit Tests

Page 47: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

“Unit Tests Tell You That You Built The System Right.

Acceptance / Integration Tests Tell You That You’ve

Built The Right System

Gojko Adzic, Specification by Example

Page 48: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Acceptance Tests

Page 49: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Ensures Value For The User

Page 50: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

(External Value)

Page 51: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Use The System As The User

Would

Page 52: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Searching For An Item', function() { Given('an auction item that is available for sale', function() { var itemTitle = 'item'; beforeEach(function() { Fictum.addResource('Item', {title: itemTitle}); }); When('I search for that auction item', function() { beforeEach(function() { Simulo.fillIn('#mainSearchWidget input[type="text"]', itemTitle); Simulo.clickOn('#mainSearchWidget .submit-search'); });

Then('I should see the auction item within the search results', function(page) { page.within('.search-results', function(page) { expect(page).toHaveContent(itemTitle); }); }); }); }); });});

Page 53: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Searching For An Item', function() { Given('an auction item that is available for sale', function() { var itemTitle = 'item'; beforeEach(function() { Fictum.addResource('Item', {title: itemTitle}); }); When('I search for that auction item', function() { beforeEach(function() { Simulo.fillIn('#mainSearchWidget input[type="text"]', itemTitle); Simulo.clickOn('#mainSearchWidget .submit-search'); });

Then('I should see the auction item within the search results', function(page) { page.within('.search-results', function(page) { expect(page).toHaveContent(itemTitle); }); }); }); }); });});

Page 54: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Searching For An Item', function() { Given('an auction item that is available for sale', function() { var itemTitle = 'item'; beforeEach(function() { Fictum.addResource('Item', {title: itemTitle}); }); When('I search for that auction item', function() { beforeEach(function() { Simulo.fillIn('#mainSearchWidget input[type="text"]', itemTitle); Simulo.clickOn('#mainSearchWidget .submit-search'); });

Then('I should see the auction item within the search results', function(page) { page.within('.search-results', function(page) { expect(page).toHaveContent(itemTitle); }); }); }); }); });});

Page 55: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Searching For An Item', function() { Given('an auction item that is available for sale', function() { var itemTitle = 'item'; beforeEach(function() { Fictum.addResource('Item', {title: itemTitle}); }); When('I search for that auction item', function() { beforeEach(function() { Simulo.fillIn('#mainSearchWidget input[type="text"]', itemTitle); Simulo.clickOn('#mainSearchWidget .submit-search'); });

Then('I should see the auction item within the search results', function(page) { page.within('.search-results', function(page) { expect(page).toHaveContent(itemTitle); }); }); }); }); });});

Page 56: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Searching For An Item', function() { Given('an auction item that is available for sale', function() { var itemTitle = 'item'; beforeEach(function() { Fictum.addResource('Item', {title: itemTitle}); }); When('I search for that auction item', function() { beforeEach(function() { Simulo.fillIn('#mainSearchWidget input[type="text"]', itemTitle); Simulo.clickOn('#mainSearchWidget .submit-search'); });

Then('I should see the auction item within the search results', function(page) { page.within('.search-results', function(page) { expect(page).toHaveContent(itemTitle); }); }); }); }); });});

Page 57: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Searching For An Item', function() { Given('an auction item that is available for sale', function() { var itemTitle = 'item'; beforeEach(function() { Fictum.addResource('Item', {title: itemTitle}); }); When('I search for that auction item', function() { beforeEach(function() { Simulo.fillIn('#mainSearchWidget input[type="text"]', itemTitle); Simulo.clickOn('#mainSearchWidget .submit-search'); });

Then('I should see the auction item within the search results', function(page) { page.within('.search-results', function(page) { expect(page).toHaveContent(itemTitle); }); }); }); }); });});

Page 58: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Searching For An Item', function() { Given('an auction item that is available for sale', function() { var itemTitle = 'item'; beforeEach(function() { Fictum.addResource('Item', {title: itemTitle}); }); When('I search for that auction item', function() { beforeEach(function() { Simulo.fillIn('#mainSearchWidget input[type="text"]', itemTitle); Simulo.clickOn('#mainSearchWidget .submit-search'); });

Then('I should see the auction item within the search results', function(page) { page.within('.search-results', function(page) { expect(page).toHaveContent(itemTitle); }); }); }); }); });});

Page 59: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Searching For An Item', function() { Given('an auction item that is available for sale', function() { var itemTitle = 'item'; beforeEach(function() { Fictum.addResource('Item', {title: itemTitle}); }); When('I search for that auction item', function() { beforeEach(function() { Simulo.fillIn('#mainSearchWidget input[type="text"]', itemTitle); Simulo.clickOn('#mainSearchWidget .submit-search'); });

Then('I should see the auction item within the search results', function(page) { page.within('.search-results', function(page) { expect(page).toHaveContent(itemTitle); }); }); }); }); });});

Page 60: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Poke At The System As The User

Would

Page 61: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Verify The System As The User

Would

Page 62: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Unit Tests

Page 63: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Ensures Value Of The Architecture

Page 64: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

(Internal Value)

Page 65: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Use Object Like A

Collaborator Would

Page 66: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Isolate The

Object

Page 67: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Button View

Page 68: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Button View

Button

Template

Some

Controller

Page 69: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); describe(‘#mouseUp’, function() { describe(‘when the mouse is over the button’, function() { beforeEach(function() { button.set(‘isActive’, true); }); it(‘fires the button action’, function() { var actionSpy = spyOn(controller, ‘someAction’); button.mouseUp();

expect(actionSpy).toHaveBeenCalled(); }); }); });});

Page 70: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); describe(‘#mouseUp’, function() { describe(‘when the mouse is over the button’, function() { beforeEach(function() { button.set(‘isActive’, true); }); it(‘fires the button action’, function() { var actionSpy = spyOn(controller, ‘someAction’); button.mouseUp();

expect(actionSpy).toHaveBeenCalled(); }); }); });});

Page 71: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); describe(‘#mouseUp’, function() { describe(‘when the mouse is over the button’, function() { beforeEach(function() { button.set(‘isActive’, true); }); it(‘fires the button action’, function() { var actionSpy = spyOn(controller, ‘someAction’); button.mouseUp();

expect(actionSpy).toHaveBeenCalled(); }); }); });});

Page 72: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); describe(‘#mouseUp’, function() { describe(‘when the mouse is over the button’, function() { beforeEach(function() { button.set(‘isActive’, true); }); it(‘fires the button action’, function() { var actionSpy = spyOn(controller, ‘someAction’); button.mouseUp();

expect(actionSpy).toHaveBeenCalled(); }); }); });});

Page 73: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); describe(‘#mouseUp’, function() { describe(‘when the mouse is over the button’, function() { beforeEach(function() { button.set(‘isActive’, true); }); it(‘fires the button action’, function() { var actionSpy = spyOn(controller, ‘someAction’); button.mouseUp();

expect(actionSpy).toHaveBeenCalled(); }); }); });});

Page 74: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); describe(‘#mouseUp’, function() { describe(‘when the mouse is over the button’, function() { beforeEach(function() { button.set(‘isActive’, true); }); it(‘fires the button action’, function() { var actionSpy = spyOn(controller, ‘someAction’); button.mouseUp();

expect(actionSpy).toHaveBeenCalled(); }); }); });});

Page 75: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); describe(‘#mouseUp’, function() { describe(‘when the mouse is over the button’, function() { beforeEach(function() { button.set(‘isActive’, true); }); it(‘fires the button action’, function() { var actionSpy = spyOn(controller, ‘someAction’); button.mouseUp();

expect(actionSpy).toHaveBeenCalled(); }); }); });});

Page 76: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); describe(‘#mouseUp’, function() { describe(‘when the mouse is not over the button’, function() { beforeEach(function() { button.set(‘isActive’, false); }); it(‘does not fire the button action’, function() { var actionSpy = spyOn(controller, ‘someAction’); button.mouseUp();

expect(actionSpy).not.toHaveBeenCalled(); }); }); });});

Page 77: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); describe(‘#mouseUp’, function() { describe(‘when the mouse is not over the button’, function() { beforeEach(function() { button.set(‘isActive’, false); }); it(‘does not fire the button action’, function() { var actionSpy = spyOn(controller, ‘someAction’); button.mouseUp();

expect(actionSpy).not.toHaveBeenCalled(); }); }); });});

Page 78: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); describe(‘#mouseUp’, function() { describe(‘when the mouse is not over the button’, function() { beforeEach(function() { button.set(‘isActive’, false); }); it(‘does not fire the button action’, function() { var actionSpy = spyOn(controller, ‘someAction’); button.mouseUp();

expect(actionSpy).not.toHaveBeenCalled(); }); }); });});

Page 79: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); describe(‘#mouseUp’, function() { describe(‘when the mouse is not over the button’, function() { beforeEach(function() { button.set(‘isActive’, false); }); it(‘does not fire the button action’, function() { var actionSpy = spyOn(controller, ‘someAction’); button.mouseUp();

expect(actionSpy).not.toHaveBeenCalled(); }); }); });});

Page 80: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); ... describe(‘#mouseDown’, function() { it(‘it sets the button to be active’, function() { button.mouseDown();

expect(button.get(‘isActive’)).toBe(true); }); });});

Page 81: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); ... describe(‘#mouseDown’, function() { it(‘it sets the button to be active’, function() { button.mouseDown();

expect(button.get(‘isActive’)).toBe(true); }); });});

Page 82: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('ButtonView', function() { var button, controller; beforeEach(function() { controller = {action: function() {}}; button = ButtonView.create({controller: controller}); }); ... describe(‘#mouseDown’, function() { it(‘it sets the button to be active’, function() { button.mouseDown();

expect(button.get(‘isActive’)).toBe(true); }); });});

Page 83: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Ensures A Well

Defined API

Page 84: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Limits Dependencies

Page 85: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Full Example

Page 86: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Some List

Pending Items

Pending Todo

P1 1

Page 87: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Some List

Pending Items

Pending Todo

P1 1

Page 88: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Some List

Pending Items

Pending Todo

P1 1

Page 89: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Some List

Pending Items

Pending Todo

P1 0

Page 90: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Some List

Pending Items

Todo #1

P1 3

Todo #2

Todo #3

Complete All

Page 91: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Some List

Pending Items

Todo #1

P1 3

Todo #2

Todo #3

Complete All

Page 92: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Some List

Pending Items

Todo #1

P1 3

Todo #2

Todo #3

Complete All

Page 93: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Some List

Pending Items

Todo #1

P1 3

Todo #2

Todo #3

Complete All

Page 94: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Some List

Pending Items

Todo #1

P1 0

Todo #2

Todo #3

Complete All

Page 95: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Marking all items in a list complete', function() { Given(‘a list with multiple incomplete items’, function() { beforeEach(function() { Fictum.addResource(‘item’, {isComplete: false}); Fictum.addResource(‘item’, {isComplete: false}); }); When(‘I press the button to mark all items complete’, function() { beforeEach(function() { Simulo.clickOn(‘.select-all-button’); }); Then(‘All of the items should be marked complete’, function() { expect($(‘input:checked’).length).toBe(2); }); Then(‘I should see that no items are still pending’, function() { expect($(‘.remaining-items’).val()).toContain(0); }); }); });});

Page 96: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Marking all items in a list complete', function() { Given(‘a list with multiple incomplete items’, function() { beforeEach(function() { Fictum.addResource(‘item’, {isComplete: false}); Fictum.addResource(‘item’, {isComplete: false}); }); When(‘I press the button to mark all items complete’, function() { beforeEach(function() { Simulo.clickOn(‘.select-all-button’); }); Then(‘All of the items should be marked complete’, function() { expect($(‘input:checked’).length).toBe(2); }); Then(‘I should see that no items are still pending’, function() { expect($(‘.remaining-items’).val()).toContain(0); }); }); });});

Page 97: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Marking all items in a list complete', function() { Given(‘a list with multiple incomplete items’, function() { beforeEach(function() { Fictum.addResource(‘item’, {isComplete: false}); Fictum.addResource(‘item’, {isComplete: false}); }); When(‘I press the button to mark all items complete’, function() { beforeEach(function() { Simulo.clickOn(‘.select-all-button’); }); Then(‘All of the items should be marked complete’, function() { expect($(‘input:checked’).length).toBe(2); }); Then(‘I should see that no items are still pending’, function() { expect($(‘.remaining-items’).val()).toContain(0); }); }); });});

Page 98: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Marking all items in a list complete', function() { Given(‘a list with multiple incomplete items’, function() { beforeEach(function() { Fictum.addResource(‘item’, {isComplete: false}); Fictum.addResource(‘item’, {isComplete: false}); }); When(‘I press the button to mark all items complete’, function() { beforeEach(function() { Simulo.clickOn(‘.select-all-button’); }); Then(‘All of the items should be marked complete’, function() { expect($(‘input:checked’).length).toBe(2); }); Then(‘I should see that no items are still pending’, function() { expect($(‘.remaining-items’).val()).toContain(0); }); }); });});

Page 99: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Marking all items in a list complete', function() { Given(‘a list with multiple incomplete items’, function() { beforeEach(function() { Fictum.addResource(‘item’, {isComplete: false}); Fictum.addResource(‘item’, {isComplete: false}); }); When(‘I press the button to mark all items complete’, function() { beforeEach(function() { Simulo.clickOn(‘.select-all-button’); }); Then(‘All of the items should be marked complete’, function() { expect($(‘input:checked’).length).toBe(2); }); Then(‘I should see that no items are still pending’, function() { expect($(‘.remaining-items’).val()).toContain(0); }); }); });});

Page 100: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Marking all items in a list complete', function() { Given(‘a list with multiple incomplete items’, function() { beforeEach(function() { Fictum.addResource(‘item’, {isComplete: false}); Fictum.addResource(‘item’, {isComplete: false}); }); When(‘I press the button to mark all items complete’, function() { beforeEach(function() { Simulo.clickOn(‘.select-all-button’); }); Then(‘All of the items should be marked complete’, function() { expect($(‘input:checked’).length).toBe(2); }); Then(‘I should see that no items are still pending’, function() { expect($(‘.remaining-items’).val()).toContain(0); }); }); });});

Page 101: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Marking all items in a list complete', function() { Given(‘a list with multiple incomplete items’, function() { beforeEach(function() { Fictum.addResource(‘item’, {isComplete: false}); Fictum.addResource(‘item’, {isComplete: false}); }); When(‘I press the button to mark all items complete’, function() { beforeEach(function() { Simulo.clickOn(‘.select-all-button’); }); Then(‘All of the items should be marked complete’, function() { expect($(‘input:checked’).length).toBe(2); }); Then(‘I should see that no items are still pending’, function() { expect($(‘.remaining-items’).val()).toContain(0); }); }); });});

Page 102: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Marking all items in a list complete', function() { Given(‘a list with multiple incomplete items’, function() { beforeEach(function() { Fictum.addResource(‘item’, {isComplete: false}); Fictum.addResource(‘item’, {isComplete: false}); }); When(‘I press the button to mark all items complete’, function() { beforeEach(function() { Simulo.clickOn(‘.select-all-button’); }); Then(‘All of the items should be marked complete’, function() { expect($(‘input:checked’).length).toBe(2); }); Then(‘I should see that no items are still pending’, function() { expect($(‘.remaining-items’).val()).toContain(0); }); }); });});

Page 103: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Marking all items in a list complete', function() { Given(‘a list with multiple incomplete items’, function() { beforeEach(function() { Fictum.addResource(‘item’, {isComplete: false}); Fictum.addResource(‘item’, {isComplete: false}); }); When(‘I press the button to mark all items complete’, function() { beforeEach(function() { Simulo.clickOn(‘.select-all-button’); }); Then(‘All of the items should be marked complete’, function() { expect($(‘input:checked’).length).toBe(2); }); Then(‘I should see that no items are still pending’, function() { expect($(‘.remaining-items’).val()).toContain(0); }); }); });});

Page 104: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Scenario('Marking all items in a list complete', function() { Given(‘a list with multiple incomplete items’, function() { beforeEach(function() { Fictum.addResource(‘item’, {isComplete: false}); Fictum.addResource(‘item’, {isComplete: false}); }); When(‘I press the button to mark all items complete’, function() { beforeEach(function() { Simulo.clickOn(‘.select-all-button’); }); Then(‘All of the items should be marked complete’, function() { expect($(‘input:checked’).length).toBe(2); }); Then(‘I should see that no items are still pending’, function() { expect($(‘.remaining-items’).val()).toContain(0); }); }); });});

Page 105: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Run It

Page 106: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Error: Button Not

Found

Page 107: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

App.SelectAllCompleteButtonView = SC.Button.extend({ classNames: [‘select-button-view’]});

Page 108: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Run It

Page 109: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Error: Length Of Complete

Page 110: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

describe('App.SelectAllCompleteButtonView', function() { var button, controller; beforeEach(function() { controller = {markAllComplete: function() {}}; button = App.SelectAllCompleteButtonView.create({controller: controller}); }); describe(‘#mouseUp’, function() { it(‘fires the mark all complete action’, function() { var actionSpy = spyOn(controller, ‘markAllComplete’); button.mouseUp();

expect(actionSpy).toHaveBeenCalled(); }); }); });});

Page 111: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Run It

Page 112: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Error: Action Not

Called

Page 113: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

App.SelectAllCompleteButtonView = SC.Button.extend({ classNames: [‘select-button-view’],

mouseUp: function(evt) { this.get(‘controller’).markAllComplete();}

});

Page 114: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Run It

Page 115: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Unit Tests Pass

Page 116: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Error:ControllerUndefined

Page 117: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

App.SelectAllCompleteButtonView = SC.Button.extend({ classNames: [‘select-button-view’],

init: function() { if(!this.get(‘controller’)) this.set(‘controller’, App.listController); },

mouseUp: function(evt) { this.get(‘controller’).markAllComplete();}

});

Page 118: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Run It

Page 119: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Unit Tests Pass

Page 120: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

Error:Unknown Method

Page 121: why cant i test my javascript - assets.en.oreilly.comassets.en.oreilly.com/1/event/59/Why Can_t I Test My JavaScript... Unit Tests Tell You That You Built The System Right. Acceptance

App.listController = SC.ArrayController.create({ ... markAllComplete: function() { }});