Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Adrià Vilanova Martínez | 535e731 | 2021-10-17 00:48:12 +0200 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import React from 'react'; |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 6 | import { render, screen, cleanup, fireEvent } from '@testing-library/react'; |
Adrià Vilanova Martínez | 535e731 | 2021-10-17 00:48:12 +0200 | [diff] [blame] | 7 | import { assert } from 'chai'; |
| 8 | import sinon from 'sinon'; |
| 9 | |
| 10 | import { RadioDescription } from './RadioDescription.tsx'; |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 11 | import {IssueWizardPersona} from '../IssueWizardTypes.tsx'; |
Adrià Vilanova Martínez | 535e731 | 2021-10-17 00:48:12 +0200 | [diff] [blame] | 12 | |
| 13 | describe('RadioDescription', () => { |
| 14 | afterEach(cleanup); |
| 15 | |
| 16 | it('renders', () => { |
| 17 | render(<RadioDescription />); |
| 18 | // look for blue radios |
| 19 | const radioOne = screen.getByRole('radio', { name: /Web Developer/i }); |
| 20 | assert.isNotNull(radioOne) |
| 21 | |
| 22 | const radioTwo = screen.getByRole('radio', { name: /End User/i }); |
| 23 | assert.isNotNull(radioTwo) |
| 24 | |
| 25 | const radioThree = screen.getByRole('radio', { name: /Chromium Contributor/i }); |
| 26 | assert.isNotNull(radioThree) |
| 27 | }); |
| 28 | |
| 29 | it('checks selected radio value', () => { |
| 30 | // We're passing in the "Web Developer" value here manually |
| 31 | // to tell our code that that radio button is selected. |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 32 | render(<RadioDescription selectedRadio={IssueWizardPersona.Developer} />); |
Adrià Vilanova Martínez | 535e731 | 2021-10-17 00:48:12 +0200 | [diff] [blame] | 33 | |
| 34 | const checkedRadio = screen.getByRole('radio', { name: /Web Developer/i }); |
| 35 | assert.isTrue(checkedRadio.checked); |
| 36 | |
| 37 | // Extra check to make sure we haven't checked every single radio button. |
| 38 | const uncheckedRadio = screen.getByRole('radio', { name: /End User/i }); |
| 39 | assert.isFalse(uncheckedRadio.checked); |
| 40 | }); |
| 41 | |
| 42 | it('sets radio value when radio button is clicked', () => { |
| 43 | // Using the sinon.js testing library to create a function for testing. |
| 44 | const setValue = sinon.stub(); |
| 45 | |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 46 | render(<RadioDescription onClickRadio={setValue} />); |
Adrià Vilanova Martínez | 535e731 | 2021-10-17 00:48:12 +0200 | [diff] [blame] | 47 | |
| 48 | const radio = screen.getByRole('radio', { name: /Web Developer/i }); |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 49 | fireEvent.click(radio); |
Adrià Vilanova Martínez | 535e731 | 2021-10-17 00:48:12 +0200 | [diff] [blame] | 50 | |
| 51 | // Asserts that "Web Developer" was passed into our "setValue" function. |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 52 | sinon.assert.calledWith(setValue, IssueWizardPersona.Developer); |
Adrià Vilanova Martínez | 535e731 | 2021-10-17 00:48:12 +0200 | [diff] [blame] | 53 | }); |
| 54 | |
| 55 | it('sets radio value when any part of the parent RoleSelection is clicked', () => { |
| 56 | const setValue = sinon.stub(); |
| 57 | |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 58 | render(<RadioDescription onClickRadio={setValue} />); |
Adrià Vilanova Martínez | 535e731 | 2021-10-17 00:48:12 +0200 | [diff] [blame] | 59 | |
| 60 | // Click text in the RoleSelection component |
| 61 | const p = screen.getByText('End User'); |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 62 | fireEvent.click(p); |
Adrià Vilanova Martínez | 535e731 | 2021-10-17 00:48:12 +0200 | [diff] [blame] | 63 | |
| 64 | // Asserts that "End User" was passed into our "setValue" function. |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 65 | sinon.assert.calledWith(setValue, IssueWizardPersona.EndUser); |
Adrià Vilanova Martínez | 535e731 | 2021-10-17 00:48:12 +0200 | [diff] [blame] | 66 | }); |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 67 | }); |