2017-06-06 09:56:29 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-17 09:24:49 +01:00
|
|
|
const test = require('ava');
|
2017-06-06 09:56:29 +02:00
|
|
|
const proxyquire = require('proxyquire');
|
2019-06-18 19:59:24 +02:00
|
|
|
const { EventEmitter } = require('events');
|
2017-06-28 10:20:22 +02:00
|
|
|
const express = require('express');
|
2019-04-30 21:14:23 +02:00
|
|
|
const getLogger = require('../test/fixtures/no-logger');
|
2017-06-06 09:56:29 +02:00
|
|
|
|
|
|
|
const getApp = proxyquire('./app', {
|
2018-11-24 12:58:30 +01:00
|
|
|
'./routes': class Index {
|
|
|
|
router() {
|
|
|
|
return express.Router();
|
|
|
|
}
|
2017-06-06 09:56:29 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-12-16 14:49:11 +01:00
|
|
|
const noop = () => {};
|
|
|
|
|
2019-06-18 19:59:24 +02:00
|
|
|
const eventStore = new EventEmitter();
|
2020-04-13 22:38:46 +02:00
|
|
|
const settingStore = {
|
|
|
|
get: () => {
|
|
|
|
Promise.resolve('secret');
|
|
|
|
},
|
|
|
|
};
|
2019-06-18 19:59:24 +02:00
|
|
|
|
2017-06-06 09:56:29 +02:00
|
|
|
const serverImpl = proxyquire('./server-impl', {
|
|
|
|
'./app': getApp,
|
|
|
|
'./metrics': {
|
2020-12-16 14:49:11 +01:00
|
|
|
createMetricsMonitor() {
|
|
|
|
return {
|
|
|
|
startMonitoring: noop,
|
|
|
|
stopMonitoring: noop,
|
|
|
|
};
|
2017-06-06 09:56:29 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
'./db': {
|
2019-06-18 19:59:24 +02:00
|
|
|
createStores() {
|
|
|
|
return {
|
2020-12-16 14:49:11 +01:00
|
|
|
db: { destroy: cb => cb() },
|
|
|
|
clientInstanceStore: { destroy: noop },
|
2020-12-17 19:22:30 +01:00
|
|
|
clientMetricsStore: { destroy: noop, on: noop },
|
2019-06-18 19:59:24 +02:00
|
|
|
eventStore,
|
2020-04-13 22:38:46 +02:00
|
|
|
settingStore,
|
2019-06-18 19:59:24 +02:00
|
|
|
};
|
2017-06-06 09:56:29 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
'./options': {
|
2017-06-28 10:17:14 +02:00
|
|
|
createOptions(o) {
|
2017-06-06 09:56:29 +02:00
|
|
|
return o;
|
|
|
|
},
|
|
|
|
},
|
2020-04-14 22:29:11 +02:00
|
|
|
'../migrator': function() {
|
2017-06-06 09:56:29 +02:00
|
|
|
return Promise.resolve();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should call preHook', async t => {
|
|
|
|
let called = 0;
|
|
|
|
await serverImpl.start({
|
2017-06-28 10:20:22 +02:00
|
|
|
port: 0,
|
2019-04-30 21:14:23 +02:00
|
|
|
getLogger,
|
2017-06-06 09:56:29 +02:00
|
|
|
preHook: () => {
|
|
|
|
called++;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
t.true(called === 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should call preRouterHook', async t => {
|
|
|
|
let called = 0;
|
2017-06-28 10:20:22 +02:00
|
|
|
await serverImpl.start({
|
|
|
|
port: 0,
|
2019-04-30 21:14:23 +02:00
|
|
|
getLogger,
|
2017-06-28 10:20:22 +02:00
|
|
|
preRouterHook: () => {
|
|
|
|
called++;
|
|
|
|
},
|
|
|
|
});
|
2017-06-06 09:56:29 +02:00
|
|
|
t.true(called === 1);
|
|
|
|
});
|
2019-06-18 19:59:24 +02:00
|
|
|
|
|
|
|
test('should call eventHook', async t => {
|
|
|
|
let called = 0;
|
|
|
|
await serverImpl.start({
|
|
|
|
port: 0,
|
|
|
|
getLogger,
|
|
|
|
eventHook: () => {
|
|
|
|
called++;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
eventStore.emit('feature-created', {});
|
|
|
|
t.true(called === 1);
|
|
|
|
});
|
2020-06-17 08:03:02 +02:00
|
|
|
|
|
|
|
test('should auto-create server on start()', async t => {
|
|
|
|
const { server } = await serverImpl.start({
|
|
|
|
port: 0,
|
|
|
|
getLogger,
|
|
|
|
start: true,
|
|
|
|
});
|
|
|
|
t.false(typeof server === 'undefined');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not create a server using create()', async t => {
|
|
|
|
const { server } = await serverImpl.create({
|
|
|
|
port: 0,
|
|
|
|
getLogger,
|
|
|
|
});
|
|
|
|
t.true(typeof server === 'undefined');
|
|
|
|
});
|
2020-12-16 14:49:11 +01:00
|
|
|
|
2020-12-17 09:41:19 +01:00
|
|
|
test('should shutdown the server when calling stop()', async t => {
|
2020-12-16 14:49:11 +01:00
|
|
|
const { server, stop } = await serverImpl.start({
|
|
|
|
port: 0,
|
|
|
|
getLogger,
|
|
|
|
start: true,
|
|
|
|
});
|
|
|
|
await stop();
|
|
|
|
t.is(server.address(), null);
|
|
|
|
});
|