Skip to content

Commit a6c31f4

Browse files
petebacondarwingkalpak
authored andcommitted
step-4 phone ordering
- Add "age" property to the phone model - Add select box to control phone list order - Override the default order value in controller - Add unit and e2e test for this feature Closes angular#213
1 parent e6673a4 commit a6c31f4

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

app/index.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@
1414

1515
<!-- Use a custom component to render a list of phones -->
1616
<phone-list></phone-list>
17+
<div class="container-fluid">
18+
<div class="row">
19+
<div class="col-md-2">
20+
<!--Sidebar content-->
21+
22+
Search: <input ng-model="query">
23+
Sort by:
24+
<select ng-model="orderProp">
25+
<option value="name">Alphabetical</option>
26+
<option value="age">Newest</option>
27+
</select>
28+
29+
</div>
30+
<div class="col-md-10">
31+
<!--Body content-->
32+
33+
<ul class="phones">
34+
<li ng-repeat="phone in phones | filter:query | orderBy:orderProp">
35+
<span>{{phone.name}}</span>
36+
<p>{{phone.snippet}}</p>
37+
</li>
38+
</ul>
39+
40+
</div>
41+
</div>
42+
</div>
1743

1844
</body>
1945
</html>

app/js/controllers.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
/* Controllers */
4+
5+
var phonecatApp = angular.module('phonecatApp', []);
6+
7+
phonecatApp.controller('PhoneListCtrl', function($scope) {
8+
$scope.phones = [
9+
{'name': 'Nexus S',
10+
'snippet': 'Fast just got faster with Nexus S.',
11+
'age': 1},
12+
{'name': 'Motorola XOOM™ with Wi-Fi',
13+
'snippet': 'The Next, Next Generation tablet.',
14+
'age': 2},
15+
{'name': 'MOTOROLA XOOM™',
16+
'snippet': 'The Next, Next Generation tablet.',
17+
'age': 3}
18+
];
19+
20+
$scope.orderProp = 'age';
21+
});

e2e-tests/scenarios.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,32 @@ describe('PhoneCat Application', function() {
2525
expect(phoneList.count()).toBe(2);
2626
});
2727

28+
29+
it('should be possible to control phone order via the drop down select box', function() {
30+
31+
var phoneNameColumn = element.all(by.repeater('phone in phones').column('phone.name'));
32+
var query = element(by.model('query'));
33+
34+
function getNames() {
35+
return phoneNameColumn.map(function(elm) {
36+
return elm.getText();
37+
});
38+
}
39+
40+
query.sendKeys('tablet'); //let's narrow the dataset to make the test assertions shorter
41+
42+
expect(getNames()).toEqual([
43+
"Motorola XOOM\u2122 with Wi-Fi",
44+
"MOTOROLA XOOM\u2122"
45+
]);
46+
47+
element(by.model('orderProp')).element(by.css('option[value="name"]')).click();
48+
49+
expect(getNames()).toEqual([
50+
"MOTOROLA XOOM\u2122",
51+
"Motorola XOOM\u2122 with Wi-Fi"
52+
]);
53+
});
2854
});
2955

3056
});

test/unit/controllersSpec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
/* jasmine specs for controllers go here */
4+
describe('PhoneCat controllers', function() {
5+
6+
describe('PhoneListCtrl', function(){
7+
var scope, ctrl;
8+
9+
beforeEach(module('phonecatApp'));
10+
11+
beforeEach(inject(function($controller) {
12+
scope = {};
13+
ctrl = $controller('PhoneListCtrl', {$scope:scope});
14+
}));
15+
16+
17+
it('should create "phones" model with 3 phones', function() {
18+
expect(scope.phones.length).toBe(3);
19+
});
20+
21+
22+
it('should set the default value of orderProp model', function() {
23+
expect(scope.orderProp).toBe('age');
24+
});
25+
});
26+
});

0 commit comments

Comments
 (0)