describe('TasmotaHandler - connect', () => { const tasmotaHandler = new TasmotaHandler(gladys, 'service-uuid-random'); sinon.spy(tasmotaHandler, 'handleMqttMessage'); beforeEach(() => { sinon.reset(); }); it('connect with subscription', () => { tasmotaHandler.connect(); assert.calledWith(gladys.service.getService, 'mqtt'); assert.callCount(mqttService.device.subscribe, 2); mqttService.device.subscribe.firstCall.calledWith( 'stat/+/+', tasmotaHandler.handleMqttMessage.bind(tasmotaHandler), ); mqttService.device.subscribe.secondCall.calledWith( 'tele/+/+', tasmotaHandler.handleMqttMessage.bind(tasmotaHandler), ); }); });
test('Should allow "fail" option to customize failures', async t => { const fail = spy() await add(undefined, undefined, { fail }) t.true(fail.calledOnce) t.is(typeof fail.firstCall.firstArg, 'string') })
it('stopInstancePubSub: should fail with empty event payload', () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {}; mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64'); sample.program.stopInstancePubSub(mocks.event, mocks.context, mocks.callback); assert.deepStrictEqual( mocks.callback.firstCall.args[0], new Error(`Attribute 'zone' missing from payload`) ); });
describe("AppController", function() { describe("getIndexPage", function() { it("should send hey", function() { let req = {} // Have `res` have a send key with a function value coz we use `res.send()` in our func let res = { // replace empty function with a spy send: sinon.spy() } indexPage.getIndexPage(req, res); // let's see what we get on res.send // console.log(res.send); // `res.send` called once expect(res.send.calledOnce).to.be.true; expect(res.send.firstCall.args[0]).to.equal("Hey"); }); }); });
function tryCatch (err, req, res, next) { try { let spy = sinon.spy(); if (err) { fn(err, req, res, spy); } else { fn(req, res, spy); } if (spy.called) { next(spy.firstCall.args[0]); } else { res.sendStatus(exports.testPassed); } } catch (e) { exports.spy.error = e; res.sendStatus(exports.testFailed); } }
it('should upload a backup', async () => { const { s3Spy, pgSpy } = makeMockHandler() const result = await handler(mockEvent) // handler should have called pgSpy with correct arguments expect(pgSpy.calledOnce).to.be.true expect(pgSpy.firstCall.args).to.have.length(1) const [arg0] = pgSpy.firstCall.args expect(arg0.S3_BUCKET).to.equal(mockEvent.S3_BUCKET) expect(arg0.PGDATABASE).to.equal(mockEvent.PGDATABASE) // handler should have called s3spy with correct arguments expect(s3Spy.calledOnce).to.be.true expect(s3Spy.firstCall.args).to.have.length(3) const [stream, config, key] = s3Spy.firstCall.args expect(stream).to.be.ok expect(config.S3_BUCKET).to.equal(mockEvent.S3_BUCKET) expect(config.PGDATABASE).to.equal(mockEvent.PGDATABASE) expect(key).to.equal('2017-05-02/dbname-02-05-2017@01-33-11.backup') expect(result).to.equal( 'mock-uploaded/2017-05-02/dbname-02-05-2017@01-33-11.backup' ) })
expect(res.send.firstCall.args[0]).to.equal("Hey");
runHooksSpy.firstCall.should.have.been.calledWith(ODM.Hook.types.beforeValidate); validateSpy.should.have.been.calledBefore(runHooksSpy.secondCall); runHooksSpy.secondCall.should.have.been.calledWith(ODM.Hook.types.afterValidate);
it(`startInstancePubSub: should fail with missing 'zone' attribute`, () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {label: 'testkey=value'}; mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64'); sample.program.startInstancePubSub( mocks.event, mocks.context, mocks.callback ); assert.deepStrictEqual( mocks.callback.firstCall.args[0], new Error(`Attribute 'zone' missing from payload`) ); });
test('Should allow "fail" option to customize failures', async t => { const fail = spy() await add(undefined, undefined, { fail }) t.true(fail.calledOnce) t.is(typeof fail.firstCall.firstArg, 'string') })
it(`stopInstancePubSub: should fail with missing 'zone' attribute`, () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {label: 'testkey=value'}; mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64'); sample.program.stopInstancePubSub(mocks.event, mocks.context, mocks.callback); assert.deepStrictEqual( mocks.callback.firstCall.args[0], new Error(`Attribute 'zone' missing from payload`) ); });
it(`stopInstancePubSub: should fail with missing 'label' attribute`, () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {zone: 'test-zone'}; mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64'); sample.program.stopInstancePubSub(mocks.event, mocks.context, mocks.callback); assert.deepStrictEqual( mocks.callback.firstCall.args[0], new Error(`Attribute 'label' missing from payload`) ); });
it(`startInstancePubSub: should fail with missing 'label' attribute`, () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {zone: 'test-zone'}; mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64'); sample.program.startInstancePubSub( mocks.event, mocks.context, mocks.callback ); assert.deepStrictEqual( mocks.callback.firstCall.args[0], new Error(`Attribute 'label' missing from payload`) ); });
it('startInstancePubSub: should fail with empty event payload', () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {}; mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64'); sample.program.startInstancePubSub( mocks.event, mocks.context, mocks.callback ); assert.deepStrictEqual( mocks.callback.firstCall.args[0], new Error(`Attribute 'zone' missing from payload`) ); });