Added tests

This commit is contained in:
Hein
2025-10-15 10:36:18 +02:00
parent 9c11b609dc
commit f058336597
10 changed files with 913 additions and 8 deletions

View File

@@ -0,0 +1,91 @@
import { MantineProvider } from '@mantine/core'
import { render, screen } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
import { Gridler } from './Gridler'
// Mock the complex sub-components
vi.mock('./GridlerDataGrid', () => ({
GridlerDataGrid: () => <div data-testid="gridler-data-grid">Data Grid</div>
}))
vi.mock('../MantineBetterMenu', () => ({
MantineBetterMenusProvider: ({ children }: { children: React.ReactNode }) => (
<div data-testid="menu-provider">{children}</div>
)
}))
// Mock the Store Provider
vi.mock('./components/Store', () => ({
Provider: ({ children, uniqueid }: { children: React.ReactNode; uniqueid: string }) => (
<div data-testid={`store-provider-${uniqueid}`}>{children}</div>
)
}))
const TestWrapper = ({ children }: { children: React.ReactNode }) => (
<MantineProvider>
{children}
</MantineProvider>
)
describe('Gridler', () => {
const defaultProps = {
cols: [
{ key: 'id', title: 'ID' },
{ key: 'name', title: 'Name' }
],
uniqueid: 'test-grid'
}
it('renders without crashing', () => {
render(
<TestWrapper>
<Gridler {...defaultProps} />
</TestWrapper>
)
expect(screen.getByTestId('gridler-data-grid')).toBeInTheDocument()
})
it('wraps content with MantineBetterMenusProvider', () => {
render(
<TestWrapper>
<Gridler {...defaultProps} />
</TestWrapper>
)
expect(screen.getByTestId('menu-provider')).toBeInTheDocument()
})
it('creates store provider with unique ID', () => {
render(
<TestWrapper>
<Gridler {...defaultProps} />
</TestWrapper>
)
expect(screen.getByTestId('store-provider-test-grid')).toBeInTheDocument()
})
it('renders children when provided', () => {
render(
<TestWrapper>
<Gridler {...defaultProps}>
<div data-testid="test-child">Custom Child</div>
</Gridler>
</TestWrapper>
)
expect(screen.getByTestId('test-child')).toBeInTheDocument()
})
it('handles different uniqueid prop', () => {
render(
<TestWrapper>
<Gridler {...defaultProps} uniqueid="different-grid" />
</TestWrapper>
)
expect(screen.getByTestId('store-provider-different-grid')).toBeInTheDocument()
})
})

View File

@@ -0,0 +1,51 @@
import { describe, expect, it } from 'vitest'
import { range } from './range'
describe('range utility', () => {
describe('single argument (end only)', () => {
it('creates range from 0 to n-1', () => {
expect(range(5)).toEqual([0, 1, 2, 3, 4])
})
it('handles zero', () => {
expect(range(0)).toEqual([])
})
it('handles one', () => {
expect(range(1)).toEqual([0])
})
it('handles negative numbers', () => {
expect(range(-5)).toEqual([])
})
})
describe('two arguments (start, end)', () => {
it('creates range from start to end-1', () => {
expect(range(2, 7)).toEqual([2, 3, 4, 5, 6])
})
it('handles start equal to end', () => {
expect(range(5, 5)).toEqual([])
})
it('handles start greater than end', () => {
expect(range(7, 2)).toEqual([])
})
})
describe('three arguments (start, end, step)', () => {
it('creates range with positive step', () => {
expect(range(0, 10, 2)).toEqual([0, 2, 4, 6, 8])
})
it('creates range with negative step', () => {
expect(range(10, 0, -2)).toEqual([10, 8, 6, 4, 2])
})
it('throws error for zero step', () => {
expect(() => range(0, 10, 0)).toThrow('Step cannot be zero')
})
})
})