-
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(enplugScreenshotAndConfiguration): Add calls for getting screens…
…hots and updating config (#70) * feat(enplugScreenshotAndConfiguration): Add calls for getting an enplug screenshot and updating configuration * mock put call for configuration update * mock response empty * add customer helper functions for usage * remove hash
- Loading branch information
Showing
5 changed files
with
212 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
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,59 @@ | ||
import Resource from './Resource'; | ||
|
||
/** | ||
* EnplugConfiguration resource | ||
*/ | ||
class EnplugConfiguration extends Resource { | ||
/** | ||
* Creates a new enplugConfiguration | ||
* | ||
* Will populate itself with the values given to it after the client parameter | ||
* @example <caption>Assigning partial enplug configuration data to a new instance</caption> | ||
* const client = new Client(); | ||
* const partialEnplugConfiguration = { | ||
* href: '/1/SYNC/enplugs/LLM3/configuration', | ||
* volume: 27, | ||
* vehicleHref: '/1/SYNC/vehicles/1234', | ||
* }; | ||
* const enplugConfiguration = new EnplugConfiguration(client, partialEnplugConfiguration); | ||
* | ||
* enplugConfiguration.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 ID | ||
* @param {string} customerCode Customer code | ||
* @param {string} deviceSerial Enplug Serial | ||
* @returns {string} URI to instance of enplug configuration | ||
*/ | ||
static makeHref(customerCode, deviceSerial) { | ||
return { | ||
href: `/1/${customerCode}/enplugs/${deviceSerial}/configuration`, | ||
}; | ||
} | ||
|
||
/** | ||
* Updates configuration for an enplug via the client | ||
* @returns {Promise} if successful returns instance of this user | ||
*/ | ||
update() { | ||
// eslint-disable-next-line no-unused-vars | ||
const { client, hydrated, href, ...body } = this; | ||
return this.client.put(href, { body }) | ||
.then(() => new EnplugConfiguration(this.client, { ...this })); | ||
} | ||
} | ||
|
||
export default EnplugConfiguration; |
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,54 @@ | ||
import chai from 'chai'; | ||
import fetchMock from 'fetch-mock'; | ||
|
||
import chaiAsPromised from 'chai-as-promised'; | ||
import Client from '../Client'; | ||
import EnplugConfiguration from './EnplugConfiguration'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When instantiating an enplug configuration based on customer and enplug serial', () => { | ||
const client = new Client(); | ||
const enplugConfiguration = new EnplugConfiguration(client, EnplugConfiguration.makeHref('SYNC', 'SERIAL11')); | ||
|
||
it('should set the href', () => enplugConfiguration.href.should.equal('/1/SYNC/enplugs/SERIAL11/configuration')); | ||
it('should not be hydrated', () => enplugConfiguration.hydrated.should.equal(false)); | ||
}); | ||
|
||
describe('When instantiating a vehicle media based on an object', () => { | ||
const client = new Client(); | ||
const mockConfiguration = new EnplugConfiguration(client, { deviceSerial: 'LLCM2', volume: 28, shouldSwitch: true, vehicleHref: '/1/SYNC/vehicles/1234' }); | ||
const enplugConfiguration = new EnplugConfiguration(client, mockConfiguration); | ||
|
||
it('should set the volume', () => enplugConfiguration.volume.should.equal(28)); | ||
it('should set the deviceSerial', () => enplugConfiguration.deviceSerial.should.equal('LLCM2')); | ||
it('should set the vehicleHref', () => enplugConfiguration.vehicleHref.should.equal('/1/SYNC/vehicles/1234')); | ||
it('should set the shouldSwitch', () => enplugConfiguration.shouldSwitch.should.equal(true)); | ||
}); | ||
|
||
describe('When updating enplug configuration', () => { | ||
const client = new Client(); | ||
const mockConfiguration = new EnplugConfiguration(client, { | ||
href: '/1/SYNC/enplugs/LLCM2/configuration', | ||
code: 'SYNC', | ||
deviceSerial: 'LLCM2', | ||
volume: 28, | ||
shouldSwitch: true, | ||
vehicleHref: '/1/SYNC/vehicles/1234' }); | ||
|
||
const singleResponse = () => new Response(); | ||
beforeEach(() => { | ||
fetchMock.put(client.resolve('/1/SYNC/enplugs/LLCM2/configuration'), singleResponse); | ||
}); | ||
beforeEach(() => fetchMock.catch(503)); | ||
|
||
let promise; | ||
beforeEach(() => { | ||
promise = new EnplugConfiguration(client, mockConfiguration).update(); | ||
}); | ||
|
||
it('should resolve the promise', () => promise.should.be.fulfilled); | ||
it('should set the volume', () => promise.then(v => v.volume).should.eventually.equal(28)); | ||
it('should set the deviceSerial', () => promise.then(v => v.deviceSerial).should.eventually.equal('LLCM2')); | ||
}); |
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,61 @@ | ||
import Resource from './Resource'; | ||
|
||
/** | ||
* EnplugScreenshot resource | ||
*/ | ||
class EnplugScreenshot extends Resource { | ||
/** | ||
* Creates a new enplugScreenshot | ||
* | ||
* Will populate itself with the values given to it after the client parameter | ||
* @example <caption>Assigning partial enplug screenshot data to a new instance</caption> | ||
* const client = new Client(); | ||
* const partialEnplugScreenshot = { | ||
* href: '/1/SYNC/enplugs/LLM3/screenshot', | ||
* contentType: 'image/jpeg', | ||
* name: '1', | ||
* }; | ||
* const enplugScreenshot = new EnplugScreenshot(client, partialEnplugScreenshot); | ||
* | ||
* enplugScreenshot.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 ID | ||
* @param {string} customerCode Customer code | ||
* @param {string} deviceSerial Enplug Serial | ||
* @returns {string} URI to instance of enplug screenshot | ||
*/ | ||
static makeHref(customerCode, deviceSerial) { | ||
return { | ||
href: `/1/${customerCode}/enplugs/${deviceSerial}/screenshot`, | ||
}; | ||
} | ||
|
||
/** | ||
* Fetches the data for this enplug screenshot via the client | ||
* @returns {Promise} If successful, a hydrated instance of this enplug screenshot | ||
*/ | ||
fetch() { | ||
return this.client.get(this.href) | ||
.then(response => new EnplugScreenshot(this.client, this, { | ||
name: response.headers.get('Name'), | ||
contentType: response.headers.get('Content-Type'), | ||
data: response.blob(), | ||
})); | ||
} | ||
} | ||
|
||
export default EnplugScreenshot; |
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,15 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import Client from '../Client'; | ||
import EnplugScreenshot from './EnplugScreenshot'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When instantiating an enplug screenshot based on customer and enplug serial', () => { | ||
const client = new Client(); | ||
const enplugScreenshot = new EnplugScreenshot(client, EnplugScreenshot.makeHref('SYNC', 'SERIAL11')); | ||
|
||
it('should set the href', () => enplugScreenshot.href.should.equal('/1/SYNC/enplugs/SERIAL11/screenshot')); | ||
it('should not be hydrated', () => enplugScreenshot.hydrated.should.equal(false)); | ||
}); |