From ee7fb360ee9eaa328a4e7d573d6ff24f0c2b50dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fa=CC=81bio=20Madeira?= Date: Tue, 5 Nov 2024 12:48:37 +0000 Subject: [PATCH] tests: added additional tests --- tests/coords-utilities.test.ts | 110 +++++++++++++++++++++++++++++++++ tests/other-utilities.test.ts | 49 +++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 tests/coords-utilities.test.ts create mode 100644 tests/other-utilities.test.ts diff --git a/tests/coords-utilities.test.ts b/tests/coords-utilities.test.ts new file mode 100644 index 0000000..463f066 --- /dev/null +++ b/tests/coords-utilities.test.ts @@ -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 + }); +}); diff --git a/tests/other-utilities.test.ts b/tests/other-utilities.test.ts new file mode 100644 index 0000000..7d11c1f --- /dev/null +++ b/tests/other-utilities.test.ts @@ -0,0 +1,49 @@ +import { numberToString, countDecimals, ObjectCache, BasicCanvasRenderer } from '../src/other-utilities'; +import { fabric } from 'fabric'; + +describe('ObjectCache', () => { + let cache: ObjectCache; + + beforeEach(() => { + cache = new ObjectCache(); + }); + + 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(), + })), +}));