-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): set up react testing
- Loading branch information
1 parent
740f958
commit 9e94a54
Showing
5 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const baseConfig = require('../../jest.config.base'); | ||
|
||
module.exports = { | ||
...baseConfig, | ||
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], | ||
testEnvironment: 'jsdom', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import '@testing-library/jest-dom'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
packages/components/src/components/VirtualizedList/__tests__/VirtualizedList.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import React from 'react'; | ||
|
||
import { render, screen } from '@testing-library/react'; | ||
|
||
import { VirtualizedList } from '../VirtualizedList'; | ||
|
||
describe('VirtualizedList', () => { | ||
describe('type tests', () => { | ||
it('should throw type errors because local mock items lack the "height" property', () => { | ||
type MockItem = { id: number; content: string }; | ||
const mockItems: MockItem[] = Array.from({ length: 100 }, (_, i) => ({ | ||
id: i, | ||
content: `Item ${i}`, | ||
})); | ||
|
||
<VirtualizedList | ||
// @ts-expect-error: correct error here, 'height' property not passed | ||
items={mockItems} | ||
listHeight={400} | ||
listMinHeight={400} | ||
// @ts-expect-error: correct error here, 'height' property not passed | ||
renderItem={(item: MockItem) => ({ id: item.id, content: item.content })} | ||
onScrollEnd={() => {}} | ||
/>; | ||
}); | ||
|
||
it('should render properly with items containing height property', () => { | ||
type MockItemWithHeight = { id: number; content: string; height: number }; | ||
const mockItems: MockItemWithHeight[] = Array.from({ length: 100 }, (_, i) => ({ | ||
id: i, | ||
content: `Item ${i}`, | ||
height: 40, | ||
})); | ||
|
||
<VirtualizedList | ||
items={mockItems} | ||
listHeight={400} | ||
listMinHeight={400} | ||
renderItem={(item: MockItemWithHeight) => ( | ||
<div key={item.id} style={{ height: item.height }}> | ||
{item.content} | ||
</div> | ||
)} | ||
onScrollEnd={() => {}} | ||
/>; | ||
}); | ||
|
||
it('should automatically type the renderItem() parameter according to the passed generic', () => { | ||
type MockItemWithHeight = { id: number; content: string; height: number }; | ||
const mockItems: MockItemWithHeight[] = Array.from({ length: 100 }, (_, i) => ({ | ||
id: i, | ||
content: `Item ${i}`, | ||
height: 40, | ||
})); | ||
|
||
<VirtualizedList | ||
items={mockItems} | ||
listHeight={400} | ||
listMinHeight={400} | ||
renderItem={item => { | ||
const idNumber: number = item.id; | ||
|
||
return ( | ||
<div key={idNumber} style={{ height: item.height }}> | ||
{item.content} | ||
</div> | ||
); | ||
}} | ||
onScrollEnd={() => {}} | ||
/>; | ||
}); | ||
}); | ||
|
||
describe('smoke tests', () => { | ||
it('renders without crashing with minimal props', () => { | ||
const mockItems = Array.from({ length: 3 }, (_, i) => ({ | ||
id: i, | ||
content: `Test Item ${i}`, | ||
height: 40, | ||
})); | ||
|
||
render( | ||
<VirtualizedList | ||
items={mockItems} | ||
listHeight={200} | ||
listMinHeight={200} | ||
renderItem={item => ( | ||
<div key={item.id} data-testid={`item-${item.id}`}> | ||
{item.content} | ||
</div> | ||
)} | ||
onScrollEnd={() => {}} | ||
/>, | ||
); | ||
|
||
// Verify the first item is rendered | ||
expect(screen.getByTestId('item-0')).toBeTruthy(); | ||
expect(screen.getByText('Test Item 0')).toBeTruthy(); | ||
}); | ||
|
||
it('renders empty list without crashing', () => { | ||
const { container } = render( | ||
<VirtualizedList | ||
items={[]} | ||
listHeight={200} | ||
listMinHeight={200} | ||
renderItem={() => null} | ||
onScrollEnd={() => {}} | ||
/>, | ||
); | ||
|
||
expect(container.firstChild).toBeTruthy(); | ||
expect(container.firstChild?.textContent).toBe(''); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters