forked from angular/angular-phonecat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ee9579
commit 3f680da
Showing
11 changed files
with
144 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React from 'react'; | ||
import { act, render, screen, waitFor, within } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import angular from 'angular'; | ||
import 'angular-resource'; | ||
import 'angular-route'; | ||
import 'angular-mocks'; | ||
import './phone-detail.module'; | ||
import '../core/phone/phone.module'; | ||
import PhoneDetail from './PhoneDetail'; | ||
import nexusS from '../phones/nexus-s.json'; | ||
import { PhoneDetail as PhoneDetailType } from './types'; | ||
|
||
describe('PhoneList', () => { | ||
let Phone: ng.resource.IResourceClass<PhoneDetailType>; | ||
let $httpBackend: ng.IHttpBackendService; | ||
let $routeParams: ng.route.IRouteParamsService; | ||
|
||
beforeEach(() => { | ||
angular.mock.module('phoneDetail'); | ||
angular.mock.inject(($resource, _$httpBackend_, _$routeParams_) => { | ||
Phone = $resource( | ||
'phones/:phoneId.json', | ||
{}, | ||
{ | ||
query: { | ||
method: 'GET', | ||
params: { phoneId: 'phones' }, | ||
isArray: true | ||
} | ||
} | ||
); | ||
|
||
$httpBackend = _$httpBackend_; | ||
$httpBackend.expectGET('phones/nexus-s.json').respond(nexusS); | ||
|
||
$routeParams = _$routeParams_; | ||
$routeParams.phoneId = 'nexus-s'; | ||
}); | ||
}); | ||
|
||
it('should fetch the `nexus-s`', async () => { | ||
render(<PhoneDetail Phone={Phone} $routeParams={$routeParams} />); | ||
|
||
act(() => { | ||
$httpBackend.flush(); | ||
}); | ||
|
||
expect(screen.getByRole('heading')).toHaveTextContent('Nexus S'); | ||
}); | ||
|
||
it('should display the first phone image as the main phone image', async () => { | ||
render(<PhoneDetail Phone={Phone} $routeParams={$routeParams} />); | ||
|
||
act(() => { | ||
$httpBackend.flush(); | ||
}); | ||
|
||
expect(screen.getByTestId('main-image')).toHaveAttribute('src', 'img/phones/nexus-s.0.jpg'); | ||
}); | ||
|
||
it('should swap main image if a thumbnail is clicked', () => { | ||
render(<PhoneDetail Phone={Phone} $routeParams={$routeParams} />); | ||
|
||
act(() => { | ||
$httpBackend.flush(); | ||
}); | ||
|
||
const thumbnails = screen.getAllByRole('listitem'); | ||
userEvent.click(within(thumbnails[2]).getByRole('img')); | ||
|
||
waitFor(() => | ||
expect(screen.getByTestId('main-image')).toHaveAttribute('src', 'img/phones/nexus-s.2.jpg') | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import angular from 'angular'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { react2angular } from 'react2angular'; | ||
import PhoneImages from './PhoneImags'; | ||
import Specifiction from './Specifiction'; | ||
import { PhoneDetail } from './types'; | ||
|
||
type Props = { | ||
Phone: ng.resource.IResourceClass<PhoneDetail>; | ||
$routeParams: ng.route.IRouteParamsService; | ||
}; | ||
|
||
const PhoneDetail: React.FC<Props> = ({ Phone, $routeParams }) => { | ||
const [phone, setPhone] = useState<PhoneDetail | null>(null); | ||
const [mainImageUrl, setMainImageUrl] = useState(''); | ||
|
||
useEffect(() => { | ||
let igonre = false; | ||
Phone.get({ phoneId: $routeParams.phoneId }, (result: PhoneDetail) => { | ||
if (!igonre) { | ||
setPhone(result); | ||
setMainImageUrl(result.images[0]); | ||
} | ||
}); | ||
return () => { | ||
igonre = true; | ||
}; | ||
}, [Phone, setPhone, $routeParams, setMainImageUrl]); | ||
|
||
if (!phone || !mainImageUrl) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<PhoneImages phone={phone} mainImageUrl={mainImageUrl} setImage={setMainImageUrl} /> | ||
|
||
<Specifiction phone={phone} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default PhoneDetail; | ||
|
||
angular | ||
.module('phoneDetail') | ||
.component('phoneDetail', react2angular(PhoneDetail, [], ['Phone', '$routeParams'])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters