Skip to content

Commit

Permalink
tests: added additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
biomadeira committed Nov 5, 2024
1 parent f7611e4 commit ee7fb36
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
110 changes: 110 additions & 0 deletions tests/coords-utilities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import {
getTextLegendPaddingFactor,
getTotalPixels,
getPixelCoords,
getQuerySubjPixelCoords,
getDomainPixelCoords,
} from '../src/coords-utilities';

describe('getTextLegendPaddingFactor', () => {
it('should return correct padding factor for different string lengths', () => {
expect(getTextLegendPaddingFactor('A')).toBe(2.5);
expect(getTextLegendPaddingFactor('AB')).toBe(10);
expect(getTextLegendPaddingFactor('ABC')).toBe(15.5);
expect(getTextLegendPaddingFactor('ABCD')).toBe(21);
expect(getTextLegendPaddingFactor('ABCDE')).toBe(29);
expect(getTextLegendPaddingFactor('ABCDEF')).toBe(35);
expect(getTextLegendPaddingFactor('ABCDEFG')).toBe(41);
expect(getTextLegendPaddingFactor('ABCDEFGH')).toBe(47);
expect(getTextLegendPaddingFactor('')).toBe(0);
});
});

describe('getTotalPixels', () => {
test('should calculate total pixels correctly with positive numbers', () => {
const queryLen = 100;
const subjLen = 200;
const varLen = 10;
const contentWidth = 500;
const contentScoringWidth = 50;

const result = getTotalPixels(queryLen, subjLen, varLen, contentWidth, contentScoringWidth);
expect(result).toBeCloseTo(16.5); // Adjust this value based on the expected outcome
});

test('should return 0 when contentWidth and contentScoringWidth are equal', () => {
const queryLen = 100;
const subjLen = 200;
const varLen = 10;
const contentWidth = 50;
const contentScoringWidth = 50;

const result = getTotalPixels(queryLen, subjLen, varLen, contentWidth, contentScoringWidth);
expect(result).toBe(1.5);
});

test('should handle zero lengths gracefully', () => {
const queryLen = 0;
const subjLen = 200;
const varLen = 10;
const contentWidth = 500;
const contentScoringWidth = 50;

const result = getTotalPixels(queryLen, subjLen, varLen, contentWidth, contentScoringWidth);
expect(result).toBeCloseTo(24.75); // Adjust this based on your expected result
});

test('should return Infinity when totalLen is 0', () => {
const queryLen = 0;
const subjLen = 0;
const varLen = 10;
const contentWidth = 500;
const contentScoringWidth = 50;

const result = getTotalPixels(queryLen, subjLen, varLen, contentWidth, contentScoringWidth);
expect(result).toBe(Infinity); // Division by zero case
});

test('should handle very large numbers', () => {
const queryLen = 1e9;
const subjLen = 1e9;
const varLen = 10;
const contentWidth = 1e8;
const contentScoringWidth = 5e7;

const result = getTotalPixels(queryLen, subjLen, varLen, contentWidth, contentScoringWidth);
expect(result).toBeCloseTo(0.475); // Adjust this based on your expected result
});
});

describe('getPixelCoords', () => {
it('should return correct start and end pixels based on content and margin widths', () => {
const [start, end] = getPixelCoords(300, 50, 25);
expect(start).toBe(75);
expect(end).toBe(325);
});
});

describe('getQuerySubjPixelCoords', () => {
it('should calculate pixel coordinates for query and subject', () => {
const [startQuery, endQuery, startSubj, endSubj] = getQuerySubjPixelCoords(100, 200, 150, 300, 50, 50, 10);
expect(startQuery).toBe(60);
expect(endQuery).toBeCloseTo(139.83, 1);
expect(startSubj).toBeCloseTo(209.83, 1);
expect(endSubj).toBeCloseTo(339.66, 1);
});
});

describe('getDomainPixelCoords', () => {
it('should calculate start and end domain pixel coordinates', () => {
const [startDomainPixels, endDomainPixels] = getDomainPixelCoords(100, 500, 1000, 200, 400, 10);
expect(startDomainPixels).toBeCloseTo(190);
expect(endDomainPixels).toBeCloseTo(60);
});

it('should handle edge case where startDomain is equal to endDomain', () => {
const [startDomainPixels, endDomainPixels] = getDomainPixelCoords(100, 500, 1000, 300, 300, 10);
expect(startDomainPixels).toBeCloseTo(230);
expect(endDomainPixels).toBeCloseTo(-20); // based on formula with zero length domain
});
});
49 changes: 49 additions & 0 deletions tests/other-utilities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { numberToString, countDecimals, ObjectCache, BasicCanvasRenderer } from '../src/other-utilities';
import { fabric } from 'fabric';

describe('ObjectCache', () => {
let cache: ObjectCache<number>;

beforeEach(() => {
cache = new ObjectCache<number>();
});

test('should return undefined for non-existent key', () => {
expect(cache.get('nonexistent')).toBeUndefined();
});

test('should store and retrieve a value', () => {
cache.put('key1', 100);
expect(cache.get('key1')).toBe(100);
});

test('should not overwrite existing value for a key', () => {
cache.put('key1', 100);
cache.put('key1', 200);
expect(cache.get('key1')).toBe(100); // Should remain 100, not overwritten
});

test('should delete a value', () => {
cache.put('key1', 100);
cache.delete('key1');
expect(cache.get('key1')).toBeUndefined();
});

test('should handle deleting a non-existent key gracefully', () => {
expect(() => cache.delete('nonexistent')).not.toThrow();
});
});

// Mock the fabric.Canvas and fabric.StaticCanvas classes
jest.mock('fabric', () => ({
Canvas: jest.fn().mockImplementation(() => ({
setWidth: jest.fn(),
setHeight: jest.fn(),
renderAll: jest.fn(),
})),
StaticCanvas: jest.fn().mockImplementation(() => ({
setWidth: jest.fn(),
setHeight: jest.fn(),
renderAll: jest.fn(),
})),
}));

0 comments on commit ee7fb36

Please sign in to comment.