Skip to content

Commit 0c15854

Browse files
refactor: improve type safety in test files - Add QuestionNode interface, replace any types, fix formatting
1 parent a553ad8 commit 0c15854

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

src/components/__tests__/QuestionNode.utils.test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import { describe, it, expect } from 'vitest';
2+
23
import { questionTree } from '../../questions/questions';
34

5+
interface QuestionNode {
6+
id: string;
7+
link?: string | null;
8+
options?: QuestionNode[];
9+
}
10+
411
// Import the utility functions from QuestionNode
512
// Since they're not exported, we'll recreate them here for testing
613
const findNodeById = (
7-
node: any,
14+
node: QuestionNode,
815
id: string
9-
): { currentNode: any; parentNode: any | null; optionIndex: number } | null => {
16+
): { currentNode: QuestionNode; parentNode: QuestionNode | null; optionIndex: number } | null => {
1017
if (node.options) {
11-
const directOptionIndex = node.options.findIndex((option: any) => option.id === id);
18+
const directOptionIndex = node.options.findIndex(option => option.id === id);
1219
if (directOptionIndex !== -1) {
1320
const targetNode = node.options[directOptionIndex];
1421
if (targetNode.options) {
@@ -91,7 +98,7 @@ describe('QuestionNode Utilities', () => {
9198
it('returns random items with roughly even distribution', () => {
9299
const array = ['a', 'b', 'c'];
93100
const counts: Record<string, number> = { a: 0, b: 0, c: 0 };
94-
101+
95102
// Run 1000 iterations to test distribution
96103
for (let i = 0; i < 1000; i++) {
97104
const result = getRandomItem(array);
@@ -106,4 +113,4 @@ describe('QuestionNode Utilities', () => {
106113
});
107114
});
108115
});
109-
});
116+
});

src/i18n/__tests__/LanguageContext.test.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { render, screen } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
3-
import { describe, it, expect, vi, beforeEach } from 'vitest';
3+
import { describe, it, expect, beforeEach } from 'vitest';
4+
45
import { useLanguage, setLanguage, languageStore } from '../LanguageContext';
56

7+
import type { LanguageCode } from '../types';
8+
69
// Test component that uses the language hook
710
const TestComponent = () => {
811
const { language, translations } = useLanguage();
@@ -58,6 +61,6 @@ describe('LanguageContext', () => {
5861
});
5962

6063
it('throws error for unsupported language', () => {
61-
expect(() => setLanguage('fr' as any)).toThrow('Unsupported language: fr');
64+
expect(() => setLanguage('fr' as LanguageCode)).toThrow('Unsupported language: fr');
6265
});
63-
});
66+
});

src/questions/__tests__/questions.test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { describe, it, expect } from 'vitest';
2+
23
import { questionTree } from '../questions';
34

5+
interface QuestionNode {
6+
id: string;
7+
link?: string | null;
8+
options?: QuestionNode[];
9+
}
10+
411
describe('Question Tree', () => {
512
it('has a root node with correct structure', () => {
613
expect(questionTree).toHaveProperty('id', 'root');
@@ -17,12 +24,12 @@ describe('Question Tree', () => {
1724
'documentation',
1825
'community',
1926
'testing',
20-
'release'
27+
'release',
2128
]);
2229
});
2330

2431
it('has valid links for leaf nodes', () => {
25-
const validateLinks = (options: any[]) => {
32+
const validateLinks = (options: QuestionNode[]) => {
2633
options.forEach(option => {
2734
if (option.link) {
2835
expect(option.link).toMatch(/^https?:\/\//); // Should be a valid URL
@@ -59,7 +66,7 @@ describe('Question Tree', () => {
5966

6067
it('has no duplicate IDs across the tree', () => {
6168
const ids = new Set<string>();
62-
const checkForDuplicates = (options: any[]) => {
69+
const checkForDuplicates = (options: QuestionNode[]) => {
6370
options.forEach(option => {
6471
expect(ids.has(option.id)).toBe(false);
6572
ids.add(option.id);
@@ -75,10 +82,10 @@ describe('Question Tree', () => {
7582
it('has consistent link structure for each category', () => {
7683
questionTree.options.forEach(category => {
7784
expect(category.link).toBeNull();
78-
category.options.forEach((subOption: any) => {
85+
category.options?.forEach(subOption => {
7986
expect(subOption.link).toBeDefined();
8087
expect(typeof subOption.link).toBe('string');
8188
});
8289
});
8390
});
84-
});
91+
});

0 commit comments

Comments
 (0)