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

Commit b535c42

Browse files
IgorMinargkalpak
authored andcommitted
step-12 Event Handlers
- Make the thumbnail images in the phone detail view clickable: - Introduce a `mainImageUrl` property on `PhoneDetailController`. - Implement the `setImage()` method for changing the main image. - Use `ngClick` on the thumbnails to register a handler that changes the main image. - Add an end-to-end test for this feature.
1 parent aeefb7c commit b535c42

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

app/app.css

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ h1 {
5151
.phone-thumbs li {
5252
background-color: white;
5353
border: 1px solid black;
54+
cursor: pointer;
5455
display: inline-block;
5556
margin: 1em;
5657
}

app/phone-detail/phone-detail.component.js

+5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ angular.
99
function PhoneDetailController($http, $routeParams) {
1010
var self = this;
1111

12+
self.setImage = function setImage(imageUrl) {
13+
self.mainImageUrl = imageUrl;
14+
};
15+
1216
$http.get('phones/' + $routeParams.phoneId + '.json').then(function(response) {
1317
self.phone = response.data;
18+
self.setImage(self.phone.images[0]);
1419
});
1520
}
1621
]

app/phone-detail/phone-detail.component.spec.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ describe('phoneDetail', function() {
88
// Test the controller
99
describe('PhoneDetailController', function() {
1010
var $httpBackend, ctrl;
11+
var xyzPhoneData = {
12+
name: 'phone xyz',
13+
images: ['image/url1.png', 'image/url2.png']
14+
};
1115

1216
beforeEach(inject(function($componentController, _$httpBackend_, $routeParams) {
1317
$httpBackend = _$httpBackend_;
14-
$httpBackend.expectGET('phones/xyz.json').respond({name: 'phone xyz'});
18+
$httpBackend.expectGET('phones/xyz.json').respond(xyzPhoneData);
1519

1620
$routeParams.phoneId = 'xyz';
1721

@@ -22,7 +26,7 @@ describe('phoneDetail', function() {
2226
expect(ctrl.phone).toBeUndefined();
2327

2428
$httpBackend.flush();
25-
expect(ctrl.phone).toEqual({name: 'phone xyz'});
29+
expect(ctrl.phone).toEqual(xyzPhoneData);
2630
});
2731

2832
});

app/phone-detail/phone-detail.template.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<img ng-src="{{$ctrl.phone.images[0]}}" class="phone" />
1+
<img ng-src="{{$ctrl.mainImageUrl}}" class="phone" />
22

33
<h1>{{$ctrl.phone.name}}</h1>
44

55
<p>{{$ctrl.phone.description}}</p>
66

77
<ul class="phone-thumbs">
88
<li ng-repeat="img in $ctrl.phone.images">
9-
<img ng-src="{{img}}" />
9+
<img ng-src="{{img}}" ng-click="$ctrl.setImage(img)" />
1010
</li>
1111
</ul>
1212

e2e-tests/scenarios.js

+17
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ describe('PhoneCat Application', function() {
7777
expect(element(by.binding('$ctrl.phone.name')).getText()).toBe('Nexus S');
7878
});
7979

80+
it('should display the first phone image as the main phone image', function() {
81+
var mainImage = element(by.css('img.phone'));
82+
83+
expect(mainImage.getAttribute('src')).toMatch(/img\/phones\/nexus-s.0.jpg/);
84+
});
85+
86+
it('should swap the main image when clicking on a thumbnail image', function() {
87+
var mainImage = element(by.css('img.phone'));
88+
var thumbnails = element.all(by.css('.phone-thumbs img'));
89+
90+
thumbnails.get(2).click();
91+
expect(mainImage.getAttribute('src')).toMatch(/img\/phones\/nexus-s.2.jpg/);
92+
93+
thumbnails.get(0).click();
94+
expect(mainImage.getAttribute('src')).toMatch(/img\/phones\/nexus-s.0.jpg/);
95+
});
96+
8097
});
8198

8299
});

0 commit comments

Comments
 (0)