-
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(rider_apps): Manage rider app config. (#97)
Allows users to get and set the configuration for a customer's Rider Apps. Resolves EN-7246. Signed-off-by: Jeff Cuevas-Koch <[email protected]> Co-authored-by: Jeff Cuevas-Koch <[email protected]>
- Loading branch information
1 parent
2c315f1
commit f4336d1
Showing
7 changed files
with
223 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,50 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import fetchMock from 'fetch-mock'; | ||
import Track from '../index'; | ||
import { charlie, riderAppConfiguration as mocks } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When retrieving a rider app configuration', () => { | ||
const api = new Track({ autoRenew: false }); | ||
|
||
beforeEach(() => charlie.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => mocks.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
it('should get a the configuration', () => { | ||
api.logIn({ username: '[email protected]', password: 'securepassword' }); | ||
|
||
const configPromise = api.customer('SYNC').riderAppConfiguration() | ||
.fetch() | ||
.then(config => config); // Do things with config | ||
|
||
return configPromise; | ||
}); | ||
}); | ||
|
||
describe('When updating a rider app configuration', () => { | ||
const api = new Track({ autoRenew: false }); | ||
|
||
beforeEach(() => charlie.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => mocks.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
it('should update the configuration', () => { | ||
api.logIn({ username: '[email protected]', password: 'securepassword' }); | ||
|
||
const configPromise = api.customer('SYNC').riderAppConfiguration() | ||
.fetch() | ||
.then((config) => { | ||
// eslint-disable-next-line no-param-reassign | ||
config.spash_image_url = 'https://example.com/updated.png'; | ||
return config.update(); | ||
}); | ||
|
||
return configPromise; | ||
}); | ||
}); |
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,36 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import fetchMock from 'fetch-mock'; | ||
import Client from '../Client'; | ||
|
||
const riderAppConfiguration = { | ||
rawObject: { | ||
href: '/1/SYNC/rider_app_configuration', | ||
splash_image_url: 'https://example.com/logo.png', | ||
accent_color: '#ABCDEF', | ||
information: [{ | ||
title: 'Agency Information', | ||
items: [ | ||
{ | ||
title: 'About Us', | ||
link: 'https://example.com/about', | ||
}, | ||
{ | ||
title: 'Routes', | ||
link: 'https://example.com/routes', | ||
}, | ||
], | ||
}], | ||
}, | ||
setUpSuccessfulMock: (client) => { | ||
const url = client.resolve('/1/SYNC/rider_app_configuration'); | ||
|
||
const putResponse = () => new Response(undefined); | ||
const getResponse = () => new Response(Client.toBlob(riderAppConfiguration.rawObject)); | ||
|
||
fetchMock | ||
.put(url, putResponse) | ||
.get(url, getResponse); | ||
} | ||
}; | ||
|
||
export default riderAppConfiguration; |
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,60 @@ | ||
import Resource from './Resource'; | ||
|
||
/** | ||
* Rider App Configuration resource | ||
*/ | ||
class RiderAppConfiguration extends Resource { | ||
/** | ||
* Creates a new RiderAppConfiguration. | ||
* | ||
* @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' || k === 'code'); | ||
|
||
Object.assign(this, newProperties, { | ||
hydrated, | ||
}); | ||
} | ||
|
||
/** | ||
* Creates an href for a given customer code | ||
* | ||
* @param {string} customerCode Customer code | ||
* @returns {{href: string}} URI to instance of Rider App Configuration | ||
*/ | ||
static makeHref(customerCode) { | ||
return { | ||
href: `/1/${customerCode}/rider_app_configuration`, | ||
code: customerCode, | ||
}; | ||
} | ||
|
||
/** | ||
* Fetches the Rider App Configuration for this customer data via the client | ||
* @returns {Promise} If successful,a hydrated instance of Rider App Configuration | ||
*/ | ||
fetch() { | ||
return this.client.get(this.href) | ||
.then(response => response.json()) | ||
.then(config => new RiderAppConfiguration(this.client, this, config)); | ||
} | ||
|
||
/** | ||
* Updates the Rider App Configuration for this customer via the client | ||
* @returns {Promise} If successful, returns the updated Rider App Configuration | ||
*/ | ||
update() { | ||
// eslint-disable-next-line no-unused-vars | ||
const { client, hydrated, code, ...body } = this; | ||
const { href } = RiderAppConfiguration.makeHref(code); | ||
return this.client.put(href, { body }) | ||
.then(() => new RiderAppConfiguration(this.client, { ...this })); | ||
} | ||
} | ||
|
||
export default RiderAppConfiguration; |
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,63 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import fetchMock from 'fetch-mock'; | ||
import Client from '../Client'; | ||
import RiderAppConfiguration from './RiderAppConfiguration'; | ||
import { riderAppConfiguration as mocks } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When instantiating a Rider App Configuration based on customer', () => { | ||
const client = new Client(); | ||
const config = new RiderAppConfiguration(client, RiderAppConfiguration.makeHref('SYNC')); | ||
|
||
it('should set the href', () => config.href.should.equal('/1/SYNC/rider_app_configuration')); | ||
it('should not be hydrated', () => config.hydrated.should.equal(false)); | ||
}); | ||
|
||
describe('When instantiating a Rider App Configuration based on an object', () => { | ||
const client = new Client(); | ||
const config = new RiderAppConfiguration(client, mocks.rawObject); | ||
|
||
it('should set the href', () => config.href.should.equal('/1/SYNC/rider_app_configuration')); | ||
it('should set the splash image url', () => config.splash_image_url.should.equal('https://example.com/logo.png')); | ||
it('should set the accent color', () => config.accent_color.should.equal('#ABCDEF')); | ||
it('should be hydrated', () => config.hydrated.should.equal(true)); | ||
}); | ||
|
||
describe('When fetching a Rider App Configuration based on customer', () => { | ||
const client = new Client(); | ||
|
||
beforeEach(() => mocks.setUpSuccessfulMock(client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
let promise; | ||
beforeEach(() => { | ||
promise = new RiderAppConfiguration(client, RiderAppConfiguration.makeHref('SYNC')).fetch(); | ||
}); | ||
|
||
it('should resolve the promise', () => promise.should.be.fulfilled); | ||
it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/rider_app_configuration')); | ||
it('should be hydrated', () => promise.then(v => v.hydrated).should.eventually.equal(true)); | ||
}); | ||
|
||
describe('When updating a Rider App Configuration for a customer', () => { | ||
const client = new Client(); | ||
|
||
beforeEach(() => mocks.setUpSuccessfulMock(client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
let promise; | ||
beforeEach(() => { | ||
const config = new RiderAppConfiguration(client, RiderAppConfiguration.makeHref('SYNC')); | ||
config.splash_image_url = 'https://example.com/logo.png'; | ||
config.accent_color = '#ABCDEF'; | ||
promise = config.update(); | ||
}); | ||
|
||
it('should resolve the promise', () => promise.should.be.fulfilled); | ||
it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/rider_app_configuration')); | ||
}); |