describe('.log', function () { it('proxies to winston.log with predefined level', async function () { const stub = sinon.stub(log.winston, 'log'); assume(log.log).to.be.a('function'); log.log('Testing'); assume(stub.getCall(0).args).to.deep.equal(['debug', 'Testing']); }); });
beforeEach(async () => { sinon.resetHistory(); mockGasket = makeGasket(); await buildConfigSet(mockGasket); docsSetupCallback = mockGasket.execApply.getCall(0).args[1]; mockDocsSetup = { link: 'README.md#with-hash' }; mockHandler = sinon.stub().resolves(mockDocsSetup); });
it('should register `Model`s schema difinition with Ajv validator', function() { const model = this.buildModel('Test3', { type: 'object' }, {}); const stub = sinon.stub(ODM.Model.validator, 'addSchema'); model._init(this.modelManager); stub.should.have.been.calledThrice; stub.getCall(0).should.have.been.calledWith(model.options.schema, model.name); stub.restore();//important! });
it('writes generated content to file', async () => { const mockContent = await generateContent(fullDocsConfigSet); await generateIndex(fullDocsConfigSet); assume(writeFileStub.getCall(0).args[1]).eqls(mockContent); });
describe('Directory', function () { describe('mkdirpSync', function () { it('should create directories recursively', function () { var spy = sinon.stub(fs, 'mkdirSync').returns(1); Directory.mkdirpSync('/some/long/path'); expect(spy.getCall(0).args[0]).to.equal('.'); expect(spy.getCall(1).args[0]).to.equal('some'); expect(spy.getCall(2).args[0].replace(/\\/g, '/')).to.equal('some/long'); expect(spy.getCall(3).args[0].replace(/\\/g, '/')).to.equal('some/long/path'); spy.restore(); }); }); });
describe('setAllPWMSync()', () => { const addr = 0x40; const on = 0x10; const off = 0xFE; const instance = pwm({ i2c, address: addr }).init(); it('should fail on wrong params', () => { (function () { instance.setAllPWMSync(4097, 0); }).should.throw(); }); it('should set all 4 registers for the channel', () => { i2c.resetAll(); instance.setAllPWMSync(on, off); i2c.writeByteSync.getCall(0).should.be.calledWith(addr, 0xFA, on & 0xFF); i2c.writeByteSync.getCall(1).should.be.calledWith(addr, 0xFB, on >> 8); i2c.writeByteSync.getCall(2).should.be.calledWith(addr, 0xFC, off & 0xFF); i2c.writeByteSync.getCall(3).should.be.calledWith(addr, 0xFD, off >> 8); }); });
it('writes README.md in docs root', async () => { await generateIndex(fullDocsConfigSet); assume(writeFileStub.getCall(0).args[0]).eqls(path.join(fullDocsConfigSet.docsRoot, 'README.md')); });
it(`must return function that calls '_resolve' under the hood and for ${ dns.NOTFOUND } error returns empty array`, () => { const error = new Error('some error'); error.code = dns.NOTFOUND; const cbSpy = sinon.spy(); const resolveSpy = sinon .stub(lookup, '_resolve') .callsFake((hostname, options, cb) => { assert.strictEqual(resolveSpy.getCall(0).args[0], hostname); assert.strictEqual(resolveSpy.getCall(0).args[1], options); cb(error); }); const resolveTask = lookup._resolveTaskBuilder(hostname, options); resolveTask(cbSpy); assert.isTrue(resolveSpy.calledOnce); assert.strictEqual(resolveSpy.getCall(0).args[0], hostname); assert.strictEqual(resolveSpy.getCall(0).args[1], options); assert.instanceOf(resolveSpy.getCall(0).args[2], Function); assert.isTrue(cbSpy.calledOnce); assert.isTrue(cbSpy.calledWithExactly(null, [])); });
it('must run two resolve tasks for IPv4 and IPv6 in parallel', () => { const options = {}; const resolveTaskBuilderStub = sinon .stub(lookup, '_resolveTaskBuilder') .callsFake(() => { return cb => {}; }); const asyncParallelSpy = sinon.spy(async, 'parallel'); lookup._resolveBoth(hostname, options); assert.isTrue(asyncParallelSpy.calledOnce); assert.isTrue(resolveTaskBuilderStub.calledTwice); assert.strictEqual(resolveTaskBuilderStub.getCall(0).args[0], hostname); assert.strictEqual(resolveTaskBuilderStub.getCall(1).args[0], hostname); assert.deepEqual( resolveTaskBuilderStub.getCall(0).args[1], Object.assign({}, options, { family: Lookup.IPv4 }) ); assert.deepEqual( resolveTaskBuilderStub.getCall(1).args[1], Object.assign({}, options, { family: Lookup.IPv6 }) ); });
it("must return function that calls '_resolve' under the hood and in case error calls callback with an error", () => { const error = new Error('some error'); const cbSpy = sinon.spy(); const resolveSpy = sinon .stub(lookup, '_resolve') .callsFake((hostname, options, cb) => { assert.strictEqual(resolveSpy.getCall(0).args[0], hostname); assert.strictEqual(resolveSpy.getCall(0).args[1], options); cb(error); }); const resolveTask = lookup._resolveTaskBuilder(hostname, options); resolveTask(cbSpy); assert.isTrue(resolveSpy.calledOnce); assert.strictEqual(resolveSpy.getCall(0).args[0], hostname); assert.strictEqual(resolveSpy.getCall(0).args[1], options); assert.instanceOf(resolveSpy.getCall(0).args[2], Function); assert.isTrue(cbSpy.calledOnce); assert.strictEqual(cbSpy.getCall(0).args.length, 1); assert.instanceOf(cbSpy.getCall(0).args[0], Error); assert.strictEqual(cbSpy.getCall(0).args[0].message, error.message); });
describe('setAllPWM()', () => { const addr = 0x40; const on = 0x10; const off = 0xFE; const instance = pwm({ i2c, address: addr }).init(); it('should fail on wrong params', () => { (function () { instance.setAllPWM(4097, 0, () => {}); }).should.throw(); }); it('should set all 4 registers for the channel', (done) => { i2c.resetAll(); instance.setAllPWM(on, off, (err) => { should.equal(err, null); i2c.writeByte.getCall(0).should.be.calledWith(addr, 0xFA, on & 0xFF); i2c.writeByte.getCall(1).should.be.calledWith(addr, 0xFB, on >> 8); i2c.writeByte.getCall(2).should.be.calledWith(addr, 0xFC, off & 0xFF); i2c.writeByte.getCall(3).should.be.calledWith(addr, 0xFD, off >> 8); done(); }); }); });
it('must correct call callback with error when family value is undefined', done => { const expectedError = new Error('some error'); const options = {family: undefined}; const cbSpy = sinon.stub(); const resolveStub = sinon.stub(lookup, '_resolve'); const resolveBothStub = sinon.stub(lookup, '_resolveBoth').rejects(expectedError); lookup.run(hostname, options, cbSpy); setImmediate(() => { assert.isTrue(resolveStub.notCalled); assert.isTrue(cbSpy.calledOnce); assert.strictEqual(cbSpy.getCall(0).args.length, 1); assert.instanceOf(cbSpy.getCall(0).args[0], Error); assert.strictEqual(cbSpy.getCall(0).args[0].message, expectedError.message); assert.isTrue(resolveBothStub.calledOnce); assert.isTrue(resolveBothStub.calledWithExactly(hostname, options)); done(); }); });
it('returns docsConfigSet from builder', async () => { getConfigSetStub.resetHistory(); const results = await buildConfigSet(mockGasket); assume(results).eqls(getConfigSetStub.getCall(0).returnValue); });
describe('.log', function () { it('proxies to winston.log with predefined level', async function () { const stub = sinon.stub(log.winston, 'log'); assume(log.log).to.be.a('function'); log.log('Testing'); assume(stub.getCall(0).args).to.deep.equal(['debug', 'Testing']); }); });
describe('Directory', () => { describe('mkdirpSync', () => { it('should create directories recursively', () => { let spy = sinon.stub(fs, 'mkdirSync').returns(1) Directory.mkdirpSync('/some/long/path') expect(spy.getCall(0).args[0]).to.equal('.') expect(spy.getCall(1).args[0]).to.equal('some') expect(spy.getCall(2).args[0].replace(/\\/g, '/')).to.equal('some/long') expect(spy.getCall(3).args[0].replace(/\\/g, '/')).to.equal('some/long/path') spy.restore() }) }) })