test: add Vitest and some tests

This commit is contained in:
MAZE 2024-06-19 14:12:06 +04:30
parent 74f6b5851d
commit def9a57e0c
6 changed files with 1973 additions and 1102 deletions

2952
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -8,6 +8,7 @@
"build": "astro build",
"preview": "astro preview",
"astro": "astro",
"test": "vitest",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx,.astro",
"lint:fix": "npm run lint -- --fix",
"lint:style": "stylelint ./**/*.{css,astro,html}",
@ -23,14 +24,14 @@
"build-storybook": "storybook build"
},
"dependencies": {
"@astrojs/react": "^3.0.3",
"@astrojs/react": "3.6.0",
"@floating-ui/react": "0.26.0",
"@radix-ui/react-dropdown-menu": "2.0.6",
"@radix-ui/react-tooltip": "1.0.7",
"@types/howler": "2.2.10",
"@types/react": "^18.2.25",
"@types/react-dom": "^18.2.10",
"astro": "4.0.3",
"astro": "4.10.3",
"deepmerge": "4.3.1",
"focus-trap-react": "10.2.3",
"framer-motion": "10.16.4",
@ -89,6 +90,7 @@
"stylelint-config-html": "1.1.0",
"stylelint-config-recess-order": "4.4.0",
"stylelint-config-standard": "34.0.0",
"stylelint-prettier": "4.0.2"
"stylelint-prettier": "4.0.2",
"vitest": "1.6.0"
}
}

View file

@ -6,5 +6,9 @@
* @returns {string} The padded number as a string.
*/
export function padNumber(number: number, maxLength: number = 2): string {
return number.toString().padStart(maxLength, '0');
const isNegative = number < 0;
const absoluteNumber = Math.abs(number).toString();
const paddedNumber = absoluteNumber.padStart(maxLength, '0');
return isNegative ? `-${paddedNumber}` : paddedNumber;
}

View file

@ -0,0 +1,53 @@
import { describe, it, expect } from 'vitest';
import { count } from '../counter';
describe('count function', () => {
it('should count characters and words in an empty string', () => {
const result = count('');
expect(result.characters).toBe(0);
expect(result.words).toBe(0);
});
it('should count characters and words in a string with multiple words', () => {
const result = count('Hello world');
expect(result.characters).toBe(10);
expect(result.words).toBe(2);
});
it('should count characters and words in a string with multiple spaces', () => {
const result = count(' Hello world ');
expect(result.characters).toBe(10);
expect(result.words).toBe(2);
});
it('should count characters and words in a string with newlines', () => {
const result = count('Hello\nworld');
expect(result.characters).toBe(10);
expect(result.words).toBe(2);
});
it('should count characters and words in a string with special characters', () => {
const result = count('Hello, world!');
expect(result.characters).toBe(12);
expect(result.words).toBe(2);
});
it('should count characters and words in a string with only spaces', () => {
const result = count(' ');
expect(result.characters).toBe(0);
expect(result.words).toBe(0);
});
it('should count characters and words in a string with a single word', () => {
const result = count('Vitest');
expect(result.characters).toBe(6);
expect(result.words).toBe(1);
});
it('should count characters and words in a string with multiple lines and spaces', () => {
const result = count(' Hello \n world ');
expect(result.characters).toBe(10);
expect(result.words).toBe(2);
});
});

View file

@ -0,0 +1,49 @@
import { describe, it, expect } from 'vitest';
import { padNumber } from '../number';
describe('padNumber function', () => {
it('should pad a single digit number to two digits by default', () => {
const result = padNumber(5);
expect(result).toBe('05');
});
it('should not pad a number that already has two digits by default', () => {
const result = padNumber(12);
expect(result).toBe('12');
});
it('should pad a number to the specified length', () => {
const result = padNumber(7, 4);
expect(result).toBe('0007');
});
it('should not pad a number that already meets the specified length', () => {
const result = padNumber(1234, 4);
expect(result).toBe('1234');
});
it('should pad a number that has more digits than the specified length', () => {
const result = padNumber(123, 5);
expect(result).toBe('00123');
});
it('should handle zero correctly', () => {
const result = padNumber(0, 3);
expect(result).toBe('000');
});
it('should pad negative numbers correctly', () => {
const result = padNumber(-5, 3);
expect(result).toBe('-005');
});
it('should handle very large padding lengths', () => {
const result = padNumber(42, 10);
expect(result).toBe('0000000042');
});
it('should handle the maximum length being less than the number length', () => {
const result = padNumber(12345, 3);
expect(result).toBe('12345');
});
});

7
vitest.config.ts Normal file
View file

@ -0,0 +1,7 @@
/// <reference types="vitest" />
import { getViteConfig } from 'astro/config';
export default getViteConfig({
test: {},
});