Skip to content

Commit

Permalink
add Profitability getProfitInfo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kenkunz committed Dec 13, 2024
1 parent d7f0bcc commit 2ed1a5e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/lib/components/Profitability.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ using the component isn't practical.
function formatProfitability(value: number | undefined) {
if (!isNumber(value)) return;
const absValue = Math.abs(value);
return absValue.toLocaleString('en-us', {
return value.toLocaleString('en-us', {
minimumFractionDigits: 1,
maximumFractionDigits: absValue < 0.001 ? 2 : 1,
style: 'percent'
maximumFractionDigits: Math.abs(value) < 0.001 ? 2 : 1,
style: 'percent',
signDisplay: 'never'
});
}
Expand Down
114 changes: 114 additions & 0 deletions src/lib/components/Profitability.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { getProfitInfo } from './Profitability.svelte';

describe('getProfitInfo', () => {
test('should handle numeric or string input', () => {
expect(getProfitInfo(0.01).value).toBe(0.01);
expect(getProfitInfo('0.01').value).toBe(0.01);
});

test('should return empty, neutral info for null or undefined', () => {
const expected = expect.objectContaining({
value: undefined,
formatted: undefined,
direction: 0,
marker: '◼︎',
directionClass: 'neutral'
});

expect(getProfitInfo(null)).toEqual(expected);
expect(getProfitInfo(undefined)).toEqual(expected);
});

test('should return 0, neutral info for value of zero', () => {
const expected = expect.objectContaining({
formatted: '0.0%',
direction: 0,
marker: '◼︎',
directionClass: 'neutral'
});

expect(getProfitInfo(0.0)).toEqual(expected);
expect(getProfitInfo(-0.0)).toEqual(expected);
});

test('should return neutral info for values that round to zero', () => {
const expected = expect.objectContaining({
formatted: '0.0%',
direction: 0,
marker: '◼︎',
directionClass: 'neutral'
});

expect(getProfitInfo(0.00001)).toEqual(expected);
expect(getProfitInfo(-0.00001)).toEqual(expected);
});

test('should return positive (profit) info for positive values', () => {
const expected = expect.objectContaining({
direction: 1,
marker: '▲',
directionClass: 'bullish'
});

expect(getProfitInfo(0.001)).toEqual(expected);
expect(getProfitInfo(0.01)).toEqual(expected);
});

test('should return negative (loss) info for negative values', () => {
const expected = expect.objectContaining({
direction: -1,
marker: '▼',
directionClass: 'bearish'
});

expect(getProfitInfo(-0.001)).toEqual(expected);
expect(getProfitInfo(-0.01)).toEqual(expected);
});

describe('formatted value', () => {
test('should have with 1 decimal of precision by default', () => {
expect(getProfitInfo(5).formatted).toBe('500.0%');
expect(getProfitInfo(0.5).formatted).toBe('50.0%');
expect(getProfitInfo(0.05).formatted).toBe('5.0%');
expect(getProfitInfo(0.005).formatted).toBe('0.5%');
});

test('should have 2 decimals of precision when needed', () => {
expect(getProfitInfo(0.0005).formatted).toBe('0.05%');
});

test('should be unsigned for negative values', () => {
expect(getProfitInfo(-0.005).formatted).toBe('0.5%');
expect(getProfitInfo(-0.0005).formatted).toBe('0.05%');
});
});

describe('toSting() method', () => {
test('should return fallback for undefined/null values', () => {
expect(getProfitInfo(undefined).toString()).toBe('---');
expect(getProfitInfo(null).toString()).toBe('---');
});

test('should return marker with formatted value for valid values', () => {
expect(getProfitInfo(0).toString()).toBe('◼︎ 0.0%');
expect(getProfitInfo(0.001).toString()).toBe('▲ 0.1%');
expect(getProfitInfo(-0.001).toString()).toBe('▼ 0.1%');
});
});

describe('getLabel() method', () => {
const labels = ['loss', 'no change', 'profit'];

test('should return first arg for negative values', () => {
expect(getProfitInfo(-0.01).getLabel(...labels)).toBe('loss');
});

test('should return second arg for negative values', () => {
expect(getProfitInfo(0).getLabel(...labels)).toBe('no change');
});

test('should return first arg for negative values', () => {
expect(getProfitInfo(0.01).getLabel(...labels)).toBe('profit');
});
});
});

0 comments on commit 2ed1a5e

Please sign in to comment.