Add unit tests for utils and module pure functions

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-05 19:14:13 +08:00
parent e596bcd18e
commit 83c2db7582
8 changed files with 563 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
import { describe, it, expect } from 'vitest';
import {
sortNumEngZhtw,
sortNumEngZhtwForFilter,
} from '@/module/sortNumEngZhtw.js';
describe('sortNumEngZhtw', () => {
it('sorts numbers in ascending order', () => {
expect(sortNumEngZhtw(['3', '1', '2']))
.toEqual(['1', '2', '3']);
});
it('places numbers before strings', () => {
expect(sortNumEngZhtw(['b', '1', 'a']))
.toEqual(['1', 'a', 'b']);
});
it('sorts English strings alphabetically', () => {
expect(sortNumEngZhtw(['cherry', 'apple', 'banana']))
.toEqual(['apple', 'banana', 'cherry']);
});
it('sorts mixed numbers and strings', () => {
const input = ['banana', '10', 'apple', '2'];
const result = sortNumEngZhtw(input);
expect(result).toEqual(['2', '10', 'apple', 'banana']);
});
});
describe('sortNumEngZhtwForFilter', () => {
it('returns negative when a < b (numbers)', () => {
expect(sortNumEngZhtwForFilter('1', '2'))
.toBeLessThan(0);
});
it('returns positive when a > b (numbers)', () => {
expect(sortNumEngZhtwForFilter('10', '2'))
.toBeGreaterThan(0);
});
it('places numbers before strings', () => {
expect(sortNumEngZhtwForFilter('1', 'a'))
.toBeLessThan(0);
expect(sortNumEngZhtwForFilter('a', '1'))
.toBeGreaterThan(0);
});
it('compares strings using locale', () => {
expect(sortNumEngZhtwForFilter('a', 'b'))
.toBeLessThan(0);
});
});