-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vadcDataDictionaryTable): Added working test for Header component
- Loading branch information
1 parent
13c2849
commit 9a090ba
Showing
5 changed files
with
148 additions
and
15 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/Analysis/DataDictionary/ColumnHeaders/ColumnHeaders.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,38 @@ | ||
Here is an example of a Jest unit test using TypeScript for the given `ColumnHeaders` component: | ||
|
||
```typescript | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import ColumnHeaders from './ColumnHeaders'; | ||
import Header from './Header'; // Assuming you've exported the Header component for testing | ||
interface ISortConfig { | ||
sortKey: string; | ||
sortDirection: 'asc' | 'desc'; | ||
} | ||
describe('ColumnHeaders', () => { | ||
it('renders ColumnHeaders with initial sorting state', () => { | ||
const handleSort = jest.fn(); | ||
const sortConfig: ISortConfig = {}; | ||
const { getByTestId } = render(<ColumnHeaders handleSort={handleSort} sortConfig={sortConfig} />); | ||
expect(getByTestid('column-headers')).toBeInTheDocument(); | ||
}); | ||
it('calls handleSort when a column is clicked', () => { | ||
const handleSort = jest.fn(); | ||
const sortConfig: ISortConfig = {}; | ||
const { getByRole } = render(<ColumnHeaders handleSort={handleSort} sortConfig={sortConfig} />); | ||
fireEvent.click(getByRole('columnheader', { name: 'Vocabulary ID' })); | ||
expect(handleSort).toHaveBeenCalledWith({ key: 'vocabularyID', direction: 'asc' }); // or 'desc' depending on the initial sorting state | ||
}); | ||
}); | ||
``` | ||
|
||
Make sure you have `@testing-library/react` and its extensions installed for this test to work correctly. Also, ensure that you have exported the `Header` component in a separate file if it is not already included in the test file. This test covers two aspects of the `ColumnHeaders` component: rendering the initial state and handling column clicks to sort data. Adjust as needed for your specific use case. |
103 changes: 103 additions & 0 deletions
103
src/Analysis/DataDictionary/ColumnHeaders/Header.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,103 @@ | ||
import React from 'react'; | ||
import { ISortConfig, IHeaderProps } from './Interfaces/Interfaces'; // adjust the import path if necessary | ||
import Header from './Header'; // adjust the import path if necessary | ||
import { render, fireEvent, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { cleanup } from '@testing-library/react'; | ||
|
||
describe('Header component', () => { | ||
afterEach(cleanup); | ||
const defaultProps: IHeaderProps = { | ||
handleSort: jest.fn(), | ||
headerJSX: <div data-testid='header'>Test Header Text</div>, // adjust the JSX for your headerJSX | ||
headerKey: 'key1', | ||
sortConfig: { key: 'key1', sortKey: '', direction: '' }, // adjust the ISortConfig interface for your use case | ||
sortable: true, | ||
}; | ||
|
||
it('renders the Header component correctly when sortable is true and there is no sort config', () => { | ||
render( | ||
<table> | ||
<thead> | ||
<tr> | ||
<Header {...defaultProps} /> | ||
</tr> | ||
</thead> | ||
</table> | ||
); | ||
expect(screen.getAllByText('Test Header Text').length).toBeGreaterThan(0); | ||
}); | ||
|
||
it(`renders the Header component correctly when sortable is true and | ||
there is a sort config with ascending direction`, () => { | ||
const sortConfig: ISortConfig = { | ||
key: 'key1', | ||
sortKey: 'key1', | ||
direction: 'ascending', | ||
}; // adjust for your use case | ||
const { getByTestId } = render( | ||
<table> | ||
<thead> | ||
<tr> | ||
<Header {...defaultProps} sortConfig={sortConfig} /> | ||
</tr> | ||
</thead> | ||
</table> | ||
); | ||
expect(screen.getAllByText('Test Header Text').length).toBeGreaterThan(0); | ||
const caretUpArrows = screen.getAllByRole('presentation', { | ||
name: /caret-up/i, | ||
}); | ||
|
||
let activeCaretUp = false; | ||
caretUpArrows.forEach((caretUpArrow) => { | ||
if (caretUpArrow['className'].includes('active')) activeCaretUp = true; | ||
}); | ||
expect(activeCaretUp).toBe(true); | ||
}); | ||
|
||
it(`renders the Header component correctly when sortable is true and | ||
there is a sort config with descending direction`, () => { | ||
const sortConfig: ISortConfig = { | ||
key: 'key1', | ||
sortKey: 'key1', | ||
direction: 'descending', | ||
}; // adjust for your use case | ||
const { getByTestId } = render( | ||
<table> | ||
<thead> | ||
<tr> | ||
<Header {...defaultProps} sortConfig={sortConfig} /> | ||
</tr> | ||
</thead> | ||
</table> | ||
); | ||
expect(screen.getAllByText('Test Header Text').length).toBeGreaterThan(0); | ||
const caretDownArrows = screen.getAllByRole('presentation', { | ||
name: /caret-down/i, | ||
}); | ||
|
||
let activeCaretDown = false; | ||
caretDownArrows.forEach((caretDownArrow) => { | ||
if (caretDownArrow['className'].includes('active')) | ||
activeCaretDown = true; | ||
}); | ||
expect(activeCaretDown).toBe(true); | ||
}); | ||
|
||
it('calls handleSort function when header is clicked', () => { | ||
const handleSort = jest.fn(); | ||
render( | ||
<table> | ||
<thead> | ||
<tr> | ||
<Header {...defaultProps} handleSort={handleSort} /> | ||
</tr> | ||
</thead> | ||
</table> | ||
); | ||
fireEvent.click(screen.getAllByText('Test Header Text')[0]); | ||
expect(handleSort).toHaveBeenCalledTimes(1); | ||
expect(handleSort).toHaveBeenCalledWith('key1'); // adjust for your use case | ||
}); | ||
}); |
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
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
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