1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00
unleash.unleash/frontend/src/component/common/util.test.ts
Nuno Góis 4167a60588
feat: biome lint frontend (#4903)
Follows up on https://github.com/Unleash/unleash/pull/4853 to add Biome
to the frontend as well.


![image](https://github.com/Unleash/unleash/assets/14320932/1906faf1-fc29-4172-a4d4-b2716d72cd65)

Added a few `biome-ignore` to speed up the process but we may want to
check and fix them in the future.
2023-10-02 13:25:46 +01:00

118 lines
3.7 KiB
TypeScript

import { updateWeightEdit } from './util';
const variantTemplate = {
id: '0',
name: 'A',
weight: 0,
weightType: 'variable' as const,
stickiness: 'default',
isValid: true,
new: false,
};
describe('updateWeightEdit', () => {
it('can assign weight to one only variant', () => {
const variants = [variantTemplate];
expect(updateWeightEdit(variants, 100)).toMatchInlineSnapshot(`
[
{
"id": "0",
"isValid": true,
"name": "A",
"new": false,
"stickiness": "default",
"weight": 100,
"weightType": "variable",
},
]
`);
});
it('can distribute weight between 2 variants evenly', () => {
const variants = [
variantTemplate,
{ ...variantTemplate, id: '2', name: 'B' },
];
updateWeightEdit(variants, 100).forEach((variant) => {
expect(variant).toHaveProperty('weight', 50);
});
});
it('can distribute weight between 8 variants evenly', () => {
const variants = Array.from({ length: 8 }, (_, i) => ({
...variantTemplate,
id: `${i}`,
name: `${i}`,
weight: i,
}));
updateWeightEdit(variants, 1000).forEach((variant) => {
expect(variant).toHaveProperty('weight', 125);
});
});
it('can distribute weight between 8 variants evenly and assign the remainder to the last variant', () => {
const variants = Array.from({ length: 8 }, (_, i) => ({
...variantTemplate,
id: `${i}`,
name: `${i}`,
weight: i,
}));
const weights = updateWeightEdit(variants, 100).map(
(variant) => variant.weight,
);
expect(weights).toEqual([13, 12, 13, 12, 13, 12, 13, 12]);
});
it('can adjust variable weight to get correct sum', () => {
const variants = [
{ ...variantTemplate, weightType: 'fix' as const, weight: 333 },
{ ...variantTemplate, id: '2', name: 'B' },
];
const weights = updateWeightEdit(variants, 1000).map(
(variant) => variant.weight,
);
expect(weights).toEqual([333, 667]);
});
it('can deal with complex example', () => {
const variants = [
{ ...variantTemplate, weightType: 'fix' as const, weight: 333 },
{ ...variantTemplate, id: '2', name: 'B' },
{ ...variantTemplate, id: '3', name: 'C' },
{ ...variantTemplate, id: '4', name: 'D' },
{ ...variantTemplate, id: '5', name: 'E' },
{
...variantTemplate,
id: '6',
name: 'F',
weightType: 'fix' as const,
weight: 111,
},
{ ...variantTemplate, id: '7', name: 'G' },
{ ...variantTemplate, id: '8', name: 'H' },
];
const weights = updateWeightEdit(variants, 1000).map(
(variant) => variant.weight,
);
expect(weights).toEqual([333, 93, 93, 93, 92, 111, 93, 92]);
});
it('can deal with 0-weight variable variant', () => {
const variants = [
{ ...variantTemplate, weightType: 'fix' as const, weight: 500 },
{
...variantTemplate,
weightType: 'fix' as const,
weight: 500,
id: '2',
name: 'B',
},
{ ...variantTemplate, id: '3', name: 'C' },
];
const weights = updateWeightEdit(variants, 1000).map(
(variant) => variant.weight,
);
expect(weights).toEqual([500, 500, 0]);
});
});