1
0
Fork 0
cautious-journey/test/TestLabels.ts

174 lines
3.9 KiB
TypeScript
Raw Normal View History

2020-08-15 04:40:56 +00:00
import { expect } from 'chai';
import { getLabelColor, getLabelNames, prioritySort, StateLabel } from '../src/labels.js';
import { random } from '../src/main.js';
2020-08-15 04:40:56 +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 = [{
adds: [],
2020-08-15 04:40:56 +00:00
becomes: [],
name: 'bin',
priority: 1,
removes: [],
2020-08-15 04:40:56 +00:00
requires: [],
}];
const states: Array<StateLabel> = [{
2020-08-15 22:37:03 +00:00
adds: [],
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: [],
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',
]);
});
});
describe('prioritize labels helper', () => {
2023-01-01 23:22:20 +00:00
it('should sort by priority', async () => {
const HIGH_LABEL = {
2020-08-15 22:37:03 +00:00
adds: [],
name: 'high',
priority: 5,
2020-08-15 22:37:03 +00:00
removes: [],
requires: [],
};
const LOW_LABEL = {
2020-08-15 22:37:03 +00:00
adds: [],
name: 'low',
priority: 1,
2020-08-15 22:37:03 +00:00
removes: [],
requires: [],
};
2020-08-15 17:38:33 +00:00
const sorted = prioritySort([LOW_LABEL, HIGH_LABEL]);
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 () => {
const FIRST_LABEL = {
2020-08-15 22:37:03 +00:00
adds: [],
name: 'label-a',
priority: 1,
2020-08-15 22:37:03 +00:00
removes: [],
requires: [],
};
const SECOND_LABEL = {
2020-08-15 22:37:03 +00:00
adds: [],
name: 'label-b',
priority: 1,
2020-08-15 22:37:03 +00:00
removes: [],
requires: [],
};
2020-08-15 17:38:33 +00:00
const sorted = prioritySort([SECOND_LABEL, FIRST_LABEL]);
expect(sorted[0]).to.deep.equal(FIRST_LABEL);
});
});
describe('label color helper', () => {
2023-01-01 23:22:20 +00:00
it('should return the value color', async () => {
expect(getLabelColor(['test'], random(), {
2020-08-15 22:37:03 +00:00
adds: [],
color: 'beans',
divider: '/',
name: '',
priority: 1,
2020-08-15 22:37:03 +00:00
removes: [],
requires: [],
values: [],
}, {
adds: [],
becomes: [],
color: 'not',
name: '',
priority: 1,
removes: [],
2020-08-15 22:37:03 +00:00
requires: [],
})).to.equal('not');
});
2023-01-01 23:22:20 +00:00
it('should return the state color when value color is unset', async () => {
expect(getLabelColor(['test'], random(), {
2020-08-15 22:37:03 +00:00
adds: [],
color: 'beans',
divider: '/',
name: '',
priority: 1,
2020-08-15 22:37:03 +00:00
removes: [],
requires: [],
values: [],
}, {
adds: [],
becomes: [],
color: '',
name: '',
priority: 1,
removes: [],
2020-08-15 22:37:03 +00:00
requires: [],
})).to.equal('beans');
});
2023-01-01 23:22:20 +00:00
it('should return the flag color', async () => {
expect(getLabelColor(['test'], random(), {
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 () => {
expect(getLabelColor(['test'], random(), {
adds: [],
name: '',
priority: 1,
removes: [],
requires: [],
})).to.equal('test');
});
});
2020-08-15 04:40:56 +00:00
});