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
RagzRagz 

Test method for a custom component

Hi, can someone help me write the  test method for the following custom component. I couldnt find any link to custom component test methods in the force.com knowledgebase/community. Please help.

 

public with sharing class AutoComplete_Controller{

	//Constructors
	
    public AutoComplete_Controller() {}
    
    
    public AutoComplete_Controller(ApexPages.StandardController controller) {}

	//Attributes
	
	private List<String> resultsname = new List<String>();
	private Boolean hasparams = false;
	private Boolean hasnoresults = false;
    public String state = '';
        
    //Getters and Setters
    
 	public Boolean getHasparams(){
 		return hasparams;
 	}
 	
 	public void clearValues(){
 		hasparams = false;
 	}
 	
 	public Boolean getHasnoresults(){
 		return hasnoresults;	
 	}

	public void avoidRefresh(){
	}

 	/*public void setState(String sName) {
        state = sName;
        system.debug('sName  - ' + sName);
    }
            
    public String getState() {
        return state;
    }*/
   
    public PageReference searchSuggestions() {

		//Initalize variables, hasparams just indicates that a search has started
        resultsname.clear();   
        hasparams = true;
        hasnoresults = false;

		//Obtain current parameters
        String sobjectname = System.currentPageReference().getParameters().get('objectname');
        String stext = String.escapeSingleQuotes(System.currentPageReference().getParameters().get('aname').trim())+'%';
        String stateText = String.escapeSingleQuotes(System.currentPageReference().getParameters().get('statename').trim());
        
        system.debug('State Name  - ' + state);
        system.debug('stateText - ' + stateText);
        
        //Limit Suggestions to 10 Results
        Integer iLimit = 10;
        
     //Validate if there's an input and get results
     
     if(stext.length() > 2){

		try{
	        		
			  //String sql = 'select City__C from ' + sobjectname + ' where City__C like \''+stext+'\' and State__c = ' + stateText + '' limit '+ iLimit;
			  String sql = 'select City__C from ' + sobjectname + ' where City__C like \'' + stext + '\' and State__c = \''+ stateText+'\' GROUP BY City__C'; 
			  // limit ' + iLimit;
			  //lstAR = [];
			  system.debug('SQL Formed + ' + sql );
		         	// 
		         	for(sobject x : Database.query(sql)){
		        		
		        		String s  = String.escapeSingleQuotes(((String)(x.get('City__C'))));
		        		resultsname.add(s);	
		        	}
				
				
			
		}catch(Exception e){
			
			resultsname.add('Unexpected Error, please contact support- ');	
		}


     }
       return null;
  }
  
	  
	  
	 public List<String> getResultsname(){
	  	  //Make sure to clear past values
	      clearValues();
	      if(resultsname.isEmpty()){
			hasnoresults = true;
			resultsname.add('No Results');
	      }
	      return resultsname;
	  }
}

 thank you

 

Ray DehlerRay Dehler

I think the reason you're having difficulty finding any links to help you out here is because you're searching for the wrong thing.

 

This is a controller extension, not a custom component.

 

Here's a good place to start:

http://salesforcesource.blogspot.com/2008/09/testing-your-controller-extentions.html

 

RagzRagz

But I am using this controller in the component , and here is the code for the component

 

<apex:component controller="AutoComplete_Controller">

<apex:attribute name="InputId" description="Id of the InputField to which the AutoComplete Componente will belong, use {!$Componente.THEID}" type="String" required="true"/>
<apex:attribute name="AutoCompleteId" description="Unique Id for this AutoComplete Component" type="String" required="true" />
<apex:attribute name="ObjectName" description="This is the lookup Object." type="String" required="true"/>
<apex:attribute name="ClassName" description="This is the name of the CSS Class used to style the AutoComplete Menu." type="String" required="true"/>
<apex:attribute name="Width" description="AutoComplete Width, In case you need to adjust it for certain inputs" type="Integer" required="true"/>
<apex:attribute name="StateName" description="State Name" type="String" required="true" />
        
          <apex:actionFunction name="search{!AutoCompleteId}" action="{!searchSuggestions}" rerender="OP_" immediate="true" >
             <apex:param name="aname" value="" />
             <apex:param name="objectname" value="" />
             <apex:param name="statename" value="" />
           </apex:actionFunction>
 
           <apex:actionFunction name="clear{!AutoCompleteId}" action="{!clearValues}" rerender="OP_" immediate="true"/>
                                                
           <div id="suggestions"><div id="{!AutoCompleteId}" class="{!ClassName}" style="position:absolute;display:none;width:{!Width}px !important;"></div></div>
           
           <script>
           
           new AutoComplete(document.getElementById('{!InputId}'),document.getElementById('{!AutoCompleteId}'),'search{!AutoCompleteId}','clear{!AutoCompleteId}','{!ObjectName}', document.getElementById('{!StateName}'));
           
           </script>                                                                     
                    
           <apex:outputPanel id="OP_{!AutoCompleteId}" layout="block">
                
            <script>
            var container = document.getElementById('{!AutoCompleteId}');
                container.innerHTML = '';
            var myElement;
            var textNode;
            var aIds;   
            </script>  
            
            <apex:actionStatus ><apex:facet name="stop">     
                <apex:repeat value="{!resultsname}" var="a" rendered="{!hasparams}">
                    <script>
                        myElement = document.createElement('div');
                                            
                        if({!hasnoresults}){
                            myElement.id = '{!AutoCompleteId}N_R';
                            myElement.style.background = "#CCCCFF";
                            textNode = document.createTextNode('{!a}');                   
                        }else{          
                            textNode = document.createTextNode('{!a}');
                        }
                        myElement.appendChild(textNode);
                        container.appendChild(myElement);       
                    </script>
                </apex:repeat>    
              </apex:facet></apex:actionStatus>
              
              <script>
              if(document.getElementById('{!InputId}').type == 'hidden'){
                var cont=document.getElementById('{!AutoCompleteId}');cont.innerHTML='';cont.style.display='none';
              }
              </script>     

           </apex:outputPanel> 
            
</apex:component>