@@ -236,6 +236,10 @@ it upwards to the heroList component, which updates the original data.
236
236
function HeroDetailController() {
237
237
var ctrl = this;
238
238
239
+ ctrl.delete = function() {
240
+ ctrl.onDelete({hero: ctrl.hero});
241
+ };
242
+
239
243
ctrl.update = function(prop, value) {
240
244
ctrl.onUpdate({hero: ctrl.hero, prop: prop, value: value});
241
245
};
@@ -303,7 +307,7 @@ it upwards to the heroList component, which updates the original data.
303
307
<div>
304
308
Name: {{$ctrl.hero.name}}<br>
305
309
Location: <editable-field field-value="$ctrl.hero.location" field-type="text" on-update="$ctrl.update('location', value)"></editable-field><br>
306
- <button ng-click="$ctrl.onDelete({hero: $ctrl.hero} )">Delete</button>
310
+ <button ng-click="$ctrl.delete( )">Delete</button>
307
311
</div>
308
312
</file>
309
313
<file name="editableField.html">
@@ -443,44 +447,52 @@ angular.module('docsTabsExample', [])
443
447
444
448
# Unit-testing Component Controllers
445
449
446
- The easiest way to unit-test a component controller is by using the {@link ngMock.$componentController $componentController}
447
- that is included in {@link ngMock}. The advantage of this method is that you do not have
448
- to create any DOM elements. The following example shows how to do this for the `heroDetail` component
449
- from above.
450
+ The easiest way to unit-test a component controller is by using the
451
+ {@link ngMock.$componentController $componentController} that is included in {@link ngMock}. The
452
+ advantage of this method is that you do not have to create any DOM elements. The following example
453
+ shows how to do this for the `heroDetail` component from above.
450
454
451
455
The examples use the [Jasmine](http://jasmine.github.io/) testing framework.
452
456
453
457
**Controller Test:**
454
458
```js
455
459
describe('component: heroDetail', function() {
456
- var component, scope, hero, $componentController;
460
+ var $componentController;
457
461
458
462
beforeEach(module('heroApp'));
459
-
460
- beforeEach(inject(function($rootScope, _$componentController_) {
461
- scope = $rootScope.$new();
463
+ beforeEach(inject(function(_$componentController_) {
462
464
$componentController = _$componentController_;
463
- hero = {name: 'Wolverine'};
464
465
}));
465
466
466
- it('should assign the name bindings to the hero object', function() {
467
+ it('should expose a ` hero` object', function() {
467
468
// Here we are passing actual bindings to the component
468
- component = $componentController('heroDetail',
469
- null,
470
- {hero: hero}
471
- );
472
- expect(component .hero.name).toBe('Wolverine');
469
+ var bindings = {hero: {name: 'Wolverine'}};
470
+ var ctrl = $componentController('heroDetail', null, bindings);
471
+
472
+ expect(ctrl.hero).toBeDefined( );
473
+ expect(ctrl .hero.name).toBe('Wolverine');
473
474
});
474
475
475
- it('should call the onDelete binding when a hero is deleted', function() {
476
- var deleteSpy = jasmine.createSpy('deleteSpy');
477
- component = $componentController('heroDetail',
478
- null,
479
- {hero: hero, onDelete: deleteSpy}
480
- );
476
+ it('should call the `onDelete` binding, when deleting the hero', function() {
477
+ var onDeleteSpy = jasmine.createSpy('onDelete');
478
+ var bindings = {hero: {}, onDelete: onDeleteSpy};
479
+ var ctrl = $componentController('heroDetail', null, bindings);
481
480
482
- component.onDelete({hero: component.hero});
483
- expect(deleteSpy).toHaveBeenCalledWith({hero: component.hero});
481
+ ctrl.delete();
482
+ expect(onDeleteSpy).toHaveBeenCalledWith({hero: ctrl.hero});
483
+ });
484
+
485
+ it('should call the `onUpdate` binding, when updating a property', function() {
486
+ var onUpdateSpy = jasmine.createSpy('onUpdate');
487
+ var bindings = {hero: {}, onUpdate: onUpdateSpy};
488
+ var ctrl = $componentController('heroDetail', null, bindings);
489
+
490
+ ctrl.update('foo', 'bar');
491
+ expect(onUpdateSpy).toHaveBeenCalledWith({
492
+ hero: ctrl.hero,
493
+ prop: 'foo',
494
+ value: 'bar'
495
+ });
484
496
});
485
497
486
498
});
0 commit comments