Skip to content

Commit 950ea0e

Browse files
petebacondarwinteropa
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 be21b63 commit 950ea0e

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

app/js/components.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ phonecatApp.component('phoneList', {
1010
}).controller('PhoneListCtrl', function() {
1111
this.phones = [
1212
{'name': 'Nexus S',
13-
'snippet': 'Fast just got faster with Nexus S.'},
13+
'snippet': 'Fast just got faster with Nexus S.',
14+
'age': 1},
1415
{'name': 'Motorola XOOM™ with Wi-Fi',
15-
'snippet': 'The Next, Next Generation tablet.'},
16+
'snippet': 'The Next, Next Generation tablet.',
17+
'age': 2},
1618
{'name': 'MOTOROLA XOOM™',
17-
'snippet': 'The Next, Next Generation tablet.'}
19+
'snippet': 'The Next, Next Generation tablet.',
20+
'age': 3}
1821
];
22+
23+
this.orderProp = 'age';
1924
});

app/partials/phone-list.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
<!--Sidebar content-->
55

66
Search: <input ng-model="$ctrl.query">
7+
Sort by:
8+
<select ng-model="$ctrl.orderProp">
9+
<option value="name">Alphabetical</option>
10+
<option value="age">Newest</option>
11+
</select>
712

813
</div>
914
<div class="col-md-10">
1015
<!--Body content-->
1116

1217
<ul class="phones">
13-
<li ng-repeat="phone in $ctrl.phones | filter:$ctrl.query">
14-
{{phone.name}}
18+
<li ng-repeat="phone in $ctrl.phones | filter:$ctrl.query | orderBy:$ctrl.orderProp">
19+
<span>{{phone.name}}</span>
1520
<p>{{phone.snippet}}</p>
1621
</li>
1722
</ul>

test/e2e/scenarios.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,32 @@ describe('PhoneCat App', function() {
2525
query.sendKeys('motorola');
2626
expect(phoneList.count()).toBe(2);
2727
});
28+
29+
30+
it('should be possible to control phone order via the drop down select box', function() {
31+
32+
var phoneNameColumn = element.all(by.repeater('phone in $ctrl.phones').column('phone.name'));
33+
var query = element(by.model('$ctrl.query'));
34+
35+
function getNames() {
36+
return phoneNameColumn.map(function(elm) {
37+
return elm.getText();
38+
});
39+
}
40+
41+
query.sendKeys('tablet'); //let's narrow the dataset to make the test assertions shorter
42+
43+
expect(getNames()).toEqual([
44+
"Motorola XOOM\u2122 with Wi-Fi",
45+
"MOTOROLA XOOM\u2122"
46+
]);
47+
48+
element(by.model('$ctrl.orderProp')).element(by.css('option[value="name"]')).click();
49+
50+
expect(getNames()).toEqual([
51+
"MOTOROLA XOOM\u2122",
52+
"Motorola XOOM\u2122 with Wi-Fi"
53+
]);
54+
});
2855
});
2956
});

test/unit/controllersSpec.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44
describe('PhoneCat controllers', function() {
55

66
describe('PhoneListCtrl', function(){
7+
var ctrl;
78

89
beforeEach(module('phonecatApp'));
910

10-
it('should create "phones" model with 3 phones', inject(function($controller) {
11-
var ctrl = $controller('PhoneListCtrl');
11+
beforeEach(inject(function($controller) {
12+
ctrl = $controller('PhoneListCtrl');
13+
}));
14+
1215

16+
it('should create "phones" model with 3 phones', function() {
1317
expect(ctrl.phones.length).toBe(3);
14-
}));
18+
});
19+
1520

21+
it('should set the default value of orderProp model', function() {
22+
expect(ctrl.orderProp).toBe('age');
23+
});
1624
});
1725
});

0 commit comments

Comments
 (0)