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

Commit 2e7a6f8

Browse files
petebacondarwingkalpak
authored andcommitted
step-6 Two-way Data Binding
- Add an `age` property to the phone model. - Add a drop-down menu to control the phone list order. - Override the default order value in controller. - Add unit and end-to-end tests for this feature. Closes #213
1 parent 197708b commit 2e7a6f8

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

Diff for: app/phone-list/phone-list.component.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ angular.
99
this.phones = [
1010
{
1111
name: 'Nexus S',
12-
snippet: 'Fast just got faster with Nexus S.'
12+
snippet: 'Fast just got faster with Nexus S.',
13+
age: 1
1314
}, {
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
}, {
1719
name: 'MOTOROLA XOOM™',
18-
snippet: 'The Next, Next Generation tablet.'
20+
snippet: 'The Next, Next Generation tablet.',
21+
age: 3
1922
}
2023
];
24+
25+
this.orderProp = 'age';
2126
}
2227
});

Diff for: app/phone-list/phone-list.component.spec.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ describe('phoneList', function() {
77

88
// Test the controller
99
describe('PhoneListController', function() {
10+
var ctrl;
1011

11-
it('should create a `phones` model with 3 phones', inject(function($componentController) {
12-
var ctrl = $componentController('phoneList');
12+
beforeEach(inject(function($componentController) {
13+
ctrl = $componentController('phoneList');
14+
}));
1315

16+
it('should create a `phones` model with 3 phones', function() {
1417
expect(ctrl.phones.length).toBe(3);
15-
}));
18+
});
19+
20+
it('should set a default value for the `orderProp` model', function() {
21+
expect(ctrl.orderProp).toBe('age');
22+
});
1623

1724
});
1825

Diff for: app/phone-list/phone-list.template.html

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@
33
<div class="col-md-2">
44
<!--Sidebar content-->
55

6-
Search: <input ng-model="$ctrl.query" />
6+
<p>
7+
Search:
8+
<input ng-model="$ctrl.query" />
9+
</p>
10+
11+
<p>
12+
Sort by:
13+
<select ng-model="$ctrl.orderProp">
14+
<option value="name">Alphabetical</option>
15+
<option value="age">Newest</option>
16+
</select>
17+
</p>
718

819
</div>
920
<div class="col-md-10">
1021
<!--Body content-->
1122

1223
<ul class="phones">
13-
<li ng-repeat="phone in $ctrl.phones | filter:$ctrl.query">
24+
<li ng-repeat="phone in $ctrl.phones | filter:$ctrl.query | orderBy:$ctrl.orderProp">
1425
<span>{{phone.name}}</span>
1526
<p>{{phone.snippet}}</p>
1627
</li>

Diff for: e2e-tests/scenarios.js

+27
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ describe('PhoneCat Application', function() {
2525
expect(phoneList.count()).toBe(2);
2626
});
2727

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

3057
});

0 commit comments

Comments
 (0)