📐 Adds test for string byte formatting

This commit is contained in:
Rune Harlyk
2024-02-23 12:16:44 +01:00
parent 60eaf81c3c
commit 84506d7903
2 changed files with 29 additions and 1 deletions
+28
View File
@@ -0,0 +1,28 @@
import { describe, it, expect } from 'vitest';
import { humanFileSize } from '../../src/lib/utilities';
describe('humanFileSize', () => {
it('returns "0B" for 0 bytes', () => {
expect(humanFileSize(0)).toBe('0B');
});
it('returns the size in bytes correctly', () => {
expect(humanFileSize(500)).toBe('500B');
});
it('returns the size in kB correctly', () => {
expect(humanFileSize(1024)).toBe('1kB');
});
it('returns the size in MB correctly', () => {
expect(humanFileSize(1048576)).toBe('1MB'); // 1024 * 1024
});
it('returns the size in GB correctly', () => {
expect(humanFileSize(1073741824)).toBe('1GB'); // 1024 * 1024 * 1024
});
it('rounds to 2 decimal places correctly', () => {
expect(humanFileSize(1536)).toBe('1.5kB'); // 1024 + 512
});
});
+39
View File
@@ -0,0 +1,39 @@
import { describe, it, expect } from 'vitest';
import { Result } from '../../src/lib/utilities';
describe('Result', () => {
it('should create a success result correctly', () => {
const successValue = 'Success value';
const result = Result.ok(successValue);
expect(result.isOk()).toBe(true);
expect(result.isErr()).toBe(false);
expect(result.inner).toBe(successValue);
});
it('should create an error result correctly', () => {
const errorMessage = 'Error message';
const result = Result.err(errorMessage);
expect(result.isOk()).toBe(false);
expect(result.isErr()).toBe(true);
expect(result.inner).toBe(errorMessage);
});
it('should type guard success and error results correctly', () => {
const successResult = Result.ok(123);
const errorResult = Result.err('Error');
if (successResult.isOk()) {
expect(typeof successResult.inner).toBe('number');
} else {
throw new Error('Expected successResult to be ok');
}
if (errorResult.isErr()) {
expect(typeof errorResult.inner).toBe('string');
} else {
throw new Error('Expected errorResult to be fail');
}
});
});