generated from Arquisoft/wiq_0
-
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.
- Loading branch information
1 parent
dd9f2b9
commit 84bd714
Showing
5 changed files
with
128 additions
and
0 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,90 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import AnswerItem from './AnswerItem'; | ||
import '@testing-library/jest-dom' | ||
|
||
describe('AnswerItem component', () => { | ||
it('renders with correct class based on selected and correct answers', () => { | ||
const correctId = 'correct_id'; | ||
const selectedId = 'selected_id'; | ||
const answer = { id: 'answer_id' }; | ||
const onSelectAnswerMock = jest.fn(); | ||
|
||
// Render AnswerItem with different props combinations | ||
const { container, rerender } = render( | ||
<AnswerItem | ||
answer={answer} | ||
selected={{ correctId, selectedId }} | ||
btnDisabled={false} | ||
onSelectAnswer={onSelectAnswerMock} | ||
> | ||
Answer | ||
</AnswerItem> | ||
); | ||
|
||
// Initial render should not have any class | ||
expect(container.firstChild).toHaveClass('answer-item'); | ||
|
||
// Re-render with correctId to simulate correct answer | ||
rerender( | ||
<AnswerItem | ||
answer={{ id: correctId }} | ||
selected={{ correctId, selectedId }} | ||
btnDisabled={false} | ||
onSelectAnswer={onSelectAnswerMock} | ||
> | ||
Answer | ||
</AnswerItem> | ||
); | ||
expect(container.firstChild).toHaveClass('answer-item success'); | ||
|
||
// Re-render with selectedId to simulate selected answer | ||
rerender( | ||
<AnswerItem | ||
answer={{ id: selectedId }} | ||
selected={{ correctId, selectedId }} | ||
btnDisabled={false} | ||
onSelectAnswer={onSelectAnswerMock} | ||
> | ||
Answer | ||
</AnswerItem> | ||
); | ||
expect(container.firstChild).toHaveClass('answer-item error'); | ||
}); | ||
|
||
it('calls onSelectAnswer prop when answer item is clicked', () => { | ||
const answer = { id: 'answer_id' }; | ||
const onSelectAnswerMock = jest.fn(); | ||
const { getByText } = render( | ||
<AnswerItem | ||
answer={answer} | ||
selected={{ correctId: 'correct_id', selectedId: 'selected_id' }} | ||
btnDisabled={false} | ||
onSelectAnswer={onSelectAnswerMock} | ||
> | ||
Answer | ||
</AnswerItem> | ||
); | ||
const answerItem = getByText('Answer'); | ||
fireEvent.click(answerItem); | ||
expect(onSelectAnswerMock).toHaveBeenCalledWith(answer.id); | ||
}); | ||
|
||
it('does not call onSelectAnswer prop when button is disabled', () => { | ||
const answer = { id: 'answer_id' }; | ||
const onSelectAnswerMock = jest.fn(); | ||
const { getByText } = render( | ||
<AnswerItem | ||
answer={answer} | ||
selected={{ correctId: 'correct_id', selectedId: 'selected_id' }} | ||
btnDisabled={true} | ||
onSelectAnswer={onSelectAnswerMock} | ||
> | ||
Answer | ||
</AnswerItem> | ||
); | ||
const answerItem = getByText('Answer'); | ||
fireEvent.click(answerItem); | ||
expect(onSelectAnswerMock).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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,35 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import AnswerList from './AnswerList'; | ||
import '@testing-library/jest-dom' | ||
|
||
describe('AnswerList component', () => { | ||
it('renders correctly with list of answers', () => { | ||
const answers = [ | ||
{ id: 'answer1', text: 'Answer 1' }, | ||
{ id: 'answer2', text: 'Answer 2' }, | ||
{ id: 'answer3', text: 'Answer 3' }, | ||
{ id: 'answer4', text: 'Answer 4' }, | ||
]; | ||
const onSelectAnswerMock = jest.fn(); | ||
const selected = { correctId: 'answer1', selectedId: 'answer2' }; | ||
const { getAllByRole } = render( | ||
<AnswerList | ||
answers={answers} | ||
onSelectAnswer={onSelectAnswerMock} | ||
selected={selected} | ||
btnDisabled={false} | ||
/> | ||
); | ||
|
||
// Check if correct number of answer items are rendered | ||
const answerItems = getAllByRole('listitem'); | ||
expect(answerItems.length).toBe(answers.length); | ||
|
||
// Check if each answer item is rendered with correct text | ||
answers.forEach((answer, index) => { | ||
expect(answerItems[index]).toHaveTextContent(answer.text); | ||
}); | ||
}); | ||
|
||
}); |
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