1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/routes/index.test.js
Christopher Kolstad c17a1980a2
Add service layer
This simplifies stores to just be storage interaction, they no longer react to events.

Controllers now call services and awaits the result from the call.

When the service calls are returned the database is updated.
This simplifies testing dramatically, cause you know that your state is
updated when returned from a call, rather than hoping the store has
picked up the event (which really was a command) and reacted to it.

Events are still emitted from eventStore, so other parts of the app can
react to events as they're being sent out.

As part of the move to services, we now also emit an application-created
event when we see a new client application.

Fixes: #685
Fixes: #595
2021-01-21 10:59:19 +01:00

92 lines
2.5 KiB
JavaScript

'use strict';
const test = require('ava');
const supertest = require('supertest');
const { EventEmitter } = require('events');
const store = require('../../test/fixtures/store');
const getLogger = require('../../test/fixtures/no-logger');
const getApp = require('../app');
const { createServices } = require('../services');
const eventBus = new EventEmitter();
function getSetup() {
const base = `/random${Math.round(Math.random() * 1000)}`;
const stores = store.createStores();
const config = {
baseUriPath: base,
stores,
eventBus,
enableLegacyRoutes: true,
getLogger,
};
const services = createServices(stores, config);
const app = getApp(config, services);
return {
base,
request: supertest(app),
};
}
test('api defintion', t => {
t.plan(5);
const { request, base } = getSetup();
return request
.get(`${base}/api/`)
.expect('Content-Type', /json/)
.expect(200)
.expect(res => {
t.truthy(res.body);
const { admin, client } = res.body.links;
t.true(admin.uri === '/api/admin');
t.true(client.uri === '/api/client');
t.true(
admin.links['feature-toggles'].uri === '/api/admin/features',
);
t.true(client.links.metrics.uri === '/api/client/metrics');
});
});
test('admin api defintion', t => {
t.plan(2);
const { request, base } = getSetup();
return request
.get(`${base}/api/admin`)
.expect('Content-Type', /json/)
.expect(200)
.expect(res => {
t.truthy(res.body);
t.true(
res.body.links['feature-toggles'].uri === '/api/admin/features',
);
});
});
test('client api defintion', t => {
t.plan(2);
const { request, base } = getSetup();
return request
.get(`${base}/api/client`)
.expect('Content-Type', /json/)
.expect(200)
.expect(res => {
t.truthy(res.body);
t.true(res.body.links.metrics.uri === '/api/client/metrics');
});
});
test('client legacy features uri', t => {
t.plan(3);
const { request, base } = getSetup();
return request
.get(`${base}/api/features`)
.expect('Content-Type', /json/)
.expect(200)
.expect(res => {
t.truthy(res.body);
t.true(res.body.version === 1);
t.deepEqual(res.body.features, []);
});
});