node-test-bed-adapter
Version:
An adapter to connect a node.js application to the Test-bed's Common Information Space or Common Simulation Space.
82 lines • 2.87 kB
JavaScript
import { LogLevel } from './index.mjs';
import proxyquire from 'proxyquire';
import { EventEmitter } from 'events';
describe('TestBedAdapter', () => {
let TestBedAdapterMock;
class KafkaClientMock extends EventEmitter {
config;
constructor(config) {
super();
this.config = config;
setTimeout(() => this.emit('ready'), 10);
}
}
class ConsumerMock extends EventEmitter {
constructor(_client, _topics, _options) {
super();
setTimeout(() => this.emit('ready'), 10);
}
addTopics(..._args) { }
}
class ProducerMock extends EventEmitter {
constructor(_client, _topics, _options) {
super();
setTimeout(() => this.emit('ready'), 10);
}
createTopics(..._args) { }
send(_pr, cb) {
cb(null, '');
}
}
beforeAll((done) => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
TestBedAdapterMock = proxyquire('./index', {
kafkajs: {
Kafka: KafkaClientMock,
Consumer: ConsumerMock,
Producer: ProducerMock,
},
}).TestBedAdapter;
done();
});
it('should throw when there is no configuration file', () => {
const foo = () => new TestBedAdapterMock();
expect(foo).toThrow();
});
it('should throw when the kafkaHost or clientId is missing', () => {
const foo = () => new TestBedAdapterMock({});
expect(foo).toThrow();
});
it('should not automatically connect to the testbed', () => {
const result = new TestBedAdapterMock({
brokers: ['localhost:3501'],
schemaRegistry: 'localhost:3502',
groupId: 'client',
});
expect(result.isConnected).toBe(false);
});
it('should connect to the testbed', (done) => {
const tba = new TestBedAdapterMock({
brokers: ['localhost:3501'],
schemaRegistry: 'localhost:3502',
groupId: 'client',
logging: {
logToConsole: LogLevel.Info,
},
});
tba.on('ready', () => {
expect(tba.isConnected).toBe(true);
done();
});
tba.connect();
});
it('should load the test-bed-config.json from the config folder', () => {
const testbed = new TestBedAdapterMock('./src/test/config/test-bed-config.json');
const configuration = testbed.configuration;
expect(configuration.kafkaHost).toEqual('localhost:3501');
expect(configuration.produce).toBeTruthy();
expect(configuration.consume).toBeTruthy();
expect(configuration.consume && configuration.consume.length).toBe(2); // time and invitations
});
});
//# sourceMappingURL=test-bed-adapter.spec.mjs.map