1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/lib/routes/admin-api/feature-schema.test.js
Ivar Conradi Østhus d01c9d2dac
fix: use airbnb lint rules directly (#583)
This drops usage of finn-eslint rules as they are no
longer maintained.
2020-04-14 22:29:11 +02:00

121 lines
2.9 KiB
JavaScript

'use strict';
const test = require('ava');
const { featureShema } = require('./feature-schema');
test('should require URL firendly name', t => {
const toggle = {
name: 'io`dasd',
enabled: false,
strategies: [{ name: 'default' }],
};
const { error } = featureShema.validate(toggle);
t.deepEqual(error.details[0].message, '"name" must be URL friendly');
});
test('should be valid toggle name', t => {
const toggle = {
name: 'app.name',
enabled: false,
strategies: [{ name: 'default' }],
};
const { value } = featureShema.validate(toggle);
t.deepEqual(value, toggle);
});
test('should strip extra variant fields', t => {
const toggle = {
name: 'app.name',
enabled: false,
strategies: [{ name: 'default' }],
variants: [
{
name: 'variant-a',
weight: 1,
unkown: 'not-allowed',
},
],
};
const { value } = featureShema.validate(toggle);
t.notDeepEqual(value, toggle);
t.falsy(value.variants[0].unkown);
});
test('should be possible to define variant overrides', t => {
const toggle = {
name: 'app.name',
enabled: false,
strategies: [{ name: 'default' }],
variants: [
{
name: 'variant-a',
weight: 1,
overrides: [
{
contextName: 'userId',
values: ['123'],
},
],
},
],
};
const { value, error } = featureShema.validate(toggle);
t.deepEqual(value, toggle);
t.falsy(error);
});
test('variant overrides must have corect shape', async t => {
t.plan(1);
const toggle = {
name: 'app.name',
enabled: false,
strategies: [{ name: 'default' }],
variants: [
{
name: 'variant-a',
weight: 1,
overrides: {
userId: ['not-alloed'],
sessionId: ['not-alloed'],
},
},
],
};
try {
await featureShema.validateAsync(toggle);
} catch (error) {
t.is(
error.details[0].message,
'"variants[0].overrides" must be an array',
);
}
});
test('should keep constraints', t => {
const toggle = {
name: 'app.constraints',
enabled: false,
strategies: [
{
name: 'default',
constraints: [
{
contextName: 'environment',
operator: 'IN',
values: ['asd'],
},
],
},
],
};
const { value, error } = featureShema.validate(toggle);
t.deepEqual(value, toggle);
t.falsy(error);
});