Skip to content

Commit

Permalink
test: added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
biomadeira committed Nov 1, 2024
1 parent e8b810e commit db2c1c9
Show file tree
Hide file tree
Showing 4 changed files with 406 additions and 0 deletions.
77 changes: 77 additions & 0 deletions tests/color-schemes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { fabric } from 'fabric';
import { defaultGradient, colorDefaultGradient, ncbiBlastGradient, colorNcbiBlastGradient } from '../src/color-schemes';

describe('defaultGradient', () => {
it('should have expected gradient color values', () => {
expect(defaultGradient[0.0]).toEqual([255, 64, 64]);
expect(defaultGradient[0.25]).toEqual([255, 255, 64]);
expect(defaultGradient[0.5]).toEqual([64, 255, 64]);
expect(defaultGradient[0.75]).toEqual([64, 255, 255]);
expect(defaultGradient[1.0]).toEqual([64, 64, 255]);
});

it('should have expected keys', () => {
expect(defaultGradient.keys).toEqual([0.0, 0.25, 0.5, 0.75, 1.0]);
});
});

describe('colorDefaultGradient', () => {
it('should apply default gradient to a fabric object', () => {
const canvasObj = new fabric.Object({});
colorDefaultGradient(canvasObj, 0, 100);

const gradient = canvasObj.get('fill') as fabric.Gradient;
expect(gradient.type).toBe('linear');
expect(gradient.coords).toEqual({ x1: 0, y1: 0, x2: 100, y2: 0 });

const expectedColorStops = [
{ offset: 0.0, color: 'rgb(255,64,64)' },
{ offset: 0.25, color: 'rgb(255,255,64)' },
{ offset: 0.5, color: 'rgb(64,255,64)' },
{ offset: 0.75, color: 'rgb(64,255,255)' },
{ offset: 1.0, color: 'rgb(64,64,255)' },
];

expect(gradient.colorStops).toEqual(expectedColorStops);
});
});

describe('ncbiBlastGradient', () => {
it('should have expected NCBI Blast gradient color values', () => {
expect(ncbiBlastGradient[0]).toEqual([0, 0, 0]);
expect(ncbiBlastGradient[40]).toEqual([0, 32, 233]);
expect(ncbiBlastGradient[50]).toEqual([117, 234, 76]);
expect(ncbiBlastGradient[80]).toEqual([219, 61, 233]);
expect(ncbiBlastGradient[200]).toEqual([219, 51, 36]);
});

it('should have expected keys', () => {
expect(ncbiBlastGradient.keys).toEqual([0, 40, 50, 80, 200]);
});
});

describe('colorNcbiBlastGradient', () => {
it('should apply NCBI Blast gradient to a fabric object', () => {
const canvasObj = new fabric.Object({});
colorNcbiBlastGradient(canvasObj, 0, 100);

const gradient = canvasObj.get('fill') as fabric.Gradient;
expect(gradient.type).toBe('linear');
expect(gradient.coords).toEqual({ x1: 0, y1: 0, x2: 100, y2: 0 });

const expectedColorStops = [
{ offset: 0.0, color: 'rgb(0,0,0)' },
{ offset: 0.199999, color: 'rgb(0,0,0)' },
{ offset: 0.2, color: 'rgb(0,32,233)' },
{ offset: 0.399999, color: 'rgb(0,32,233)' },
{ offset: 0.4, color: 'rgb(117,234,76)' },
{ offset: 0.599999, color: 'rgb(117,234,76)' },
{ offset: 0.6, color: 'rgb(219,61,233)' },
{ offset: 0.799999, color: 'rgb(219,61,233)' },
{ offset: 0.8, color: 'rgb(219,51,36)' },
{ offset: 1.0, color: 'rgb(219,51,36)' },
];

expect(gradient.colorStops).toEqual(expectedColorStops);
});
});
105 changes: 105 additions & 0 deletions tests/color-utilities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {
getRgbColorGradient,
getRgbColorFixed,
getGradientSteps,
HSVtoRGB,
colorByDatabaseName,
} from '../src/color-utilities';
import { ColorType, ColorSchemeEnum } from '../src/custom-types';

describe('getRgbColorGradient', () => {
const colorScheme: ColorType = {
keys: [0, 1, 2, 3, 4],
0: [255, 0, 0],
1: [0, 255, 0],
2: [0, 0, 255],
3: [255, 255, 0],
4: [255, 0, 255],
};
const gradientSteps = [0, 1, 2, 3, 4];

it('returns color for zero score', () => {
expect(getRgbColorGradient(0, gradientSteps, colorScheme)).toBe('rgb(255,0,0)');
});

it('throws error for mismatched lengths between gradientSteps and colorScheme', () => {
const invalidGradientSteps = [0, 1, 2];
expect(() => getRgbColorGradient(0, invalidGradientSteps, colorScheme)).toThrow(
'Color Scheme and Gradient Steps should have matching lengths!'
);
});

it('calculates gradient color for a score within step range', () => {
const color = getRgbColorGradient(1.5, gradientSteps, colorScheme);
expect(color).toMatch(/rgb\(\d{1,3},\d{1,3},\d{1,3}\)/);
});
});

describe('getRgbColorFixed', () => {
const colorScheme: ColorType = {
keys: [0, 1, 2, 3, 4],
0: [255, 0, 0],
1: [0, 255, 0],
2: [0, 0, 255],
3: [255, 255, 0],
4: [255, 0, 255],
};
const gradientSteps = [0, 1, 2, 3, 4];

it('returns fixed color for score at the lowest range', () => {
expect(getRgbColorFixed(0.5, gradientSteps, colorScheme)).toBe('rgb(255,0,0)');
});

it('returns fixed color for score within middle range', () => {
expect(getRgbColorFixed(1.5, gradientSteps, colorScheme)).toBe('rgb(0,255,0)');
expect(getRgbColorFixed(2.5, gradientSteps, colorScheme)).toBe('rgb(0,0,255)');
});

it('returns fixed color for score in the highest range', () => {
expect(getRgbColorFixed(5, gradientSteps, colorScheme)).toBe('rgb(255,0,255)');
});
});

describe('getGradientSteps', () => {
it('returns fixed gradient steps for ColorSchemeEnum.fixed', () => {
const steps = getGradientSteps(0, 100, 1, ColorSchemeEnum.fixed);
expect(steps).toEqual([0, 0.1, 1, 10, 100]);
});

it('returns dynamic gradient steps based on e-values', () => {
const steps = getGradientSteps(1e-5, 1, 1e-5, ColorSchemeEnum.dynamic);
expect(steps).toHaveLength(5);
expect(steps[0]).toBeCloseTo(1e-5);
expect(steps[4]).toBeCloseTo(1);
});

it('returns specific gradient steps for ColorSchemeEnum.ncbiblast', () => {
const steps = getGradientSteps(0, 200, 1, ColorSchemeEnum.ncbiblast);
expect(steps).toEqual([0, 40, 50, 80, 200]);
});
});

describe('HSVtoRGB', () => {
it('converts HSV to RGB correctly for various values', () => {
expect(HSVtoRGB(0, 1, 1)).toEqual([255, 0, 0]); // Red
expect(HSVtoRGB(0.3333, 1, 1)).toEqual([0, 255, 0]); // Green
expect(HSVtoRGB(0.6666, 1, 1)).toEqual([0, 0, 255]); // Blue
});

it('handles black and white correctly', () => {
expect(HSVtoRGB(0, 0, 0)).toEqual([0, 0, 0]); // Black
expect(HSVtoRGB(0, 0, 1)).toEqual([255, 255, 255]); // White
});
});

describe('colorByDatabaseName', () => {
it('returns correct color for known domain names', () => {
expect(colorByDatabaseName('Pfam')).toBe('rgb(211,47,47)');
expect(colorByDatabaseName('SMART')).toBe('rgb(106,27,154)');
expect(colorByDatabaseName('PROSITE profiles')).toBe('rgb(0,150,136)');
});

it('returns default color for unknown domain names', () => {
expect(colorByDatabaseName('UnknownDomain')).toBe('rgb(128,128,128)');
});
});
114 changes: 114 additions & 0 deletions tests/custom-types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {
ColorSchemeEnum,
RenderOptions,
JobIdValidable,
jobIdDefaults,
TextType,
RectType,
ObjectType,
ColorType,
CoordsValues,
objectDefaults,
textDefaults,
rectDefaults,
lineDefaults,
} from '../src/custom-types';

// Test suite for ColorSchemeEnum
describe('ColorSchemeEnum', () => {
test('should have expected values', () => {
expect(ColorSchemeEnum.fixed).toBe('fixed');
expect(ColorSchemeEnum.dynamic).toBe('dynamic');
expect(ColorSchemeEnum.ncbiblast).toBe('ncbiblast');
expect(ColorSchemeEnum.blasterjs).toBe('blasterjs');
});
});

// Test suite for RenderOptions
describe('RenderOptions Interface', () => {
const options: RenderOptions = {
canvasWidth: 800,
canvasHeight: 600,
colorScheme: ColorSchemeEnum.fixed,
};

test('should accept valid values for RenderOptions properties', () => {
expect(typeof options.canvasWidth).toBe('number');
expect(typeof options.canvasHeight).toBe('number');
expect(options.colorScheme).toBe(ColorSchemeEnum.fixed);
});

test('should allow undefined properties in RenderOptions', () => {
const partialOptions: RenderOptions = { jobId: 'job123' };
expect(partialOptions.jobId).toBe('job123');
expect(partialOptions.canvasWidth).toBeUndefined();
});
});

// Test suite for JobIdValidable and jobIdDefaults
describe('JobIdValidable', () => {
test('should match jobIdDefaults values', () => {
expect(jobIdDefaults.value).toBe('');
expect(jobIdDefaults.required).toBe(true);
expect(jobIdDefaults.minLength).toBe(35);
expect(jobIdDefaults.maxLength).toBe(60);
expect(jobIdDefaults.pattern).toBeInstanceOf(RegExp);
});

test('should validate jobId pattern', () => {
const validJobId = 'abc-DEF-123-456-789-p2m';
const invalidJobId = 'abc-123';

expect(jobIdDefaults.pattern?.test(validJobId)).toBe(true);
expect(jobIdDefaults.pattern?.test(invalidJobId)).toBe(false);
});
});

// Test suite for TextType, RectType, and ObjectType
describe('TextType, RectType, and ObjectType interfaces', () => {
test('should apply default values correctly', () => {
expect(objectDefaults.selectable).toBe(false);
expect(objectDefaults.evented).toBe(false);
expect(objectDefaults.objectCaching).toBe(false);

expect(textDefaults.selectable).toBe(objectDefaults.selectable);
expect(rectDefaults.objectCaching).toBe(objectDefaults.objectCaching);
});
});

// Test suite for ColorType
describe('ColorType Interface', () => {
const colorType: ColorType = {
keys: [1, 2, 3],
1: [255, 0, 0],
2: [0, 255, 0],
3: [0, 0, 255],
};

test('should have keys as array of numbers', () => {
expect(Array.isArray(colorType.keys)).toBe(true);
expect(colorType.keys).toContain(1);
});

test('should have color mappings as [number, number, number]', () => {
expect(colorType[1]).toEqual([255, 0, 0]);
expect(colorType[2]).toEqual([0, 255, 0]);
});
});

// Test suite for CoordsValues interface
describe('CoordsValues Interface', () => {
const coords: CoordsValues = {
queryLen: 100,
subjLen: 200,
start: 10,
end: 90,
};

test('should accept coordinate properties', () => {
expect(coords.queryLen).toBe(100);
expect(coords.subjLen).toBe(200);
expect(coords.start).toBe(10);
expect(coords.end).toBe(90);
});
});
Loading

0 comments on commit db2c1c9

Please sign in to comment.