LWC JEST to select options in lightning-radio-group

 Lightning radio group tag in LWC JEST Test

Select elements in lightning web components are similar to all the elements, but for lightning-radio-group is slightly different.  To select the Options in lightning-radio-group tag, we have to fire an event with an object of that option. 
[event evt = new CustomEvent('change', {detail: {value: 'option2'}});
dispatchEvent(evt);]
lightning-radio-group-jest
Let see how we can select option in JEST to test the LWC component with an example.

exLightningRadioGroupJest.html
[<template>
    <lightning-card title="Example Radio Group JEST" icon-name="standard:account">
       <div class="slds-m-around_medium">
            <lightning-radio-group name="radioGroup"
                                   label="Select a Fruit"
                                   options={options}
                                   value={value}
                                   type="radio"
                                   onchange={handleClick}>
            </lightning-radio-group>
            
            <p>Selected Value Is: {selectedValue}</p>
       </div>
    </lightning-card>
 </template>]

exLightningRadioGroupJest.js
[import { LightningElement } from 'lwc';

export default class ExLightningRadioGroupJest extends LightningElement {
    value = '';
    selectedValue; 
    
    // Options list to show on page
    get options() {
        return [
            { label: 'Apple', value: 'Option1' },
            { label: 'Banana', value: 'Option2' },
            { label: 'Cherry', value: 'Option3' },
            { label: 'Date', value: 'Option4' },
            { label: 'Elderberry', value: 'Option5' },
        ];
    }

    // On selection get the selected value
    handleClick(event){
      this.selectedValue = event.detail.value;
    }
}]

exLightningRadioGroupJest.js-meta.xml
[<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>49.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
    </targets>
</LightningComponentBundle>]
exLightningRadioGroupJest.test.js
[import { createElement } from 'lwc';
import exLightningRadioGroupJest from 'c/exLightningRadioGroupJest';

describe('ex-Lightning-Radio-Group-Jest', () => {
    afterEach(() => {
        // The jsdom instance is shared across test cases in a single file so reset the DOM
        while (document.body.firstChild) {
            document.body.removeChild(document.body.firstChild);
        }
    });

    it('Display selected option on radio group', () => {
        // Create element
        const element = createElement('c-ex-Lightning-Radio-Group-Jest', {
            is: exLightningRadioGroupJest
        });
        document.body.appendChild(element);

        // enter sample text in inputbox
        let radioEl = element.shadowRoot.querySelector('lightning-radio-group');
        radioEl.dispatchEvent(new CustomEvent('change', {detail: {value: 'option2'}}));

        return Promise.resolve()
        .then(() => {
            // Verify displayed value
            const div = element.shadowRoot.querySelector('p');
            expect(div.textContent).toBe('Selected Value Is: option2');
        });
    });
});]
also find above code in SWDC World git repo.

COMMENTS

Name

Android Apps,1,Apex,7,Apex Methods,1,Batch Apex,2,Chrome Add-on,1,Chrome browser,1,Coding Best Practices,2,CRM Basics,1,cURL,1,Database Query,1,Dataset,1,Dynamic Apex,3,Dynamic SOQL,1,Einstein Analytics,9,Future Apex,1,Git,1,Google Chrome,1,IDs and Urls Hacks,1,JS for LWC,5,Lightning Components,2,lightning Page,1,Lightning Web Components,14,Linux for Windows users,1,LWC,16,LWC-JEST,7,Queueable Apex,1,Redirect page,1,REGEX,1,REST API,1,Salesforce CRM,1,Salesforce Errors,1,Salesforce Handy Commands,1,Salesforce Interview Questions,14,Salesforce Lightning,1,Salesforce New Features,1,Salesforce Tasks,2,Schedule Apex,1,SLDS,4,SVG Image,3,Test Class,1,Triggers,1,uiObjectInfoApi,1,Visualforce Pages,3,Visualforce Pagination,2,VS Code,2,Wave Analytics,7,Winter Release Notes,1,workbench,1,
ltr
item
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog: LWC JEST to select options in lightning-radio-group
LWC JEST to select options in lightning-radio-group
LWC JEST to select options in lightning-radio-group, Lightning radio group tag in LWC JEST Test, Jest testing for lightning web component radio button
https://1.bp.blogspot.com/-p1l3fuMl74A/Xy_Ef0Mg9pI/AAAAAAAAAaw/aPIKun1YKJwj4GQfxuTWy7CFAV5rCMuJgCLcBGAsYHQ/w621-h640/lightning-radio-group-jest.png
https://1.bp.blogspot.com/-p1l3fuMl74A/Xy_Ef0Mg9pI/AAAAAAAAAaw/aPIKun1YKJwj4GQfxuTWy7CFAV5rCMuJgCLcBGAsYHQ/s72-w621-c-h640/lightning-radio-group-jest.png
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog
https://www.swdcworld.com/2020/08/lwc-jest-to-select-options-in-lightning.html
https://www.swdcworld.com/
https://www.swdcworld.com/
https://www.swdcworld.com/2020/08/lwc-jest-to-select-options-in-lightning.html
true
5370056087523941001
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy