Adrià Vilanova MartÃnez | 535e731 | 2021-10-17 00:48:12 +0200 | [diff] [blame^] | 1 | // Copyright 2021 The Chromium Authors. All rights reserved. |
| 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'; |
| 6 | import { render, screen, cleanup } from '@testing-library/react'; |
| 7 | import userEvent from '@testing-library/user-event' |
| 8 | import { assert } from 'chai'; |
| 9 | import sinon from 'sinon'; |
| 10 | |
| 11 | import { RadioDescription } from './RadioDescription.tsx'; |
| 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. |
| 32 | render(<RadioDescription value={'Web Developer'} />); |
| 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 | |
| 46 | render(<RadioDescription setValue={setValue} />); |
| 47 | |
| 48 | const radio = screen.getByRole('radio', { name: /Web Developer/i }); |
| 49 | userEvent.click(radio); |
| 50 | |
| 51 | // Asserts that "Web Developer" was passed into our "setValue" function. |
| 52 | sinon.assert.calledWith(setValue, 'Web Developer'); |
| 53 | }); |
| 54 | |
| 55 | it('sets radio value when any part of the parent RoleSelection is clicked', () => { |
| 56 | const setValue = sinon.stub(); |
| 57 | |
| 58 | render(<RadioDescription setValue={setValue} />); |
| 59 | |
| 60 | // Click text in the RoleSelection component |
| 61 | const p = screen.getByText('End User'); |
| 62 | userEvent.click(p); |
| 63 | |
| 64 | // Asserts that "End User" was passed into our "setValue" function. |
| 65 | sinon.assert.calledWith(setValue, 'End User'); |
| 66 | }); |
| 67 | }); |