connector.test.js 3.03 KB
import expect from 'expect.js';
import muk from 'muk';
import net from '../mock/fakeNet';

describe('connector.js', function() {

    let C;
    before(function() {
        C = muk('../../src/lib/connector', {
            'net': net
        });

        C.__set__('log', {
            info: function() {},
            error: function() {}
        });
    })

    describe('init', function() {
        it('new connector init ok', function(done) {
            var connect = new C({
                host: '1.1.1.1',
                port: '123'
            }, function(con) {
                expect(con.connected).to.be(true);
                done();
            });
        });

        it('new connector error', function(done) {
            var connect = new C({
                host: '1.1.1.1',
                port: '123'
            }, function(con) {}, function(client, data, error) {
                expect(error.message).to.be('mock error');
                done();
            });

            connect.client.addEmit('error', {
                message: 'mock error'
            });
        });

        it('new connect after error then reconnect', function(done) {
            var connect = new C({
                host: '1.1.1.1',
                port: '123'
            }, function(con) {}, function() {});
            connect.on('reconnect', function() {
                done();
            });
            connect.client.addEmit('error', {
                message: 'mock error'
            });
        });
    });

    describe('connector write then get data', function() {
        it('on data expect ok', function(done) {
            C.__set__('toHex', function() {});
            C.__set__('spliteRet', function() {
                return {
                    rets: ['mock']
                }
            });
            C.__set__('locateOrder', function() {
                return {
                    name: 'mock'
                }
            });

            var connect = new C({
                host: '1.1.1.1',
                port: '123'
            }, function(con) {}, function() {
                return 'ok';
            });

            connect.on('mock', function(data) {
                expect(data).to.be('ok');
                done();
            });

            connect.client.write('test');
        });
    });

    describe('sendOrder', function() {
        it('send expect same back', function(done) {
            var connect = new C({
                host: '1.1.1.1',
                port: '123'
            }, function(con) {}, function() {});
            C.__set__('spliteRet', function(mq) {
                return {
                    rets: [],
                    mq: mq
                };
            });
            let call = new Buffer('Mock', 'hex');
            connect.client.on('data', function(data) {
                expect(data).to.eql(call);
                expect(connect.orderMq[0]).to.be('mo');
                done();
            });
            connect.send('Mock');

        });
    })

    after(function() {
        muk.restore();
    });
});