More unit tests (#632)

* deepCopy tests

* add tests for randInt

* add test for getNextColor
This commit is contained in:
pngwn 2022-02-16 10:02:59 +00:00 committed by GitHub
parent 6fdc76c31b
commit b57999c29d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 9 deletions

View File

@ -1,24 +1,93 @@
import { assert, describe, expect, it } from "vitest";
import { prettyBytes } from "./helpers";
import { assert, describe, test } from "vitest";
import { prettyBytes, deepCopy, randInt, getNextColor } from "./helpers";
describe("prettyBytes", () => {
it("handle B", () => {
test("handle B", () => {
assert.equal(prettyBytes(10), "10.0 B");
});
it("handles KB", () => {
test("handles KB", () => {
assert.equal(prettyBytes(1_300), "1.3 KB");
});
it("handles MB", () => {
test("handles MB", () => {
assert.equal(prettyBytes(1_300_000), "1.2 MB");
});
it("handles GB", () => {
test("handles GB", () => {
assert.equal(prettyBytes(1_300_000_123), "1.2 GB");
});
it("handles PB", () => {
test("handles PB", () => {
assert.equal(prettyBytes(1_300_000_123_000), "1.2 PB");
});
});
describe("deepCopy", () => {
test("handle arrays", () => {
const array = [1, 2, 3];
const copy = deepCopy(array);
assert.ok(array !== copy);
assert.deepEqual(array, copy);
});
test("handle objects", () => {
const obj = { a: 1, b: 2, c: 3 };
const copy = deepCopy(obj);
assert.ok(obj !== copy);
assert.deepEqual(obj, copy);
});
test("handle complex structures", () => {
const obj = {
a: 1,
b: {
a: 1,
b: {
a: 1,
b: { a: 1, b: 2, c: 3 },
c: [
1,
2,
{ a: 1, b: { a: 1, b: { a: 1, b: 2, c: 3 }, c: [1, 2, 3] } }
]
},
c: 3
},
c: 3
};
const copy = deepCopy(obj);
assert.ok(obj !== copy);
assert.deepEqual(obj, copy);
});
});
describe("randInt", () => {
test("returns a random number", () => {
assert.typeOf(randInt(0, 10), "number");
});
test("respects min and max", () => {
const n = randInt(0, 10);
assert.ok(n >= 0 && n <= 10);
});
test("respects min and max when negative", () => {
const n = randInt(-100, -10);
assert.ok(n > -100 && n < -10);
});
});
describe("getNextColor", () => {
test("returns a color", () => {
assert.equal(getNextColor(0), "rgba(255, 99, 132, 1)");
});
test("returns a color when index is very high", () => {
assert.ok(
getNextColor(999999999).match(
/rgba\([0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}, [0-9]\)/
)
);
});
});

View File

@ -15,8 +15,7 @@ export function randInt(min: number, max: number): number {
return Math.floor(Math.random() * (max - min) + min);
}
export const getNextColor = (index: number, alpha?: number): string => {
alpha = alpha || 1;
export const getNextColor = (index: number, alpha: number = 1): string => {
let default_colors = [
[255, 99, 132],
[54, 162, 235],