-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(driver-run-trip): Add support for Drivers, Runs, Trips. (#28)
* Add Driver and DriversContext resources * Add trip resource * Add Run resource
- Loading branch information
1 parent
44b4137
commit 208cf69
Showing
14 changed files
with
645 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import fetchMock from 'fetch-mock'; | ||
import Track from '../index'; | ||
import { charlie, drivers as mockDrivers } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When searching for drivers by name', () => { | ||
const api = new Track({ autoRenew: false }); | ||
|
||
beforeEach(() => charlie.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => mockDrivers.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
it('should get a list of drivers', () => { | ||
api.logIn({ username: '[email protected]', password: 'securepassword' }); | ||
|
||
const driversPromise = api.customer('SYNC').drivers() | ||
.withQuery('charlie') // drivers containing 'charlie' in their name | ||
.getPage() | ||
.then(page => page.list) | ||
.then(drivers => drivers); // Do things with list of drivers | ||
|
||
return driversPromise; | ||
}); | ||
}); | ||
|
||
describe('When retrieving a driver by ID', () => { | ||
const api = new Track({ autoRenew: false }); | ||
|
||
beforeEach(() => charlie.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => mockDrivers.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
it('should get a driver', () => { | ||
api.logIn({ username: '[email protected]', password: 'securepassword' }); | ||
|
||
const driverPromise = api.customer('SYNC').driver(1) | ||
.fetch() | ||
.then(driver => driver); // Do things with driver | ||
|
||
return driverPromise; | ||
}); | ||
}); |
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,27 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import fetchMock from 'fetch-mock'; | ||
import Track from '../index'; | ||
import { charlie, runs as mockRuns } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When retrieving a run by ID', () => { | ||
const api = new Track({ autoRenew: false }); | ||
|
||
beforeEach(() => charlie.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => mockRuns.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
it('should get a run', () => { | ||
api.logIn({ username: '[email protected]', password: 'securepassword' }); | ||
|
||
const runPromise = api.customer('SYNC').run(1) | ||
.fetch() | ||
.then(run => run); // Do things with run | ||
|
||
return runPromise; | ||
}); | ||
}); |
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,27 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import fetchMock from 'fetch-mock'; | ||
import Track from '../index'; | ||
import { charlie, trips as mockTrips } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When retrieving a trip by ID', () => { | ||
const api = new Track({ autoRenew: false }); | ||
|
||
beforeEach(() => charlie.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => mockTrips.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
it('should get a trip', () => { | ||
api.logIn({ username: '[email protected]', password: 'securepassword' }); | ||
|
||
const tripPromise = api.customer('SYNC').trip(3) | ||
.fetch() | ||
.then(trip => trip); // Do things with trip | ||
|
||
return tripPromise; | ||
}); | ||
}); |
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 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,58 @@ | ||
import Resource from './Resource'; | ||
|
||
/** | ||
* Driver resource | ||
*/ | ||
class Driver extends Resource { | ||
|
||
/** | ||
* Creates a new driver. | ||
* | ||
* Will populate itself with the values given to it after the client parameter | ||
* @example <caption>Assigning partial driver data to a new instance</caption> | ||
* const client = new Client(); | ||
* const partialDriverData = { | ||
* href: '/1/SYNC/drivers/1', | ||
* id: 1, | ||
* customer_driver_id: '0001', | ||
* first_name: 'Charlie', | ||
* last_name: 'Singh', | ||
* }; | ||
* const driver = new Driver(client, partialDriverData); | ||
* | ||
* driver.hydrated == true; | ||
* @param {Client} client Instance of pre-configured client | ||
* @param {Array} rest Remaining arguments to use in assigning values to this instance | ||
*/ | ||
constructor(client, ...rest) { | ||
super(client); | ||
|
||
const newProperties = Object.assign({}, ...rest); | ||
const hydrated = !Object.keys(newProperties).every(k => k === 'href'); | ||
Object.assign(this, newProperties, { hydrated }); | ||
} | ||
|
||
/** | ||
* Makes a href for a given customer code and DI | ||
* @param {string} customerCode Customer code | ||
* @param {number} id Driver ID | ||
* @returns {{href: string}} URI to instance of driver | ||
*/ | ||
static makeHref(customerCode, id) { | ||
return { | ||
href: `/1/${customerCode}/drivers/${id}`, | ||
}; | ||
} | ||
|
||
/** | ||
* Fetches the data for this driver via the client | ||
* @returns {Promise} If successful, a hydrated instance of this driver | ||
*/ | ||
fetch() { | ||
return this.client.get(this.href) | ||
.then(response => response.json()) | ||
.then(driver => new Driver(this.client, this, driver)); | ||
} | ||
} | ||
|
||
export default Driver; |
Oops, something went wrong.