Add vitest and initial tests

This commit is contained in:
Manuel
2023-03-17 22:10:00 +01:00
parent 47cb9cd5b6
commit 4a856c6267
17 changed files with 1410 additions and 56 deletions

View File

@@ -0,0 +1,27 @@
import { describe, expect, it } from 'vitest';
import { percentage } from './percentage.tool';
describe('percentage', () => {
it.concurrent('be fixed value', () => {
// arrange
const value = 62;
// act
const fixedPercentage = percentage(value, 100);
// assert
expect(fixedPercentage).toBe('62.0');
});
it.concurrent('be fixed value when decimal places', () => {
// arrange
const value = 42.69696969;
// act
const fixedPercentage = percentage(value, 100);
// assert
expect(fixedPercentage).toBe('42.7');
});
});

View File

@@ -0,0 +1,2 @@
export const percentage = (partialValue: number, totalValue: number) =>
((100 * partialValue) / totalValue).toFixed(1);

View File

@@ -0,0 +1,47 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { isToday } from './date.tool';
describe('isToday', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it.concurrent('should return true if date is today', () => {
// arrange
const date = new Date(2022, 3, 17);
vi.setSystemTime(date);
// act
const today = isToday(date);
// assert
expect(today).toBe(true);
});
it.concurrent("should return true if date is today and time doesn't match", () => {
// arrange
vi.setSystemTime(new Date(2022, 3, 17, 16, 25, 11));
// act
const today = isToday(new Date(2022, 3, 17));
// assert
expect(today).toBe(true);
});
it.concurrent("should be false if date doesn't match", () => {
// arrange
vi.setSystemTime(new Date(2022, 3, 17, 16));
// act
const today = isToday(new Date(2022, 3, 15));
// assert
expect(today).toBe(false);
});
});

View File

@@ -0,0 +1,8 @@
export const isToday = (date: Date) => {
const today = new Date();
return (
today.getDate() === date.getDate() &&
today.getMonth() === date.getMonth() &&
today.getFullYear() === date.getFullYear()
);
};

View File

@@ -0,0 +1,26 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { Stopwatch } from './stopwatch.tool';
describe('stopwatch', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it.concurrent('should be elapsed time between start and current', () => {
// arrange
vi.setSystemTime(new Date(2023, 2, 26, 0, 0, 0));
const stopwatch = new Stopwatch();
// act
vi.setSystemTime(new Date(2023, 2, 26, 0, 0, 2));
const milliseconds = stopwatch.getEllapsedMilliseconds();
// assert
expect(milliseconds).toBe(2000);
});
});