2020-08-15 04:40:56 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
|
2023-01-01 23:15:22 +00:00
|
|
|
import { getLabelColor, getLabelNames, prioritySort, StateLabel } from '../src/labels.js';
|
|
|
|
import { random } from '../src/main.js';
|
2020-08-15 04:40:56 +00:00
|
|
|
|
2020-08-15 14:31:28 +00:00
|
|
|
describe('label helpers', () => {
|
2020-08-15 04:40:56 +00:00
|
|
|
describe('label name helper', () => {
|
2023-01-01 23:22:20 +00:00
|
|
|
it('should return an empty set', async () => {
|
2020-08-15 04:40:56 +00:00
|
|
|
expect(getLabelNames([], []).size).to.equal(0);
|
|
|
|
});
|
|
|
|
|
2023-01-01 23:22:20 +00:00
|
|
|
it('should return all flags', async () => {
|
2020-08-15 04:40:56 +00:00
|
|
|
const flags = [{
|
|
|
|
adds: [],
|
|
|
|
name: 'foo',
|
|
|
|
priority: 1,
|
|
|
|
removes: [],
|
|
|
|
requires: [],
|
|
|
|
}, {
|
|
|
|
adds: [],
|
|
|
|
name: 'bar',
|
|
|
|
priority: 1,
|
|
|
|
removes: [],
|
|
|
|
requires: [],
|
|
|
|
}];
|
|
|
|
const names = flags.map((f) => f.name);
|
|
|
|
|
|
|
|
const labels = getLabelNames(flags, []);
|
|
|
|
expect(Array.from(labels)).to.deep.equal(names);
|
|
|
|
});
|
|
|
|
|
2023-01-01 23:22:20 +00:00
|
|
|
it('should return all states', async () => {
|
2020-08-15 04:40:56 +00:00
|
|
|
const values = [{
|
2020-08-15 19:58:29 +00:00
|
|
|
adds: [],
|
2020-08-15 04:40:56 +00:00
|
|
|
becomes: [],
|
|
|
|
name: 'bin',
|
|
|
|
priority: 1,
|
2020-08-15 19:58:29 +00:00
|
|
|
removes: [],
|
2020-08-15 04:40:56 +00:00
|
|
|
requires: [],
|
|
|
|
}];
|
2020-08-18 13:54:57 +00:00
|
|
|
const states: Array<StateLabel> = [{
|
2020-08-15 22:37:03 +00:00
|
|
|
adds: [],
|
2020-08-18 13:54:57 +00:00
|
|
|
divider: '/',
|
2020-08-15 04:40:56 +00:00
|
|
|
name: 'foo',
|
|
|
|
priority: 1,
|
2020-08-15 22:37:03 +00:00
|
|
|
removes: [],
|
2020-08-15 04:40:56 +00:00
|
|
|
requires: [],
|
|
|
|
values,
|
|
|
|
}, {
|
2020-08-15 22:37:03 +00:00
|
|
|
adds: [],
|
2020-08-18 13:54:57 +00:00
|
|
|
divider: '/',
|
2020-08-15 04:40:56 +00:00
|
|
|
name: 'bar',
|
|
|
|
priority: 1,
|
2020-08-15 22:37:03 +00:00
|
|
|
removes: [],
|
2020-08-15 04:40:56 +00:00
|
|
|
requires: [],
|
|
|
|
values,
|
|
|
|
}];
|
|
|
|
|
|
|
|
const labels = getLabelNames([], states);
|
|
|
|
expect(Array.from(labels)).to.deep.equal([
|
|
|
|
'foo/bin',
|
|
|
|
'bar/bin',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2020-08-15 14:31:28 +00:00
|
|
|
|
|
|
|
describe('prioritize labels helper', () => {
|
2023-01-01 23:22:20 +00:00
|
|
|
it('should sort by priority', async () => {
|
2020-08-15 14:31:28 +00:00
|
|
|
const HIGH_LABEL = {
|
2020-08-15 22:37:03 +00:00
|
|
|
adds: [],
|
2020-08-15 14:31:28 +00:00
|
|
|
name: 'high',
|
|
|
|
priority: 5,
|
2020-08-15 22:37:03 +00:00
|
|
|
removes: [],
|
2020-08-15 14:31:28 +00:00
|
|
|
requires: [],
|
|
|
|
};
|
|
|
|
const LOW_LABEL = {
|
2020-08-15 22:37:03 +00:00
|
|
|
adds: [],
|
2020-08-15 14:31:28 +00:00
|
|
|
name: 'low',
|
|
|
|
priority: 1,
|
2020-08-15 22:37:03 +00:00
|
|
|
removes: [],
|
2020-08-15 14:31:28 +00:00
|
|
|
requires: [],
|
|
|
|
};
|
|
|
|
|
2020-08-15 17:38:33 +00:00
|
|
|
const sorted = prioritySort([LOW_LABEL, HIGH_LABEL]);
|
2020-08-15 14:31:28 +00:00
|
|
|
expect(sorted[0]).to.deep.equal(HIGH_LABEL);
|
|
|
|
});
|
|
|
|
|
2023-01-01 23:22:20 +00:00
|
|
|
it('should sort by name when priority is equal', async () => {
|
2020-08-15 14:31:28 +00:00
|
|
|
const FIRST_LABEL = {
|
2020-08-15 22:37:03 +00:00
|
|
|
adds: [],
|
2020-08-15 14:31:28 +00:00
|
|
|
name: 'label-a',
|
|
|
|
priority: 1,
|
2020-08-15 22:37:03 +00:00
|
|
|
removes: [],
|
2020-08-15 14:31:28 +00:00
|
|
|
requires: [],
|
|
|
|
};
|
|
|
|
const SECOND_LABEL = {
|
2020-08-15 22:37:03 +00:00
|
|
|
adds: [],
|
2020-08-15 14:31:28 +00:00
|
|
|
name: 'label-b',
|
|
|
|
priority: 1,
|
2020-08-15 22:37:03 +00:00
|
|
|
removes: [],
|
2020-08-15 14:31:28 +00:00
|
|
|
requires: [],
|
|
|
|
};
|
|
|
|
|
2020-08-15 17:38:33 +00:00
|
|
|
const sorted = prioritySort([SECOND_LABEL, FIRST_LABEL]);
|
2020-08-15 14:31:28 +00:00
|
|
|
expect(sorted[0]).to.deep.equal(FIRST_LABEL);
|
|
|
|
});
|
|
|
|
});
|
2020-08-15 19:58:29 +00:00
|
|
|
|
|
|
|
describe('label color helper', () => {
|
2023-01-01 23:22:20 +00:00
|
|
|
it('should return the value color', async () => {
|
2023-01-01 23:15:22 +00:00
|
|
|
expect(getLabelColor(['test'], random(), {
|
2020-08-15 22:37:03 +00:00
|
|
|
adds: [],
|
2020-08-15 19:58:29 +00:00
|
|
|
color: 'beans',
|
2020-08-18 13:54:57 +00:00
|
|
|
divider: '/',
|
2020-08-15 19:58:29 +00:00
|
|
|
name: '',
|
|
|
|
priority: 1,
|
2020-08-15 22:37:03 +00:00
|
|
|
removes: [],
|
|
|
|
requires: [],
|
2020-08-15 19:58:29 +00:00
|
|
|
values: [],
|
|
|
|
}, {
|
|
|
|
adds: [],
|
|
|
|
becomes: [],
|
|
|
|
color: 'not',
|
|
|
|
name: '',
|
|
|
|
priority: 1,
|
|
|
|
removes: [],
|
2020-08-15 22:37:03 +00:00
|
|
|
requires: [],
|
2020-08-15 19:58:29 +00:00
|
|
|
})).to.equal('not');
|
|
|
|
});
|
|
|
|
|
2023-01-01 23:22:20 +00:00
|
|
|
it('should return the state color when value color is unset', async () => {
|
2023-01-01 23:15:22 +00:00
|
|
|
expect(getLabelColor(['test'], random(), {
|
2020-08-15 22:37:03 +00:00
|
|
|
adds: [],
|
2020-08-15 19:58:29 +00:00
|
|
|
color: 'beans',
|
2020-08-18 13:54:57 +00:00
|
|
|
divider: '/',
|
2020-08-15 19:58:29 +00:00
|
|
|
name: '',
|
|
|
|
priority: 1,
|
2020-08-15 22:37:03 +00:00
|
|
|
removes: [],
|
|
|
|
requires: [],
|
2020-08-15 19:58:29 +00:00
|
|
|
values: [],
|
|
|
|
}, {
|
|
|
|
adds: [],
|
|
|
|
becomes: [],
|
|
|
|
color: '',
|
|
|
|
name: '',
|
|
|
|
priority: 1,
|
|
|
|
removes: [],
|
2020-08-15 22:37:03 +00:00
|
|
|
requires: [],
|
2020-08-15 19:58:29 +00:00
|
|
|
})).to.equal('beans');
|
|
|
|
});
|
|
|
|
|
2023-01-01 23:22:20 +00:00
|
|
|
it('should return the flag color', async () => {
|
2023-01-01 23:15:22 +00:00
|
|
|
expect(getLabelColor(['test'], random(), {
|
2020-08-15 19:58:29 +00:00
|
|
|
adds: [],
|
|
|
|
color: 'not',
|
|
|
|
name: '',
|
|
|
|
priority: 1,
|
|
|
|
removes: [],
|
|
|
|
requires: [],
|
|
|
|
})).to.equal('not');
|
|
|
|
});
|
|
|
|
|
2023-01-01 23:22:20 +00:00
|
|
|
it('should return a random color when the flag color is unset', async () => {
|
2023-01-01 23:15:22 +00:00
|
|
|
expect(getLabelColor(['test'], random(), {
|
2020-08-15 19:58:29 +00:00
|
|
|
adds: [],
|
|
|
|
name: '',
|
|
|
|
priority: 1,
|
|
|
|
removes: [],
|
|
|
|
requires: [],
|
|
|
|
})).to.equal('test');
|
|
|
|
});
|
|
|
|
});
|
2020-08-15 04:40:56 +00:00
|
|
|
});
|