Skip to content

Commit 2952206

Browse files
author
QuickSander
committed
feat: Added some unit tests for setBrightness.
1 parent 9565997 commit 2952206

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ HttpPushRgb.prototype = {
371371
if (!this.color || !this.color.brightness) {
372372
var url = this.brightness.set_url.replace('%s', level);
373373

374-
this._httpRequest(url, '', this.brightness.http_method, function(error, response, body) {
374+
this._httpRequest(url, '', this.brightness.http_method, function(error, response, responseBody) {
375375
if (!this._handleHttpErrorResponse('setBrightness()', error, response, responseBody, callback)) {
376376
this.log('setBrightness() successfully set to %s %', level);
377377
callback();
@@ -503,7 +503,7 @@ HttpPushRgb.prototype = {
503503

504504
this.log('_setRGB converting H:%s S:%s B:%s to RGB:%s ...', this.cache.hue, this.cache.saturation, this.cache.brightness, r + g + b);
505505

506-
this._httpRequest(url, '', this.color.http_method, function(error, response, body) {
506+
this._httpRequest(url, '', this.color.http_method, function(error, response, responseBody) {
507507
if (!this._handleHttpErrorResponse('_setRGB()', error, response, responseBody, callback)) {
508508
this.log('... _setRGB() successfully set to #%s', r + g + b);
509509
callback();

test/basics.test.js

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var TestConfig = function() {
2121
"color": {
2222
"status": "http://localhost:8080/color/status",
2323
"url": "http://localhost:8080/color/set/%s",
24-
"brightness": true
24+
"brightness": false
2525
},
2626
"brightness": {
2727
"status": "http://localhost:8080/brightness/status",
@@ -200,6 +200,80 @@ describe('Get power state', function () {
200200

201201
});
202202

203+
// -----------------------------------------------------------------------------
204+
describe('Set brightness', function () {
205+
206+
beforeEach(function () {
207+
// 1. Arrange
208+
this.testConfig = new TestConfig();
209+
210+
// This will also make sure to reset the embedded Sinon stubs.
211+
this.homebridgeStub = new (require('./homebridge.stub.js'))(this.testConfig);
212+
sut(this.homebridgeStub);
213+
214+
this.homebridgeStub.accessory._httpRequest = sinon.stub();
215+
this.homebridgeCallback = sinon.stub();
216+
217+
});
218+
219+
220+
it('sends HTTP GET request with correct URL', function () {
221+
// 1. Arrange
222+
var url = this.testConfig.brightness.url.replace('%s', 100);
223+
224+
// 2. Act
225+
this.homebridgeStub.accessory.setBrightness(100, this.homebridgeCallback);
226+
227+
// 3. Assert
228+
expect(this.homebridgeStub.accessory._httpRequest.firstCall.args[0]).equals(url);
229+
expect(this.homebridgeStub.accessory._httpRequest.firstCall.args[1]).to.be.empty; // Body empty.
230+
expect(this.homebridgeStub.accessory._httpRequest.firstCall.args[2]).equals('GET');
231+
});
232+
233+
it('replies to Homebridge on valid HTTP GET device response "100"', function () {
234+
// 2. Act
235+
// Trigger filling of callback
236+
this.homebridgeStub.accessory.setBrightness(100, this.homebridgeCallback);
237+
// Call collected HTTP response callback to simulate device response.
238+
this.homebridgeStub.accessory._httpRequest.firstCall.callback(undefined, {statusCode: 200}, '100');
239+
240+
// 3. Assert
241+
expect(this.homebridgeCallback.calledOnce).to.be.true;
242+
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['setBrightness() successfully set to %s %', 100]);
243+
});
244+
245+
it('sets RGB instead of brightness to Homebridge when config.color.brightness equals true', function () {
246+
// 1. Arrange
247+
this.homebridgeStub.accessory.color.brightness = true;
248+
249+
// 2. Act
250+
// Trigger filling of callback
251+
this.homebridgeStub.accessory.setBrightness(100, this.homebridgeCallback);
252+
// Call collected HTTP response callback to simulate device response.
253+
this.homebridgeStub.accessory._httpRequest.firstCall.callback(undefined, {statusCode: 200}, '100');
254+
255+
// 3. Assert
256+
expect(this.homebridgeCallback.calledOnce).to.be.true; // But now inside _setRGB.
257+
expect(this.homebridgeStub.logger.firstCall.args[0]).deep.equals('_setRGB converting H:%s S:%s B:%s to RGB:%s ...');
258+
expect(this.homebridgeStub.logger.secondCall.args).deep.equals(['... _setRGB() successfully set to #%s', 'FFFFFF']);
259+
});
260+
261+
it('replies an Error object with message "Received HTTP error code 500." to Homebridge on HTTP GET device response status code 500', function () {
262+
// 1. Arrange
263+
const HTTP_ERROR_STATUS_CODE = 500;
264+
265+
// 2. Act
266+
// Trigger filling of callback
267+
this.homebridgeStub.accessory.setBrightness(100, this.homebridgeCallback);
268+
// Call collected HTTP response callback to simulate device response.
269+
this.homebridgeStub.accessory._httpRequest.firstCall.callback(undefined, {statusCode: HTTP_ERROR_STATUS_CODE}, 'Dummy error');
270+
271+
// 3. Assert
272+
expect(this.homebridgeCallback.firstCall.args[0]).to.be.instanceOf(Error).and.have.property('message', 'Received HTTP error code '+HTTP_ERROR_STATUS_CODE+': "Dummy error"');
273+
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['setBrightness() returned HTTP error code: %s: "%s"', HTTP_ERROR_STATUS_CODE, 'Dummy error']);
274+
});
275+
276+
});
203277

204278
// -----------------------------------------------------------------------------
205279
describe('Get services', function () {

0 commit comments

Comments
 (0)