function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ben Allington 7Ben Allington 7 

Writing Test Class for my search lightning component

I have been attempting to write a test class for my search function but its throwing me a little, my first time designing a lightning component so if someone could walk me through it that would I would hugely appreciate it.

ContactList.cmp
<aura:component controller="ContactListController">

    <aura:attribute name="accounts" type="account[]"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:handler event="c:SearchKeyChange" action="{!c.searchKeyChange}"/>

    <ul>
        <aura:iteration items="{!v.accounts}" var="account">
            <li>
                <a href="{! '#/sObject/' + account.Id + '/view'}">
                    <p>{!account.Name}</p>
                    <p>{!account.Phone}</p>
                    <p>{!account.BillingStreet}</p>
                </a>
            </li>
        </aura:iteration>
    </ul>
    </aura:component>



ControllerListController.js
({
        doInit : function(component, event) {
            var action = component.get("c.findAll");
            action.setCallback(this, function(a) {
                component.set("v.accounts", a.getReturnValue());
            });
            $A.enqueueAction(action);
        },
    
        
        searchKeyChange: function(component, event) {
        var searchKey = event.getParam("searchKey");
        var action = component.get("c.findByLocation");
        action.setParams({
          "searchKey": searchKey
        });
        action.setCallback(this, function(a) {
            component.set("v.accounts", a.getReturnValue());
        });
        $A.enqueueAction(action);
    }
        
    })



ContactListController.apxc
public with sharing class ContactListController {
    
        @AuraEnabled
        public static List<Account> findAll() {
            return [SELECT id, name, BillingStreet, phone FROM Account LIMIT 50];
        }
    
        @AuraEnabled
        public static List<Account> findByLocation(String searchKey) {
            String name = '%' + searchKey + '%';
            return [SELECT id, name, phone, BillingStreet FROM Account WHERE BillingStreet LIKE :name LIMIT 50];
        }
    
        
    
    }



SearchBar.cmp
<aura:component >
        <div>
            <input type="text" class="form-control"
                   placeholder="Search" onkeyup="{!c.searchKeyChange}"/>
        </div>
    </aura:component>



SearchBarController.js

   
({
        searchKeyChange : function(component, event, helper) {
        
        var myEvent = $A.get("e.c:SearchKeyChange");
        myEvent.setParams({"searchKey":event.target.value});
        myEvent.fire();
    
    }
    })

 
prasanth kumarprasanth kumar
here is the test class.   I got 100% code coverage. 
@istest
 private class ContactListControllerTest
 {
   @istest private static void testMethod1()
   {
     //SELECT id, name, BillingStreet, phone FROM Account LIMIT 50];
     
     account a1 = new account();
     a1.name='hello raja';
     a1.BillingStreet = 'tesing raja';
     a1.phone='1234534';
     insert a1;
   
   ContactListController.findAll();
   

   // 2nd method test coverage.
   
  
   
     
   
ContactListController.findByLocation('hello raja');
   
   }
   
   
   
   }

 
prasanth kumarprasanth kumar
hi, 

   I am trying to save the code then it is showing this error message.  how u done this ?   Failed to save undefined: No EVENT named markup://c:searchKeyChange1 found : [markup://c:ContactList]: Source