I am looking to deploy a Lightning component written by the amazing Jitendra Zaa. Unfortunately, there was no test class included with the component on his blog post. I have no idea where to begin in writing this one.
public class CircularProgressController {
/**
* This class is used to return as JSON Object
**/
class WrapperJSON{
public Integer total {get;set;}
public Integer actual {get;set;}
public Integer val {get;set;}
}
@AuraEnabled
public static String computePercentage(String sObjectName, String recordId, String totalValueFieldName, String actualValueFieldName){
Integer retVal = 0 ;
String query = null;
WrapperJSON retObj = new WrapperJSON();
if(totalValueFieldName != null && totalValueFieldName.trim() != '' && actualValueFieldName != null && actualValueFieldName.trim() != '' ){
query = 'SELECT '+totalValueFieldName+', '+actualValueFieldName+' FROM '+sObjectName+' WHERE Id =: recordId';
}
else if (actualValueFieldName != null && actualValueFieldName.trim() != '' ) {
query = 'SELECT '+actualValueFieldName+' FROM '+sObjectName+' WHERE Id =: recordId';
}
if(query != null){
try{
List<SOBject> lstObj = Database.query(query);
if(lstObj.size() > 0){
Decimal totalVal = 0;
Decimal actualVal = 0;
if(totalValueFieldName != null && totalValueFieldName.trim() != ''){
totalVal = Decimal.valueOf(String.valueOf(lstObj[0].get(totalValueFieldName)));
retObj.total = Integer.valueOf(totalVal) ;
}
actualVal = Decimal.valueOf(String.valueOf(lstObj[0].get(actualValueFieldName)));
//Means only 1 API Name was supplied and field type is percentage
if(totalVal == 0){
retObj.val = Integer.valueOf(actualVal) ;
retObj.actual = Integer.valueOf(actualVal) ;
}else if (actualVal > 0){
retObj.val = Integer.valueOf( ( actualVal / totalVal ) * 100 );
retObj.actual = Integer.valueOf(actualVal) ;
}
}
}catch(Exception e){}
}
return JSON.serialize(retObj) ;
}
}
Where should I begin in testing this controller?
**EDIT: I realized thanks to given advice that I was overthinking most of this. I have put together a basic test class as follows. It's hard to test actual values for 99% of the use cases in this class because many are derived from formulas and are time specific values. Therefore I have opted to test against null. This is what I have so far.
@IsTest
public class circularProgressControllerTest {
private static testMethod void runTest()
{
Account acc = new Account(Name = 'Account Test', Type = 'Customer');
insert acc;
Case a = new Case(Subject = 'Account Tester', Status='New', Origin='Email', AccountId = [select id from Account where id =:acc.Id][0]);
insert a;
a = [select id, Status from Case where id =:a.Id][0];
a.Status = 'Open';
update a;
a = [select id, First_Response_Time_Minutes_hidden__c, SLA_HI__c from Case where id =:a.Id][0];
Set<String> JsonString = CircularProgressController.computePercentage(Case, a.Id, a.SLA_HI__c, a.First_Response_Time_Minutes_hidden__c);
Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(JsonString);
List<Map<String, String>> myMaps = (List<Map<String, String>>) meta.get('results');
Test.startTest();
System.assertNotEquals(null, myMaps.Val);
Test.stopTest();
}
}
It's important that I run the Case update section, as the only use case of this component requires it to calculate SLA times in a 'prettier' fashion. The issue I am running into now is assigning the sObject of Case – 'Variable does not exist: Case'.
Best Answer
Well, the class you posted only has one method, so it looks like the right way to start is to test that single method :)
Of course, you need to understand what it does before testing.
It looks like the method does the following:
sObjectName
type, with a givenrecordId
, retrieving thetotalValueFieldName
and theactualValueFieldName
WrapperJSON
with the result.There seems to be different behaviour when the
totalVal
is zero and when it's non-zero.I would create 2 methods that:
WrapperJSON
resultWrapperJSON
If you're not comfortable with the constructs of creating a test class and test methods, then I would check the Trailhead module recommended by Adrian Larson above.