✅ Add vitest and initial tests
This commit is contained in:
@@ -11,7 +11,7 @@ import type {
|
||||
} from '../../../pages/api/modules/usenet/history';
|
||||
import { UsenetInfoRequestParams, UsenetInfoResponse } from '../../../pages/api/modules/usenet';
|
||||
import { UsenetPauseRequestParams } from '../../../pages/api/modules/usenet/pause';
|
||||
import { queryClient } from '../../../tools/queryClient';
|
||||
import { queryClient } from '../../../tools/server/configurations/tanstack/queryClient.tool';
|
||||
import { UsenetResumeRequestParams } from '../../../pages/api/modules/usenet/resume';
|
||||
|
||||
const POLLING_INTERVAL = 2000;
|
||||
|
||||
@@ -22,12 +22,12 @@ import { CategoryEditModal } from '../components/Dashboard/Wrappers/Category/Cat
|
||||
import { ConfigProvider } from '../config/provider';
|
||||
import { usePackageAttributesStore } from '../tools/client/zustands/usePackageAttributesStore';
|
||||
import { ColorTheme } from '../tools/color';
|
||||
import { queryClient } from '../tools/queryClient';
|
||||
import { queryClient } from '../tools/server/configurations/tanstack/queryClient.tool';
|
||||
import {
|
||||
getServiceSidePackageAttributes,
|
||||
ServerSidePackageAttributesType,
|
||||
} from '../tools/server/getPackageVersion';
|
||||
import { theme } from '../tools/theme';
|
||||
import { theme } from '../tools/server/theme/theme';
|
||||
|
||||
import { useEditModeInformationStore } from '../hooks/useEditModeInformation';
|
||||
import '../styles/global.scss';
|
||||
|
||||
@@ -9,8 +9,8 @@ import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import Parser from 'rss-parser';
|
||||
|
||||
import { getConfig } from '../../../../tools/config/getConfig';
|
||||
import { Stopwatch } from '../../../../tools/shared/stopwatch';
|
||||
import { IRssWidget } from '../../../../widgets/rss/RssWidgetTile';
|
||||
import { Stopwatch } from '../../../../tools/shared/time/stopwatch.tool';
|
||||
|
||||
type CustomItem = {
|
||||
'media:content': string;
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -6,7 +6,7 @@ import { i18n } from 'next-i18next';
|
||||
import { useState } from 'react';
|
||||
import { useConfigContext } from '../../config/provider';
|
||||
import { useColorTheme } from '../../tools/color';
|
||||
import { isToday } from '../../tools/isToday';
|
||||
import { isToday } from '../../tools/shared/time/date.tool';
|
||||
import { defineWidget } from '../helper';
|
||||
import { IWidget } from '../widgets';
|
||||
import { CalendarDay } from './CalendarDay';
|
||||
|
||||
@@ -4,7 +4,7 @@ import axios from 'axios';
|
||||
import { useTranslation } from 'next-i18next';
|
||||
import { useConfigContext } from '../../config/provider';
|
||||
import { bytes } from '../../tools/bytesHelper';
|
||||
import { percentage } from '../../tools/percentage';
|
||||
import { percentage } from '../../tools/shared/math/percentage.tool';
|
||||
import { DashDotInfo } from './DashDotCompactNetwork';
|
||||
|
||||
interface DashDotCompactStorageProps {
|
||||
|
||||
Reference in New Issue
Block a user