Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.

Commit 44f37ab

Browse files
committed
test(uber-service): add basic tests for UberService
1 parent b8cc1ba commit 44f37ab

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

__mocks__/uber-estimates-client.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const getExpectedTimeOfArrival = jest.fn(() => ({
2+
times: [
3+
{
4+
localized_display_name: 'first localized display name',
5+
estimate: 'first estimate',
6+
},
7+
{
8+
localized_display_name: 'second localized display name',
9+
estimate: 'second estimate',
10+
},
11+
],
12+
}));
13+
14+
const getPrices = jest.fn(() => ({
15+
prices: [
16+
{
17+
localized_display_name: 'first localized display name',
18+
distance: 'first distance',
19+
duration: 'first duration',
20+
high_estimate: 'first high estimate',
21+
low_estimate: 'first low estimate',
22+
currency_code: 'first currency code',
23+
surgeMultiplier: undefined,
24+
},
25+
{
26+
localized_display_name: 'second localized display name',
27+
distance: 'second distance',
28+
duration: 'second duration',
29+
high_estimate: 'second high estimate',
30+
low_estimate: 'second low estimate',
31+
currency_code: 'second currency code',
32+
surgeMultiplier: 'surgeMultiplier',
33+
},
34+
],
35+
}));
36+
37+
const constructor = jest.fn(() => ({
38+
getExpectedTimeOfArrival,
39+
getPrices,
40+
}));
41+
42+
const UberEstimatesClient = constructor;
43+
44+
export default UberEstimatesClient;
45+
export {
46+
getExpectedTimeOfArrival,
47+
getPrices,
48+
};

src/services/UberService.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import UberEstimatesClient from 'uber-estimates-client';
2+
3+
import AddressLocator from './AddressLocator';
4+
import UberService from './UberService';
5+
6+
jest.mock('uber-estimates-client');
7+
jest.mock('./AddressLocator');
8+
9+
describe('UberService', () => {
10+
let service;
11+
12+
beforeEach(() => {
13+
UberEstimatesClient.mockClear();
14+
AddressLocator.mockClear();
15+
service = new UberService();
16+
});
17+
18+
describe('#constructor', () => {
19+
it('constructs service', () => {
20+
expect(service).toBeDefined();
21+
expect(UberEstimatesClient).toHaveBeenCalledTimes(1);
22+
expect(AddressLocator).toHaveBeenCalledTimes(1);
23+
});
24+
});
25+
26+
describe('#getTimeEstimates', () => {
27+
it('gets time estimates', async () => {
28+
const timeEstimates = await service.getTimeEstimates('firstjaebaebae');
29+
expect(timeEstimates.location).toEqual({ coordinate: { latitude: 'firstjaebaebaelatitude', longitude: 'firstjaebaebaelongitude' } });
30+
expect(timeEstimates.estimates[0].productName).toEqual('first localized display name');
31+
});
32+
});
33+
34+
describe('#getPriceEstimates', () => {
35+
it('gets price estimates', async () => {
36+
const priceEstimates = await service.getPriceEstimates({
37+
startAddress: 'firstjaebaebae',
38+
endAddress: 'secondjaebaebae',
39+
});
40+
expect(priceEstimates).toBeDefined();
41+
expect(priceEstimates.start).toEqual({ coordinate: { latitude: 'firstjaebaebaelatitude', longitude: 'firstjaebaebaelongitude' } });
42+
expect(priceEstimates.estimates[0].productName).toEqual('first localized display name');
43+
});
44+
});
45+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const getFirstLocation = jest.fn((address) => {
2+
if (address === 'firstjaebaebae') {
3+
return Promise.resolve({
4+
coordinate: {
5+
latitude: 'firstjaebaebaelatitude',
6+
longitude: 'firstjaebaebaelongitude',
7+
},
8+
});
9+
}
10+
11+
if (address === 'secondjaebaebae') {
12+
return Promise.resolve({
13+
coordinate: {
14+
latitude: 'secondjaebaebaelatitude',
15+
longitude: 'secondjaebaebaelongitude',
16+
},
17+
});
18+
}
19+
20+
throw new Error(`Unknown address: ${address}`);
21+
});
22+
23+
const constructor = jest.fn(() => ({ getFirstLocation }));
24+
25+
const AddressLocator = constructor;
26+
27+
export default AddressLocator;
28+
export { getFirstLocation };

0 commit comments

Comments
 (0)