Skip to content

Commit 01da453

Browse files
author
wefine
committed
matched / Tutorial / 7 - XHR & Dependency Injection, ut with karma and it with protractor passed.
Signed-off-by: wefine <wang.xiaoren@zte.com.cn>
1 parent 8f2a218 commit 01da453

File tree

107 files changed

+1463
-31
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+1463
-31
lines changed

src/app/phone-list/phone-list.component.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
export class PhoneListController {
2-
constructor() {
3-
this.phones = [
4-
{
5-
name: 'Nexus S',
6-
snippet: 'Fast just got faster with Nexus S.'
7-
}, {
8-
name: 'Motorola XOOM\u2122 with Wi-Fi',
9-
snippet: 'The Next, Next Generation tablet.'
10-
}, {
11-
name: 'MOTOROLA XOOM\u2122',
12-
snippet: 'The Next, Next Generation tablet.'
13-
}
14-
];
2+
/**
3+
* @param {!angular.$http} $http
4+
* @ngInject
5+
*/
6+
constructor($http) {
7+
let self = this;
8+
self.orderProp = 'age';
159

16-
this.orderProp = 'age';
10+
$http.get('phones/phones.json').then(function(response) {
11+
self.phones = response.data;
12+
});
1713
}
1814
}
1915

src/app/phone-list/phone-list.component.spec.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,30 @@ describe('phoneList', function () {
1313
// Test the controller
1414
describe('PhoneListController', function () {
1515

16-
let ctrl;
16+
let $httpBackend, ctrl;
17+
18+
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
19+
// This allows us to inject a service and assign it to a variable with the same name
20+
// as the service while avoiding a name conflict.
21+
beforeEach(inject(function($componentController, _$httpBackend_) {
22+
$httpBackend = _$httpBackend_;
23+
$httpBackend.expectGET('phones/phones.json')
24+
.respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
1725

18-
beforeEach(inject(function ($componentController) {
1926
ctrl = $componentController('phoneList');
2027
}));
2128

22-
it('should create a `phones` model with 3 phones', function () {
23-
expect(ctrl.phones.length).toBe(3);
29+
it('should create a `phones` property with 2 phones fetched with `$http`', function() {
30+
expect(ctrl.phones).toBeUndefined();
31+
32+
$httpBackend.flush();
33+
expect(ctrl.phones).toEqual([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
2434
});
2535

26-
it('should set a default value for the `orderProp` model', function () {
36+
it('should set a default value for the `orderProp` property', function() {
2737
expect(ctrl.orderProp).toBe('age');
2838
});
39+
2940
});
3041

3142
});

src/app/phone-list/phone-list.e2e.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ describe('PhoneCat Application', function () {
1515
let phoneList = element.all(by.repeater('phone in $ctrl.phones'));
1616
let query = element(by.model('$ctrl.query'));
1717

18-
expect(phoneList.count()).toBe(3);
18+
expect(phoneList.count()).toBe(20);
1919

2020
query.sendKeys('nexus');
2121
expect(phoneList.count()).toBe(1);
2222

2323
query.clear();
2424
query.sendKeys('motorola');
25-
expect(phoneList.count()).toBe(2);
25+
expect(phoneList.count()).toBe(8);
2626
});
2727

2828
it('should be possible to control phone order via the drop-down menu', function () {

src/app/phone-list/phone-list.template.html

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
<!--Sidebar content-->
55

66
<p>
7-
Search:
8-
<input ng-model="$ctrl.query">
7+
<label>Search:
8+
<input ng-model="$ctrl.query"/>
9+
</label>
910
</p>
1011

1112
<p>
12-
Sort by:
13-
<select ng-model="$ctrl.orderProp">
14-
<option value="name">Alphabetical</option>
15-
<option value="age">Newest</option>
16-
</select>
13+
<label>Sort by:
14+
<select ng-model="$ctrl.orderProp">
15+
<option value="name">Alphabetical</option>
16+
<option value="age">Newest</option>
17+
</select>
18+
</label>
1719
</p>
1820

1921
</div>
@@ -29,4 +31,4 @@
2931

3032
</div>
3133
</div>
32-
</div>
34+
</div>

src/app/templates.module.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/public/img/.gitkeep

Whitespace-only changes.
22.5 KB
22.7 KB
4.58 KB
27.7 KB

0 commit comments

Comments
 (0)