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

@@ -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`,
};
}

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,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,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);
});
});