UNPKG

@csllc/j1939

Version:

J1939 transport layer for CANBUS communication

62 lines (41 loc) 1.47 kB
const J1939Mock = require('./bus-mock'); const sinon = require('sinon'); const J1939 = require('../..'); const expect = require('chai').expect; const assert = require('chai').assert; const MY_ADDR = 0xFE; describe('Address Claim', function() { beforeEach(function() { this.remote = new J1939Mock({ address: 1, name: [1, 0, 0, 0, 0, 0, 0, 0], }); this.local = new J1939(this.remote.localBus(), { address: MY_ADDR, name: [1, 2, 3, 4, 5, 6, 7, 8], }); }); afterEach(function() { this.local.close(); this.remote.close(); }); it('should claim an address if no response on the bus', function(done) { let me = this; var openSpy = sinon.spy(); var addressSpy = sinon.spy(); me.local.on('open', openSpy); me.local.on('address', addressSpy); setTimeout(function() { // after 1 sec, the open event should have been called exactly once // with my address expect(addressSpy.callCount).equals(2); expect(openSpy.callCount).to.equal(1); expect(addressSpy.getCall(0).args).to.eql([me.local.CLAIM_IN_PROGRESS, MY_ADDR]); expect(addressSpy.getCall(1).args).to.eql([me.local.CLAIM_SUCCESSFUL, MY_ADDR]); expect(openSpy.getCall(0).args[0]).to.equal(MY_ADDR); done(); }, 1000); // opening the bus kicks off the address claim process this.remote.openBus(); }); });