-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
userdashboard/AiReportExplanation: add switch component to display/hi…
…de ai explanation on frontend with test
- Loading branch information
Showing
8 changed files
with
128 additions
and
22 deletions.
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
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,29 @@ | ||
import React, { useState } from 'react' | ||
|
||
export const SwitchButton = (props) => { | ||
const [isChecked, setIsChecked] = useState(false) | ||
|
||
const toggleIsChecked = () => { | ||
setIsChecked(!isChecked) | ||
} | ||
|
||
return ( | ||
<button | ||
id={props.id} | ||
className="btn-switch" | ||
aria-labelledby={'switch-id-' + props.id} | ||
role="switch" | ||
aria-checked={isChecked} | ||
onClick={toggleIsChecked} | ||
> | ||
<span className="btn-switch__switch"> | ||
<span className="btn-switch__adjuster" /> | ||
</span> | ||
<span | ||
id={'switch-id-' + props.id} | ||
> | ||
{props.switchLabel} | ||
</span> | ||
</button> | ||
) | ||
} |
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,25 @@ | ||
import React from 'react' | ||
import { render, screen, fireEvent } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import { SwitchButton } from '../SwitchButton' | ||
|
||
test('Show button correctly', () => { | ||
render( | ||
<SwitchButton | ||
switchLabel="This is the label for the switch" | ||
/> | ||
) | ||
expect(screen.getByText('This is the label for the switch')).toBeInTheDocument() | ||
}) | ||
|
||
test('Switch button should change state onclick', () => { | ||
const { getByRole } = render(<SwitchButton />) | ||
const switchButton = getByRole('switch') | ||
|
||
expect(switchButton).toHaveAttribute('aria-checked', 'false') | ||
fireEvent.click(switchButton) | ||
expect(switchButton).toHaveAttribute('aria-checked', 'true') | ||
fireEvent.click(switchButton) | ||
// State should be updated back to false after the second click | ||
expect(switchButton).toHaveAttribute('aria-checked', 'false') | ||
}) |
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
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,4 @@ | ||
### Added | ||
|
||
- general switch button react component | ||
- switch button to AIreportExplanation component |
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