Skip to content

Commit

Permalink
Adding tests for the HueAPIConfig to eliminate typo issues as identif…
Browse files Browse the repository at this point in the history
…ied in #155.
  • Loading branch information
peter-murray committed Dec 31, 2019
1 parent d45f101 commit f40492d
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/api/HueApiConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = class HueApiConfig {
this._remoteApi = remoteApi;
this._transport = transport;

this._isRemote = !! config.remote && !!remoteApi;
this._isRemote = !!config.remote && !!remoteApi;
}

/**
Expand Down Expand Up @@ -41,7 +41,7 @@ module.exports = class HueApiConfig {
if (this.isRemote) {
return this._remoteApi;
} else {
throw new ApiError('This API has not been set up a remote API');
throw new ApiError('This API has not been set up as a remote API');
}
}

Expand Down Expand Up @@ -131,13 +131,13 @@ module.exports = class HueApiConfig {

_requireRemote() {
if (!this.isRemote) {
throw new ApiError('The function in only valid on a remote Hue API instance');
throw new ApiError('The function is only valid on a remote Hue API instance');
}
}

_requireLocal() {
if (this.isRemote) {
throw new ApiError('The function in only valid on a local Hue API instance');
throw new ApiError('The function is only valid on a local Hue API instance');
}
}
};
106 changes: 106 additions & 0 deletions lib/api/HueApiConfig.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
'use strict';

const expect = require('chai').expect
, HueApiConfig = require('./HueApiConfig')
, Transport = require('./http/Transport')
, RemoteApi = require('./http/RemoteApi')
, ApiError = require('../ApiError')
;

const BASE_URL = 'https://localhost:443'
, DUMMY_CLIENT_KEY = 'aaaaaaaaaaaaaaaaaa'
, DUMMY_CLIENT_SECRET = 'abcd1234567890'
, DUMMY_CLIENT_ID = 'abcd1234567890abc'
, USERNAME = 'xxxxxxxxxxxxxxxxxxxx'
;


describe('HueApiConfig', () => {

const dummyTransport = new Transport(null, null, null);

describe('Remote API', () => {

const dummyRemoteApi = new RemoteApi(null, null);

let config;

beforeEach(() => {
config = new HueApiConfig({
remote: true,
username: USERNAME,
baseUrl: BASE_URL,
clientSecret: DUMMY_CLIENT_SECRET,
clientId: DUMMY_CLIENT_ID
},
dummyTransport,
dummyRemoteApi
);
});

it('should get #baseURL', () => {
expect(config.baseUrl).to.equal(BASE_URL);
});

it('should get #username', () => {
expect(config.username).to.equal(USERNAME);
});

it('should identify a remote API', () => {
expect(config.isRemote).to.be.true;
expect(config.transport).to.equal(dummyTransport);
expect(config.remote).to.equal(dummyRemoteApi);
});


it('should get #clientSecret and #clientId', () => {
expect(config.clientSecret).to.equal(DUMMY_CLIENT_SECRET);
expect(config.clientId).to.equal(DUMMY_CLIENT_ID);
});


it('should fail on #clientKey', () => {
try {
config.clientKey;
} catch (err) {
expect(err.message).to.contain('only valid on a local Hue API instance');
}
});
});


describe('Local API', () => {

let config;

beforeEach(() => {
config = new HueApiConfig({
baseUrl: BASE_URL,
username: USERNAME,
clientkey: DUMMY_CLIENT_KEY,
},
dummyTransport);
});

it('should get #baseURL', () => {
expect(config.baseUrl).to.equal(BASE_URL);
});

it('should get #username', () => {
expect(config.username).to.equal(USERNAME);
});

it('should get #clientKey', () => {
expect(config.clientKey).to.equal(DUMMY_CLIENT_KEY);
});

it('should error on #remote', () => {
try {
config.remote;
} catch (err) {
expect(err).to.be.instanceOf(ApiError);
expect(err.message).to.contain('not been set up as a remote API');
}
});
});
});

0 comments on commit f40492d

Please sign in to comment.