Skip to content

Commit

Permalink
test: migrate to vitest (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyv committed Jul 30, 2024
1 parent 0b2d348 commit 7f2f24e
Show file tree
Hide file tree
Showing 36 changed files with 1,012 additions and 1,317 deletions.
153 changes: 0 additions & 153 deletions test/mix.test.cjs

This file was deleted.

91 changes: 91 additions & 0 deletions test/mix.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { describe, it, expect } from 'vitest';
import chroma from '../index.js';

describe('Some tests for chroma.color()', () => {
it('hsv interpolation white <-> red', () => {
const result = chroma('white').interpolate('red', 0.5, 'hsv');
expect(result.hex()).toBe('#ff8080');
});

it('use mix as alias', () => {
const result = chroma('white').mix('red', 0.5, 'hsv');
expect(result.hex()).toBe('#ff8080');
});

it('alternative mix syntax', () => {
const result = chroma.mix('red', 'blue', 0.25, 'rgb');
expect(result.hex()).toBe('#bf0040');
});

it('hsl interpolation white <-> red', () => {
const result = chroma('white').interpolate('red', 0.5, 'hsl');
expect(result.hex()).toBe('#ff8080');
});

it('rgb interpolation white <-> red', () => {
const result = chroma('white').interpolate('red', 0.5, 'rgb');
expect(result.hex()).toBe('#ff8080');
});

it('hsv interpolation red <-> white', () => {
const result = chroma('red').interpolate('white', 0.5, 'hsv');
expect(result.hex()).toBe('#ff8080');
});

it('hsl interpolation red <-> white', () => {
const result = chroma('red').interpolate('white', 0.5, 'hsl');
expect(result.hex()).toBe('#ff8080');
});

it('rgb interpolation red <-> white', () => {
const result = chroma('red').interpolate('white', 0.5, 'rgb');
expect(result.hex()).toBe('#ff8080');
});

it('interpolation short function', () => {
const interpolateFn = (t) => chroma.interpolate('#ff0000', '#ffffff', t, 'hsv').hex();

expect(interpolateFn(0)).toBe('#ff0000');
expect(interpolateFn(0.5)).toBe('#ff8080');
expect(interpolateFn(1)).toBe('#ffffff');
});

it('num interpolation white <-> red', () => {
const result = chroma(0xffffff).interpolate(0xff0000, 0.5, 'num');
expect(result.hex()).toBe('#ff7fff');
});

it('num interpolation red <-> white', () => {
const result = chroma(0xff0000).interpolate(0xffffff, 0.5, 'num');
expect(result.hex()).toBe('#ff7fff');
});

it('interpolation short function with num provided', () => {
const interpolateFn = (t) => chroma.interpolate(0xff0000, 0xffffff, t, 'num').hex();

expect(interpolateFn(0)).toBe('#ff0000');
expect(interpolateFn(0.5)).toBe('#ff7fff');
expect(interpolateFn(1)).toBe('#ffffff');
});

it('interpolate in num', () => {
const result = chroma.interpolate(chroma.num(0xffffe0), chroma.num(0x102180), 0.5, 'num');
expect(result.hex()).toBe('#8810b0');
expect(result.num()).toBe(8917168);
});

it('interpolate in hsv', () => {
const result = chroma.interpolate('white', 'black', 0.5, 'hsv');
expect(result.hex()).toBe('#808080');
});

it('interpolate in hsl', () => {
const result = chroma.interpolate('lightyellow', 'navy', 0.5, 'hsl');
expect(result.hex()).toBe('#31ff98');
});

it('interpolate in lrgb', () => {
const result = chroma.interpolate('red', 'blue', 0.5, 'lrgb');
expect(result.hex()).toBe('#b400b4');
});
});
54 changes: 0 additions & 54 deletions test/num.test.cjs

This file was deleted.

27 changes: 27 additions & 0 deletions test/num.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, it, expect } from 'vitest';
import chroma from '../index.js';

describe('Some tests for chroma.num()', () => {
it('number output', () => {
const color = chroma.hsl(0, 1, 0.5, 0.5);
expect(color.num()).toBe(0xff0000);
});

it('num color', () => {
const colors = [chroma(0xff0000), chroma(0x000000), chroma(0xffffff), chroma(0x31ff98), chroma('red')];

expect(colors[0].hex()).toBe('#ff0000');
expect(colors[0].num()).toBe(0xff0000);

expect(colors[1].hex()).toBe('#000000');
expect(colors[1].num()).toBe(0x000000);

expect(colors[2].hex()).toBe('#ffffff');
expect(colors[2].num()).toBe(0xffffff);

expect(colors[3].hex()).toBe('#31ff98');
expect(colors[3].num()).toBe(0x31ff98);

expect(colors[4].num()).toBe(0xff0000);
});
});
27 changes: 0 additions & 27 deletions test/num2rgb.test.cjs

This file was deleted.

24 changes: 24 additions & 0 deletions test/num2rgb.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, it, expect } from 'vitest';
import num2rgb from '../src/io/num/num2rgb.js';

describe('Testing num2rgb color conversions', () => {
const testCases = {
black: { in: 0x000000, out: [0, 0, 0, 1] },
white: { in: 0xffffff, out: [255, 255, 255, 1] },
red: { in: 0xff0000, out: [255, 0, 0, 1] },
green: { in: 0x00ff00, out: [0, 255, 0, 1] },
blue: { in: 0x0000ff, out: [0, 0, 255, 1] },
yellow: { in: 0xffff00, out: [255, 255, 0, 1] },
cyan: { in: 0x00ffff, out: [0, 255, 255, 1] },
magenta: { in: 0xff00ff, out: [255, 0, 255, 1] }
};

Object.keys(testCases).forEach((key) => {
const { in: input, out: expected } = testCases[key];

it(`should convert numeric HEX to RGB for ${key}`, () => {
const result = num2rgb(input);
expect(result).toEqual(expected);
});
});
});
Loading

0 comments on commit 7f2f24e

Please sign in to comment.