✅ Add vitest and initial tests
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
import Dockerode from 'dockerode';
|
||||
|
||||
import { MatchingImages, ServiceType, tryMatchPort } from './types';
|
||||
|
||||
function tryMatchType(imageName: string): ServiceType {
|
||||
// Try to find imageName inside MatchingImages
|
||||
|
||||
const match = MatchingImages.find(({ image }) => imageName.includes(image));
|
||||
if (match) {
|
||||
return match.type;
|
||||
}
|
||||
return 'Other';
|
||||
}
|
||||
|
||||
export function tryMatchService(container: Dockerode.ContainerInfo | undefined) {
|
||||
if (container === undefined) return {};
|
||||
const name = container.Names[0].substring(1);
|
||||
const type = tryMatchType(container.Image);
|
||||
const port = tryMatchPort(type.toLowerCase())?.value ?? container.Ports[0]?.PublicPort;
|
||||
return {
|
||||
name,
|
||||
id: container.Id,
|
||||
type: tryMatchType(container.Image),
|
||||
url: `localhost${port ? `:${port}` : ''}`,
|
||||
icon: `https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/${name
|
||||
.replace(/\s+/g, '-')
|
||||
.toLowerCase()}.png`,
|
||||
};
|
||||
}
|
||||
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');
|
||||
});
|
||||
});
|
||||
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);
|
||||
});
|
||||
});
|
||||
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