Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3043695

Browse files
committed
docs(guide/component): improve tests
Fixes #14739
1 parent acb545e commit 3043695

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

docs/content/guide/component.ngdoc

+36-24
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ it upwards to the heroList component, which updates the original data.
236236
function HeroDetailController() {
237237
var ctrl = this;
238238

239+
ctrl.delete = function() {
240+
ctrl.onDelete({hero: ctrl.hero});
241+
};
242+
239243
ctrl.update = function(prop, value) {
240244
ctrl.onUpdate({hero: ctrl.hero, prop: prop, value: value});
241245
};
@@ -303,7 +307,7 @@ it upwards to the heroList component, which updates the original data.
303307
<div>
304308
Name: {{$ctrl.hero.name}}<br>
305309
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>
307311
</div>
308312
</file>
309313
<file name="editableField.html">
@@ -443,44 +447,52 @@ angular.module('docsTabsExample', [])
443447

444448
# Unit-testing Component Controllers
445449

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.
450454

451455
The examples use the [Jasmine](http://jasmine.github.io/) testing framework.
452456

453457
**Controller Test:**
454458
```js
455459
describe('component: heroDetail', function() {
456-
var component, scope, hero, $componentController;
460+
var $componentController;
457461

458462
beforeEach(module('heroApp'));
459-
460-
beforeEach(inject(function($rootScope, _$componentController_) {
461-
scope = $rootScope.$new();
463+
beforeEach(inject(function(_$componentController_) {
462464
$componentController = _$componentController_;
463-
hero = {name: 'Wolverine'};
464465
}));
465466

466-
it('should assign the name bindings to the hero object', function() {
467+
it('should expose a `hero` object', function() {
467468
// 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');
473474
});
474475

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);
481480

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+
});
484496
});
485497

486498
});

0 commit comments

Comments
 (0)