Skip to content

Commit

Permalink
First draft for adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mthh committed Sep 18, 2024
1 parent 4731217 commit 1d5599f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
68 changes: 68 additions & 0 deletions src/helpers/common.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { expect, test } from 'vitest';
import {
camelToFlat, findSuitableName, getNumberOfDecimals, getUniqueValues,
isFiniteNumber, isNonNull,
} from './common';

test('isFiniteNumber', () => {
expect(isFiniteNumber(1)).toBe(true);
expect(isFiniteNumber(1.12)).toBe(true);
expect(isFiniteNumber('1')).toBe(true);
expect(isFiniteNumber('1.12')).toBe(true);
expect(isFiniteNumber('')).toBe(false);
expect(isFiniteNumber(null)).toBe(false);
expect(isFiniteNumber(undefined)).toBe(false);
expect(isFiniteNumber(true)).toBe(false);
expect(isFiniteNumber(false)).toBe(false);
expect(isFiniteNumber(NaN)).toBe(false);
expect(isFiniteNumber(Infinity)).toBe(false);
expect(isFiniteNumber(-Infinity)).toBe(false);
expect(isFiniteNumber('a')).toBe(false);
// expect(isFiniteNumber([])).toBe(false);
// expect(isFiniteNumber({})).toBe(false);
});

test('isNonNull', () => {
expect(isNonNull(1)).toBe(true);
expect(isNonNull(1.12)).toBe(true);
expect(isNonNull('1')).toBe(true);
expect(isNonNull('1.12')).toBe(true);
expect(isNonNull('')).toBe(false);
expect(isNonNull(null)).toBe(false);
expect(isNonNull(undefined)).toBe(false);
expect(isNonNull(true)).toBe(true);
expect(isNonNull(false)).toBe(true);
expect(isNonNull(NaN)).toBe(true);
expect(isNonNull(Infinity)).toBe(true);
expect(isNonNull(-Infinity)).toBe(true);
});

test('getNumberOfDecimals', () => {
expect(getNumberOfDecimals(1)).toBe(0);
expect(getNumberOfDecimals(1.12)).toBe(2);
expect(getNumberOfDecimals(1.123456)).toBe(6);
expect(getNumberOfDecimals(0.000789)).toBe(6);
});

test('camelToFlat', () => {
expect(camelToFlat('camelCase')).toBe('Camel Case');
expect(camelToFlat('camelCaseTest')).toBe('Camel Case Test');
expect(camelToFlat('CamelCasE')).toBe('Camel Cas E');
expect(camelToFlat('NaturalEarth2')).toBe('Natural Earth 2');
expect(camelToFlat('Kavrayskiy7')).toBe('Kavrayskiy 7');
});

test('getUniqueValues', () => {
expect(getUniqueValues([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
expect(getUniqueValues(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
});

test('findSuitableName', () => {
expect(findSuitableName('name', ['name', 'other name'])).toBe('name (1)');
expect(findSuitableName('name', ['name', 'other name', 'name (1)'])).toBe('name (2)');
expect(findSuitableName('name', ['name', 'other name', 'name (1)', 'name (2)'])).toBe('name (3)');
expect(findSuitableName('name', ['name', 'other name', 'name (1)', 'name (3)'])).toBe('name (2)');
expect(findSuitableName('name', ['name (1)', 'name (2)', 'name (3)'])).toBe('name (4)');
});
4 changes: 2 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ export default defineConfig({
environment: 'jsdom',
globals: true,
transformMode: { web: [/\.[jt]sx?$/] },
setupFiles: ['node_modules/@testing-library/jest-dom/extend-expect.js'],
setupFiles: ['@testing-library/jest-dom'],
// otherwise, solid would be loaded twice:
// deps: { registerNodeLoader: true },
deps: { registerNodeLoader: true },
// if you have few tests, try commenting one
// or both out to improve performance:
threads: false,
Expand Down

0 comments on commit 1d5599f

Please sign in to comment.