Skip to content

Commit

Permalink
chore: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pnodet committed Aug 22, 2024
1 parent 46185cb commit 6e9e635
Show file tree
Hide file tree
Showing 17 changed files with 649 additions and 18 deletions.
50 changes: 50 additions & 0 deletions src/arrays/ensure-is-array.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, expect, test } from 'bun:test';
import { ensureIsArray } from './ensure-is-array';

describe('ensureIsArray', () => {
test('should wrap a non-array value in an array', () => {
const result = ensureIsArray(1);

expect(result).toEqual([1]);
});

test('should return the same array if the input is already an array', () => {
const array = [1, 2, 3];
const result = ensureIsArray(array);

expect(result).toBe(array);
});

test('should wrap a string in an array if it is not an array', () => {
const result = ensureIsArray('hello');

expect(result).toEqual(['hello']);
});

test('should return the same array if the input is an array of strings', () => {
const array = ['a', 'b', 'c'];
const result = ensureIsArray(array);

expect(result).toBe(array);
});

test('should wrap an object in an array if it is not an array', () => {
const obj = { key: 'value' };
const result = ensureIsArray(obj);

expect(result).toEqual([obj]);
});

test('should return the same array if the input is an array of objects', () => {
const array = [{ key: 'value' }, { key: 'anotherValue' }];
const result = ensureIsArray(array);

expect(result).toBe(array);
});

test('should return an empty array when an empty array is passed', () => {
const result = ensureIsArray([]);

expect(result).toEqual([]);
});
});
82 changes: 82 additions & 0 deletions src/arrays/group.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { describe, expect, test } from 'bun:test';
import { group } from './group';

describe('group', () => {
test('should group items by a given key (number keys)', () => {
const items = [
{ id: 1, category: 'fruit' },
{ id: 2, category: 'vegetable' },
{ id: 3, category: 'fruit' },
{ id: 4, category: 'meat' },
];

const result = group(items, item => item.id % 2);

expect(result).toEqual({
0: [
{ id: 2, category: 'vegetable' },
{ id: 4, category: 'meat' },
],
1: [
{ id: 1, category: 'fruit' },
{ id: 3, category: 'fruit' },
],
});
});

test('should group items by a given key (string keys)', () => {
const items = [
{ name: 'apple', type: 'fruit' },
{ name: 'carrot', type: 'vegetable' },
{ name: 'banana', type: 'fruit' },
{ name: 'chicken', type: 'meat' },
];

const result = group(items, item => item.type);

expect(result).toEqual({
fruit: [
{ name: 'apple', type: 'fruit' },
{ name: 'banana', type: 'fruit' },
],
vegetable: [{ name: 'carrot', type: 'vegetable' }],
meat: [{ name: 'chicken', type: 'meat' }],
});
});

test('should handle an empty array', () => {
const items: Array<{ name: string; type: string }> = [];
const result = group(items, item => item.type);

expect(result).toEqual({});
});

test('should handle unique grouping key', () => {
const items = [
{ name: 'apple', id: 1 },
{ name: 'carrot', id: 2 },
{ name: 'banana', id: 3 },
];

const result = group(items, item => item.id);

expect(result).toEqual({
1: [{ name: 'apple', id: 1 }],
2: [{ name: 'carrot', id: 2 }],
3: [{ name: 'banana', id: 3 }],
});
});

test('should handle grouping by a calculated key', () => {
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const result = group(items, item =>
item % 3 === 0 ? 'divisible by 3' : 'other',
);

expect(result).toEqual({
'divisible by 3': [3, 6, 9],
other: [1, 2, 4, 5, 7, 8, 10],
});
});
});
46 changes: 46 additions & 0 deletions src/currency/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, test } from 'bun:test';
import { formatPrice, type Currency } from './index';

describe('formatPrice', () => {
test('should format price with default locale and cents enabled', () => {
const money = { amount: '1234.56', currencyCode: 'EUR' } satisfies Currency;
const result = formatPrice(money);
expect(result).toBe('1 234,56 €');
});

test('should format price with custom locale and cents enabled', () => {
const money = { amount: '1234.56', currencyCode: 'USD' }satisfies Currency;
const result = formatPrice(money, 'en-US');
expect(result).toBe('$1,234.56');
});

test('should format price with quantity multiplier', () => {
const money = { amount: '1234.56', currencyCode: 'USD' }satisfies Currency;
const result = formatPrice(money, 'en-US', 2);
expect(result).toBe('$2,469.12');
});

test('should format price with cents disabled when applicable', () => {
const money = { amount: '1234.00', currencyCode: 'USD' }satisfies Currency;
const result = formatPrice(money, 'en-US', 1, true);
expect(result).toBe('$1,234');
});

test('should still include cents if the amount has fractional part even if disableCents is true', () => {
const money = { amount: '1234.56', currencyCode: 'USD' }satisfies Currency;
const result = formatPrice(money, 'en-US', 1, true);
expect(result).toBe('$1,234.56');
});

test('should format price correctly with a different currency code', () => {
const money = { amount: '1234.56', currencyCode: 'GBP' }satisfies Currency;
const result = formatPrice(money, 'en-GB');
expect(result).toBe('£1,234.56');
});

test('should handle large quantities correctly', () => {
const money = { amount: '1000.00', currencyCode: 'EUR' }satisfies Currency;
const result = formatPrice(money, 'fr', 100);
expect(result).toBe('100 000,00 €');
});
});
4 changes: 2 additions & 2 deletions src/currency/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { CurrencyCode } from './types';

type Money = {
export type Currency = {
amount: string;
currencyCode: CurrencyCode;
};

export const formatPrice = (
{ amount, currencyCode }: Money,
{ amount, currencyCode }: Currency,
locale = 'fr',
quantity = 1,
disableCents = false,
Expand Down
12 changes: 6 additions & 6 deletions src/functions/debounce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('debounce', () => {
debouncedFunc();
expect(func).not.toHaveBeenCalled();

await sleep(ms + 10);
await sleep(ms + 5);
expect(func).toHaveBeenCalledTimes(1);
});

Expand All @@ -23,11 +23,11 @@ describe('debounce', () => {
debouncedFunc();
expect(func).not.toHaveBeenCalled();

await sleep(ms - 10);
await sleep(ms - 5);
debouncedFunc();
expect(func).not.toHaveBeenCalled();

await sleep(ms + 10);
await sleep(ms + 5);
expect(func).toHaveBeenCalledTimes(1);
});

Expand All @@ -37,7 +37,7 @@ describe('debounce', () => {

debouncedFunc();
debouncedFunc.cancel();
await sleep(ms + 10);
await sleep(ms + 5);
expect(func).not.toHaveBeenCalled();
});

Expand All @@ -48,7 +48,7 @@ describe('debounce', () => {

debouncedFunc();
controller.abort();
await sleep(ms + 10);
await sleep(ms + 5);
expect(func).not.toHaveBeenCalled();
});

Expand All @@ -60,7 +60,7 @@ describe('debounce', () => {
const debouncedFunc = debounce(func, ms, { signal: controller.signal });

debouncedFunc();
await sleep(ms + 10);
await sleep(ms + 5);
expect(func).not.toHaveBeenCalled();
});
});
4 changes: 2 additions & 2 deletions src/functions/memo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ describe('memo', () => {

test('should compute and cache new result if cache is expired', async () => {
const func = mock((x: number) => x * 3);
const ttl = 25; // 100ms TTL
const ttl = 5;
const memoizedFunc = memo(func, { ttl });

const firstCall = memoizedFunc(3);

expect(firstCall).toBe(9);

await sleep(ttl + 10); // Wait for the cache to expire
await sleep(ttl + 5); // Wait for the cache to expire

const secondCall = memoizedFunc(3);

Expand Down
4 changes: 2 additions & 2 deletions src/future/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'bun:test';
import { Future } from './index.js';
import { Future } from './index';

const ms = 25;
const ms = 1;

describe('Future', () => {
test('should resolve with the correct value', async () => {
Expand Down
1 change: 0 additions & 1 deletion src/math/round.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ describe('round function', () => {
const value = 1.2345;
const precision = 3.1;

console.log(value, precision, round(value, precision));
expect(round(value, precision)).toBe(1.234_386_076_761_533_1);
});
});
Loading

0 comments on commit 6e9e635

Please sign in to comment.