Skip to content

Commit

Permalink
For 328: dropping testing of events, unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Mar 23, 2013
1 parent 94bb6a1 commit 9d58427
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 487 deletions.
Binary file modified backbone-fundamentals.epub
Binary file not shown.
124 changes: 0 additions & 124 deletions backbone-fundamentals.md
Original file line number Diff line number Diff line change
Expand Up @@ -10165,130 +10165,6 @@ asyncTest('Can wire up view methods to DOM elements.', function() {
});
```
### Event
For events, we may want to test a few different use cases:
* Extending plain objects to support custom events
* Binding and triggering custom events on objects
* Passing along arguments to callbacks when events are triggered
* Binding a passed context to an event callback
* Removing custom events
and a few others that will be detailed in our module below:
```javascript
module( 'About Backbone.Events', {
setup: function() {
this.obj = {};
_.extend( this.obj, Backbone.Events );
this.obj.off(); // remove all custom events before each test is run.
}
});

test('Can extend JavaScript objects to support custom events.', function() {
expect(3);

var basicObject = {};

// How would you give basicObject these functions?
_.extend( basicObject, Backbone.Events );

equal( typeof basicObject.on, 'function' );
equal( typeof basicObject.off, 'function' );
equal( typeof basicObject.trigger, 'function' );
});

test('Allows us to bind and trigger custom named events on an object.', function() {
expect( 1 );

var callback = this.spy();

this.obj.on( 'basic event', callback );
this.obj.trigger( 'basic event' );

// How would you cause the callback for this custom event to be called?
ok( callback.called );
});

test('Also passes along any arguments to the callback when an event is triggered.', function() {
expect( 1 );

var passedArgs = [];

this.obj.on('some event', function() {
for (var i = 0; i < arguments.length; i++) {
passedArgs.push( arguments[i] );
}
});

this.obj.trigger( 'some event', 'arg1', 'arg2' );

deepEqual( passedArgs, ['arg1', 'arg2'] );
});


test('Can also bind the passed context to the event callback.', function() {
expect( 1 );

var foo = { color: 'blue' };
var changeColor = function() {
this.color = 'red';
};

// How would you get 'this.color' to refer to 'foo' in the changeColor function?
this.obj.on( 'an event', changeColor, foo );
this.obj.trigger( 'an event' );

equal( foo.color, 'red' );
});

test( 'Uses `all` as a special event name to capture all events bound to the object.', function() {
expect( 2 );

var callback = this.spy();

this.obj.on( 'all', callback );
this.obj.trigger( 'custom event 1' );
this.obj.trigger( 'custom event 2' );

equal( callback.callCount, 2 );
equal( callback.getCall(0).args[0], 'custom event 1' );
});

test('Also can remove custom events from objects.', function() {
expect( 5 );

var spy1 = this.spy();
var spy2 = this.spy();
var spy3 = this.spy();

this.obj.on( 'foo', spy1 );
this.obj.on( 'bar', spy1 );
this.obj.on( 'foo', spy2 );
this.obj.on( 'foo', spy3 );

// How do you unbind just a single callback for the event?
this.obj.off( 'foo', spy1 );
this.obj.trigger( 'foo' );

ok( spy2.called );

// How do you unbind all callbacks tied to the event with a single method
this.obj.off( 'foo' );
this.obj.trigger( 'foo' );

ok( spy2.callCount, 1 );
ok( spy2.calledOnce, 'Spy 2 called once' );
ok( spy3.calledOnce, 'Spy 3 called once' );

// How do you unbind all callbacks and events tied to the object with a single method?
this.obj.off( 'bar' );
this.obj.trigger( 'bar' );

equal( spy1.callCount, 0 );
});
```
### App
Expand Down
118 changes: 0 additions & 118 deletions backbone-fundamentals.rtf
Original file line number Diff line number Diff line change
Expand Up @@ -7894,124 +7894,6 @@ asyncTest('Can wire up view methods to DOM elements.', function() \{\line
$('#todoList li input.check').click();\line
equal( this.todoView.model.get('done'), true );\line
\});\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs28 Event\par}
{\pard \ql \f0 \sa180 \li0 \fi0 For events, we may want to test a few different use cases:\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab Extending plain objects to support custom events\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab Binding and triggering custom events on objects\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab Passing along arguments to callbacks when events are triggered\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab Binding a passed context to an event callback\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab Removing custom events\sa180\par}
{\pard \ql \f0 \sa180 \li0 \fi0 and a few others that will be detailed in our module below:\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \f1 module( 'About Backbone.Events', \{\line
setup: function() \{\line
this.obj = \{\};\line
_.extend( this.obj, Backbone.Events );\line
this.obj.off(); // remove all custom events before each test is run.\line
\}\line
\});\line
\line
test('Can extend JavaScript objects to support custom events.', function() \{\line
expect(3);\line
\line
var basicObject = \{\};\line
\line
// How would you give basicObject these functions?\line
_.extend( basicObject, Backbone.Events );\line
\line
equal( typeof basicObject.on, 'function' );\line
equal( typeof basicObject.off, 'function' );\line
equal( typeof basicObject.trigger, 'function' );\line
\});\line
\line
test('Allows us to bind and trigger custom named events on an object.', function() \{\line
expect( 1 );\line
\line
var callback = this.spy();\line
\line
this.obj.on( 'basic event', callback );\line
this.obj.trigger( 'basic event' );\line
\line
// How would you cause the callback for this custom event to be called?\line
ok( callback.called );\line
\});\line
\line
test('Also passes along any arguments to the callback when an event is triggered.', function() \{\line
expect( 1 );\line
\line
var passedArgs = [];\line
\line
this.obj.on('some event', function() \{\line
for (var i = 0; i < arguments.length; i++) \{\line
passedArgs.push( arguments[i] );\line
\}\line
\});\line
\line
this.obj.trigger( 'some event', 'arg1', 'arg2' );\line
\line
deepEqual( passedArgs, ['arg1', 'arg2'] );\line
\});\line
\line
\line
test('Can also bind the passed context to the event callback.', function() \{\line
expect( 1 );\line
\line
var foo = \{ color: 'blue' \};\line
var changeColor = function() \{\line
this.color = 'red';\line
\};\line
\line
// How would you get 'this.color' to refer to 'foo' in the changeColor function?\line
this.obj.on( 'an event', changeColor, foo );\line
this.obj.trigger( 'an event' );\line
\line
equal( foo.color, 'red' );\line
\});\line
\line
test( 'Uses `all` as a special event name to capture all events bound to the object.', function() \{\line
expect( 2 );\line
\line
var callback = this.spy();\line
\line
this.obj.on( 'all', callback );\line
this.obj.trigger( 'custom event 1' );\line
this.obj.trigger( 'custom event 2' );\line
\line
equal( callback.callCount, 2 );\line
equal( callback.getCall(0).args[0], 'custom event 1' );\line
\});\line
\line
test('Also can remove custom events from objects.', function() \{\line
expect( 5 );\line
\line
var spy1 = this.spy();\line
var spy2 = this.spy();\line
var spy3 = this.spy();\line
\line
this.obj.on( 'foo', spy1 );\line
this.obj.on( 'bar', spy1 );\line
this.obj.on( 'foo', spy2 );\line
this.obj.on( 'foo', spy3 );\line
\line
// How do you unbind just a single callback for the event?\line
this.obj.off( 'foo', spy1 );\line
this.obj.trigger( 'foo' );\line
\line
ok( spy2.called );\line
\line
// How do you unbind all callbacks tied to the event with a single method\line
this.obj.off( 'foo' );\line
this.obj.trigger( 'foo' );\line
\line
ok( spy2.callCount, 1 );\line
ok( spy2.calledOnce, 'Spy 2 called once' );\line
ok( spy3.calledOnce, 'Spy 3 called once' );\line
\line
// How do you unbind all callbacks and events tied to the object with a single method?\line
this.obj.off( 'bar' );\line
this.obj.trigger( 'bar' );\line
\line
equal( spy1.callCount, 0 );\line
\});\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs28 App\par}
{\pard \ql \f0 \sa180 \li0 \fi0 It can also be useful to write tests for any application bootstrap you may have in place. For the following module, our setup instantiates and appends to a TodoApp view and we can test anything from local instances of views being correctly defined to application interactions correctly resulting in changes to instances of local collections.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \f1 module( 'About Backbone Applications' , \{\line
Expand Down
124 changes: 0 additions & 124 deletions chapters/13-sinonjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,130 +460,6 @@ asyncTest('Can wire up view methods to DOM elements.', function() {
});
```

### Event

For events, we may want to test a few different use cases:

* Extending plain objects to support custom events
* Binding and triggering custom events on objects
* Passing along arguments to callbacks when events are triggered
* Binding a passed context to an event callback
* Removing custom events

and a few others that will be detailed in our module below:

```javascript
module( 'About Backbone.Events', {
setup: function() {
this.obj = {};
_.extend( this.obj, Backbone.Events );
this.obj.off(); // remove all custom events before each test is run.
}
});

test('Can extend JavaScript objects to support custom events.', function() {
expect(3);

var basicObject = {};

// How would you give basicObject these functions?
_.extend( basicObject, Backbone.Events );

equal( typeof basicObject.on, 'function' );
equal( typeof basicObject.off, 'function' );
equal( typeof basicObject.trigger, 'function' );
});

test('Allows us to bind and trigger custom named events on an object.', function() {
expect( 1 );

var callback = this.spy();

this.obj.on( 'basic event', callback );
this.obj.trigger( 'basic event' );

// How would you cause the callback for this custom event to be called?
ok( callback.called );
});

test('Also passes along any arguments to the callback when an event is triggered.', function() {
expect( 1 );

var passedArgs = [];

this.obj.on('some event', function() {
for (var i = 0; i < arguments.length; i++) {
passedArgs.push( arguments[i] );
}
});

this.obj.trigger( 'some event', 'arg1', 'arg2' );

deepEqual( passedArgs, ['arg1', 'arg2'] );
});


test('Can also bind the passed context to the event callback.', function() {
expect( 1 );

var foo = { color: 'blue' };
var changeColor = function() {
this.color = 'red';
};

// How would you get 'this.color' to refer to 'foo' in the changeColor function?
this.obj.on( 'an event', changeColor, foo );
this.obj.trigger( 'an event' );

equal( foo.color, 'red' );
});

test( 'Uses `all` as a special event name to capture all events bound to the object.', function() {
expect( 2 );

var callback = this.spy();

this.obj.on( 'all', callback );
this.obj.trigger( 'custom event 1' );
this.obj.trigger( 'custom event 2' );

equal( callback.callCount, 2 );
equal( callback.getCall(0).args[0], 'custom event 1' );
});

test('Also can remove custom events from objects.', function() {
expect( 5 );

var spy1 = this.spy();
var spy2 = this.spy();
var spy3 = this.spy();

this.obj.on( 'foo', spy1 );
this.obj.on( 'bar', spy1 );
this.obj.on( 'foo', spy2 );
this.obj.on( 'foo', spy3 );

// How do you unbind just a single callback for the event?
this.obj.off( 'foo', spy1 );
this.obj.trigger( 'foo' );

ok( spy2.called );

// How do you unbind all callbacks tied to the event with a single method
this.obj.off( 'foo' );
this.obj.trigger( 'foo' );

ok( spy2.callCount, 1 );
ok( spy2.calledOnce, 'Spy 2 called once' );
ok( spy3.calledOnce, 'Spy 3 called once' );

// How do you unbind all callbacks and events tied to the object with a single method?
this.obj.off( 'bar' );
this.obj.trigger( 'bar' );

equal( spy1.callCount, 0 );
});
```

### App

Expand Down
Loading

0 comments on commit 9d58427

Please sign in to comment.