✅ Add vitest and initial tests
This commit is contained in:
27
src/tools/shared/math/percentage.tool.test.ts
Normal file
27
src/tools/shared/math/percentage.tool.test.ts
Normal 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');
|
||||
});
|
||||
});
|
||||
2
src/tools/shared/math/percentage.tool.ts
Normal file
2
src/tools/shared/math/percentage.tool.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export const percentage = (partialValue: number, totalValue: number) =>
|
||||
((100 * partialValue) / totalValue).toFixed(1);
|
||||
47
src/tools/shared/time/date.tool.test.ts
Normal file
47
src/tools/shared/time/date.tool.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
8
src/tools/shared/time/date.tool.ts
Normal file
8
src/tools/shared/time/date.tool.ts
Normal 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()
|
||||
);
|
||||
};
|
||||
26
src/tools/shared/time/stopwatch.tool.test.ts
Normal file
26
src/tools/shared/time/stopwatch.tool.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user