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
LudivineLudivine 

Help to write a Test Class for Aura Component

Hello,

I have created a lightning component in sandbox to display the lock/ Unlock icon on my record Page.
This needs to create the following Apex method:
public class UnlockRecordUtil {
  @AuraEnabled
    public static Boolean islocked(String recordId){
       return Approval.isLocked(Id.ValueOf(recordId));
     }
    @AuraEnabled
    public static boolean unlockRecord(String objId){
        List<String> unlockrec = new List<string>();
        unlockrec.add(objId);
        Approval.UnlockResult[] unlockedRersult = Approval.unlock(unlockrec);
        return true;
    }
    @AuraEnabled
    public static Boolean LockRecords(String objId){
        List<Account> sobjectre = [Select Id FROM Account WHERE ID =:objId];
        Approval.LockResult[] lrList = Approval.lock(sobjectre, false);
    return true;
    }
}
However, I don't know how to do the test class for this ligthning component.
I have tried to fire approval from my test class but it is not testing these aura method...
Mant thanks in advance.
Best Regards.
Best Answer chosen by Ludivine
$hwet@$hwet@
Hi Ludivine,

I was able to get 81% coverage. not able to cover the return statements.Please find the code below:

@isTest
private class UnlockRecordUtiltest{
     static testmethod void islockedtest(){
     Account acc1 =  new Account(name='test');
     insert acc1;
         UnlockRecordUtil.islocked(string.valueof(acc1.id));
     }
     static testmethod void unlockRecordtest(){
      Account acc2 =  new Account(name='test');
        insert acc2;
         UnlockRecordUtil.unlockRecord(string.valueof(acc2.id));
     }
      static testmethod void LockRecordstest(){
          Account acc = new Account(name='test Account');
         UnlockRecordUtil.LockRecords(string.valueof(acc.id));
     }
}

All Answers

$hwet@$hwet@
Hi Ludivine,

I was able to get 81% coverage. not able to cover the return statements.Please find the code below:

@isTest
private class UnlockRecordUtiltest{
     static testmethod void islockedtest(){
     Account acc1 =  new Account(name='test');
     insert acc1;
         UnlockRecordUtil.islocked(string.valueof(acc1.id));
     }
     static testmethod void unlockRecordtest(){
      Account acc2 =  new Account(name='test');
        insert acc2;
         UnlockRecordUtil.unlockRecord(string.valueof(acc2.id));
     }
      static testmethod void LockRecordstest(){
          Account acc = new Account(name='test Account');
         UnlockRecordUtil.LockRecords(string.valueof(acc.id));
     }
}
This was selected as the best answer
LudivineLudivine

$hwet@,Many thanks, I managed to cover 100% of my Apex Class! Awesome!