-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25402c3
commit e3dce3e
Showing
5 changed files
with
256 additions
and
27 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,54 @@ | ||
const test = require('narval') | ||
|
||
const Mock = function () { | ||
let sandbox = test.sinon.createSandbox() | ||
|
||
const stubs = { | ||
hap: { | ||
Service: sandbox.stub(), | ||
Characteristic: sandbox.stub() | ||
}, | ||
registerAccessory: sandbox.stub() | ||
} | ||
|
||
const instances = { | ||
accessoryInformation: { | ||
setCharacteristic: sandbox.stub().callsFake(() => { | ||
return instances.accessoryInformation | ||
}) | ||
}, | ||
switch: { | ||
on: sandbox.stub().callsFake(() => { | ||
return instances.switch | ||
}), | ||
getCharacteristic: sandbox.stub().callsFake(() => { | ||
return instances.switch | ||
}) | ||
} | ||
} | ||
|
||
stubs.hap.Characteristic.Model = 'MODEL' | ||
stubs.hap.Characteristic.SerialNumber = 'SERIAL_NUMBER' | ||
stubs.hap.Characteristic.Manufacturer = 'MANUFACTURER' | ||
stubs.hap.Characteristic.On = 'ON' | ||
|
||
stubs.hap.Service.AccessoryInformation = sandbox.stub().callsFake(function () { | ||
return instances.accessoryInformation | ||
}) | ||
|
||
stubs.hap.Service.Switch = sandbox.stub().callsFake(function () { | ||
return instances.switch | ||
}) | ||
|
||
const restore = () => { | ||
sandbox.restore() | ||
} | ||
|
||
return { | ||
restore, | ||
stubs, | ||
instances | ||
} | ||
} | ||
|
||
module.exports = Mock |
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,25 @@ | ||
const test = require('narval') | ||
|
||
const mockery = require('./mockery') | ||
|
||
const MODULE = 'request-promise' | ||
|
||
const Mock = function () { | ||
let sandbox = test.sinon.createSandbox() | ||
|
||
const stub = sandbox.stub().resolves() | ||
|
||
const restore = () => { | ||
sandbox.restore() | ||
mockery.deregister(MODULE) | ||
} | ||
|
||
mockery.register(MODULE, stub) | ||
|
||
return { | ||
restore, | ||
stub | ||
} | ||
} | ||
|
||
module.exports = Mock |
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,153 @@ | ||
const test = require('narval') | ||
|
||
const HomebridgeMocks = require('../../Homebridge.mocks') | ||
const RequestPromiseMocks = require('../../RequestPromise.mocks') | ||
|
||
test.describe('Switch Plugin Factory', () => { | ||
let homebridge | ||
let requestPromise | ||
let SwitchFactory | ||
let Switch | ||
let switchPlugin | ||
let fooConfig | ||
let sandbox | ||
let log | ||
|
||
test.beforeEach(() => { | ||
sandbox = test.sinon.createSandbox() | ||
log = sandbox.stub() | ||
fooConfig = { | ||
abilityName: 'foo-name', | ||
accessory: 'DomapicSwitch', | ||
apiKey: 'foo-api-key', | ||
bridgeUrl: 'foo-url/foo-id', | ||
name: 'foo-service-name foo-name', | ||
serviceName: 'foo-service-name', | ||
servicePackageName: 'foo-service-package', | ||
serviceProcessId: 'foo-service-processId' | ||
} | ||
homebridge = new HomebridgeMocks() | ||
requestPromise = new RequestPromiseMocks() | ||
|
||
SwitchFactory = require('../../../../lib/plugins/SwitchFactory') | ||
Switch = new SwitchFactory(homebridge.stubs.hap.Service, homebridge.stubs.hap.Characteristic) | ||
switchPlugin = new Switch(log, fooConfig) | ||
}) | ||
|
||
test.afterEach(() => { | ||
sandbox.restore() | ||
homebridge.restore() | ||
requestPromise.restore() | ||
}) | ||
|
||
test.describe('Switch static name getter', () => { | ||
test.it('should return accessory name', () => { | ||
test.expect(Switch.name).to.equal('DomapicSwitch') | ||
}) | ||
}) | ||
|
||
test.describe('Switch instance', () => { | ||
test.describe('logError method', () => { | ||
test.it('should log error message', () => { | ||
const FOO_MESSAGE = 'Foo error message' | ||
const error = new Error(FOO_MESSAGE) | ||
switchPlugin.logError(error) | ||
test.expect(log).to.have.been.calledWith(`ERROR: ${FOO_MESSAGE}`) | ||
}) | ||
}) | ||
|
||
test.describe('getServices method', () => { | ||
test.it('should set accesory Manufacturer as Domapic', () => { | ||
switchPlugin.getServices() | ||
test.expect(homebridge.instances.accessoryInformation.setCharacteristic).to.have.been.calledWith( | ||
homebridge.stubs.hap.Characteristic.Manufacturer, | ||
'Domapic' | ||
) | ||
}) | ||
|
||
test.it('should set accesory Model with configuration servicePackageName', () => { | ||
switchPlugin.getServices() | ||
test.expect(homebridge.instances.accessoryInformation.setCharacteristic).to.have.been.calledWith( | ||
homebridge.stubs.hap.Characteristic.Model, | ||
'foo-service-package' | ||
) | ||
}) | ||
|
||
test.it('should set accesory SerialNumber with configuration serviceProcessId', () => { | ||
switchPlugin.getServices() | ||
test.expect(homebridge.instances.accessoryInformation.setCharacteristic).to.have.been.calledWith( | ||
homebridge.stubs.hap.Characteristic.SerialNumber, | ||
'foo-service-processId' | ||
) | ||
}) | ||
|
||
test.it('should have configured switch service to call "getSwitchOnCharacteristic" method on get event', () => { | ||
switchPlugin.getServices() | ||
test.expect(homebridge.instances.switch.on).to.have.been.calledWith( | ||
'get', | ||
switchPlugin.getSwitchOnCharacteristic | ||
) | ||
}) | ||
|
||
test.it('should have configured switch service to call "setSwitchOnCharacteristic" method on set event', () => { | ||
switchPlugin.getServices() | ||
test.expect(homebridge.instances.switch.on).to.have.been.calledWith( | ||
'set', | ||
switchPlugin.setSwitchOnCharacteristic | ||
) | ||
}) | ||
}) | ||
|
||
test.describe('getSwitchOnCharacteristic method', () => { | ||
test.it('should call to request plugin bridge api, and invoque callback with result if request is success', () => { | ||
const fooData = 'foo' | ||
requestPromise.stub.resolves({ | ||
data: fooData | ||
}) | ||
const cb = sandbox.stub() | ||
return switchPlugin.getSwitchOnCharacteristic(cb) | ||
.then(() => { | ||
return Promise.all([ | ||
test.expect(requestPromise.stub).to.have.been.called(), | ||
test.expect(cb).to.have.been.calledWith(null, fooData) | ||
]) | ||
}) | ||
}) | ||
|
||
test.it('should invoque callback with error if request fails', () => { | ||
const fooError = new Error('foo') | ||
requestPromise.stub.rejects(fooError) | ||
const cb = sandbox.stub() | ||
return switchPlugin.getSwitchOnCharacteristic(cb) | ||
.then(() => { | ||
return test.expect(cb).to.have.been.calledWith(fooError) | ||
}) | ||
}) | ||
}) | ||
|
||
test.describe('setSwitchOnCharacteristic method', () => { | ||
test.it('should call to request plugin bridge api, and invoque callback with no data if request is success', () => { | ||
const fooData = 'foo' | ||
requestPromise.stub.resolves() | ||
const cb = sandbox.stub() | ||
return switchPlugin.setSwitchOnCharacteristic(fooData, cb) | ||
.then(() => { | ||
return Promise.all([ | ||
test.expect(requestPromise.stub.getCall(0).args[0].body.data).to.equal(fooData), | ||
test.expect(cb).to.have.been.called() | ||
]) | ||
}) | ||
}) | ||
|
||
test.it('should invoque callback with error if request fails', () => { | ||
const fooError = new Error('foo') | ||
requestPromise.stub.rejects(fooError) | ||
const cb = sandbox.stub() | ||
return switchPlugin.setSwitchOnCharacteristic('foo', cb) | ||
.then(() => { | ||
return test.expect(cb).to.have.been.calledWith(fooError) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
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