Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions frontend/src/utils/arrayParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,51 @@ import { describe, it, expect } from 'vitest';
import { parseCustomArrayInput } from './arrayParser';

describe('parseCustomArrayInput', () => {
it('should parse a typical comma-separated input', () => {
expect(parseCustomArrayInput("1, 2, 3")).toEqual([1, 2, 3]);
it('should parse standard comma-separated integers', () => {
expect(parseCustomArrayInput('5, 8, 10')).toEqual([5, 8, 10]);
});

it('should handle trailing commas', () => {
expect(parseCustomArrayInput("5, 8,")).toEqual([5, 8]);
expect(parseCustomArrayInput('5, 8,')).toEqual([5, 8]);
});

it('should handle empty middle commas', () => {
expect(parseCustomArrayInput("5,, 8")).toEqual([5, 8]);
expect(parseCustomArrayInput('5,, 8')).toEqual([5, 8]);
});

it('should handle a single element', () => {
expect(parseCustomArrayInput("5")).toEqual([5]);
it('should parse a single element', () => {
expect(parseCustomArrayInput('5')).toEqual([5]);
});

it('should handle a zero element', () => {
expect(parseCustomArrayInput("0")).toEqual([0]);
it('should parse a zero element', () => {
expect(parseCustomArrayInput('0')).toEqual([0]);
});

it('should handle duplicate elements', () => {
expect(parseCustomArrayInput("5, 5, 5")).toEqual([5, 5, 5]);
expect(parseCustomArrayInput('5, 5, 5')).toEqual([5, 5, 5]);
});

it('should ignore invalid non-numeric entries', () => {
expect(parseCustomArrayInput("5, abc, 8")).toEqual([5, 8]);
expect(parseCustomArrayInput('5, abc, 8')).toEqual([5, 8]);
expect(parseCustomArrayInput('abc, def')).toEqual([]);
expect(parseCustomArrayInput('1, 2.5, 3')).toEqual([1, 3]); // ignores floats based on regex /^-?\d+$/
});

it('should parse negative numbers', () => {
expect(parseCustomArrayInput("-5, 0, 10")).toEqual([-5, 0, 10]);
it('should handle negative numbers', () => {
expect(parseCustomArrayInput('-5, 0, 10')).toEqual([-5, 0, 10]);
});

it('should return an empty array for an empty string or all non-numeric input', () => {
expect(parseCustomArrayInput("")).toEqual([]);
expect(parseCustomArrayInput("abc, def")).toEqual([]);
});

it('should return an empty array for null/undefined/non-string input', () => {
// @ts-expect-error testing invalid input types
it('should return empty array for non-string inputs', () => {
// @ts-expect-error Testing invalid input type
expect(parseCustomArrayInput(null)).toEqual([]);
// @ts-expect-error testing invalid input types
// @ts-expect-error Testing invalid input type
expect(parseCustomArrayInput(undefined)).toEqual([]);
// @ts-expect-error testing invalid input types
// @ts-expect-error Testing invalid input type
expect(parseCustomArrayInput(123)).toEqual([]);
});
});
Loading