这是我第一次使用笑话测试,我正在尝试测试一种方法,该方法从具有访问权限(数组)的组的查询中生成闪电药丸。每当从阵列中添加或删除新组时,都会更新闪电药丸。
我需要测试以下场景,但我不确定我是否正确创建了测试:
(这些已经在实际代码中正常运行)。
测试#1:查询返回的数组应该显示为闪电药丸
测试#2:单击药丸中的 X 图标应该从数组中删除选定的药丸
对于 Test#1,jest 测试对于多个或单个元素的模拟数组失败。我不确定问题出在哪里,但调试显示此行未定义:
const detailEls = element.shadowRoot.querySelectorAll('lightning-pill');
我正在使用返回方法 GroupController.getSpecificGroups:
公共共享类 GroupController {
/* Description: Gets the groups where the contact is being shared to
*
*/
@AuraEnabled
public static List<sObject> getSpecificGroups(String recordId){
List<GroupShare> groupShareList = new List<GroupShare>();
List<sObject> returnShareList = new List<sObject>();
try{
groupShareList = [SELECT Id, UserOrGroupId, UserOrGroup.Name, ContactId, Contact.Name,
FROM GroupShare
WHERE ContactId =: recordId];
if(groupShareList != NULL && !groupShareList.isEmpty()){
for(GroupShare csBuff : groupShareList){
returnShareList.add(csBuff);
}
}
}
catch(queryException qExcp){
}
return returnShareList;
}
}
测试方法是否成功将 recordId 传递给 apex 方法调用时通过了 Jest 测试,所以我认为方法调用没有问题。
it('passes the recordId to the Apex method correctly', () => {
const RECORD_ID = '00AAABBBCCCDDD12345';
const APEX_PARAMETERS = { recordId: RECORD_ID };
// Assign mock value for resolved Apex promise
getSpecificGroups.mockResolvedValue(APEX_GROUPS_SUCCESS);
// Create initial element
const element = createElement('c-cmm-specific-group-sharing', {
is: Cmm_specificGroupSharing
});
element.recordId = RECORD_ID;
document.body.appendChild(element);
// Select button for executing Apex call
const buttonEl = element.shadowRoot.querySelector('lightning-button');
buttonEl.click();
return flushPromises().then(() => {
// Validate parameters of mocked Apex call
expect(getSpecificGroups.mock.calls[0][0]).toEqual(APEX_PARAMETERS);
});
});
对于测试#2,去除药丸的开玩笑测试失败。使用 dispatchEvent 不会从数组中删除药丸:
it('handleRemoveSelectedItem works', () => {
// Create element
const element = createElement('c-cmm-specific-group-sharing', {
is: Cmm_specificGroupSharing
});
element.availableGroups = APEX_GROUPS_SUCCESS;
document.body.appendChild(element);
// Remove a selected item
const selPills = element.shadowRoot.querySelectorAll('lightning-pill');
selPills[0].dispatchEvent(new CustomEvent('remove'));
// Check selection
expect(element.availableGroups.length).toBe(0);
});
开玩笑测试
import { createElement } from 'lwc';
import Cmm_specificGroupSharing from 'c/c-cmm-specific-group-sharing';
import getSpecificGroups from '@salesforce/apex/GroupController.getSpecificGroups';
import delete from "@salesforce/apex/GroupController.delete";
// Mocking Apex method call
jest.mock(
'@salesforce/apex/GroupController.getSpecificGroups',
() => {
return {
default: jest.fn()
};
},
{ virtual: true }
);
// Sample data for Apex call
const APEX_GROUPS_SUCCESS = [
{
"Id": "000001112222DDDD001",
"UserOrGroupId": "00AAABBBCCCDDD12345",
"Name": "Asia Pacific"
}
];
describe('c-cmm-specific-group-sharing', () => {
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);
}
// Prevent data saved on mocks from leaking between tests
jest.clearAllMocks();
});
// Helper function to wait until the microtask queue is empty. This is needed for promise
// timing when calling imperative Apex.
function flushPromises() {
// eslint-disable-next-line no-undef
return new Promise(resolve => setImmediate(resolve));
}
it('passes the recordId to the Apex method correctly', () => {
const RECORD_ID = '00AAABBBCCCDDD12345';
const APEX_PARAMETERS = { recordId: RECORD_ID };
// Assign mock value for resolved Apex promise
getSpecificGroups.mockResolvedValue(APEX_GROUPS_SUCCESS);
// Create initial element
const element = createElement('c-cmm-specific-group-sharing', {
is: Cmm_specificGroupSharing
});
element.recordId = RECORD_ID;
document.body.appendChild(element);
// Select button for executing Apex call
const buttonEl = element.shadowRoot.querySelector('lightning-button');
buttonEl.click();
return flushPromises().then(() => {
// Validate parameters of mocked Apex call
expect(getSpecificGroups.mock.calls[0][0]).toEqual(APEX_PARAMETERS);
});
});
it('renders one sharing group', () => {
// Assign mock value for resolved Apex promise
getSpecificGroups.mockResolvedValue(APEX_GROUPS_SUCCESS);
// Create initial element
const element = createElement('c-cmm-specific-group-sharing', {
is: Cmm_specificGroupSharing
});
document.body.appendChild(element);
// Select button for executing Apex call
const buttonEl = element.shadowRoot.querySelector('lightning-button');
buttonEl.click();
return flushPromises().then(() => {
const detailEls = element.shadowRoot.querySelectorAll('lightning-pill');
expect(detailEls.length).toBe(APEX_GROUPS_SUCCESS.length);
expect(detailEls[0].label).toBe(
APEX_GROUPS_SUCCESS[0].Name
);
});
});
it('handleRemoveSelectedItem works', () => {
// Create element
const element = createElement('c-cmm-specific-group-sharing', {
is: Cmm_specificGroupSharing
});
element.availableGroups = APEX_GROUPS_SUCCESS;
document.body.appendChild(element);
// Remove a selected item
const selPills = element.shadowRoot.querySelectorAll('lightning-pill');
selPills[0].dispatchEvent(new CustomEvent('remove'));
// Check selection
expect(element.availableGroups.length).toBe(0);
});
});
JS 和 HTML
import {
LightningElement
api
} from 'lwc';
import getSpecificGroups from '@salesforce/apex/GroupController.getSpecificGroups';
import deleteGroup from "@salesforce/apex/GroupController.deleteGroup";
export default class Cmm_specificGroupSharing extends LightningElement {
@api availableGroups;
@api
get recordId() {
return this._recordId;
}
set recordId(value) {
this._recordId = value;
}
connectedCallback() {
this.getSpecificGroups();
}
getSpecificGroups() {
this.availableGroups =[];
getSpecificGroups({
recordId: this._recordId,
})
.then(result => {
result.map(gShare => {
let obj = {
'Id': gShare.Id,
'UserOrGroupId': gShare.UserOrGroupId,
'Name': gShare.UserOrGroup.Name
};
this.availableGroups.push(obj);
return null;
})
console.log('result' + JSON.stringify(result));
})
.catch((err) => {
});
}
handleRemoveSelectedItem(event) {
const recordId = event.currentTarget.dataset.id;
this.availableGroups = this. availableGroups.filter(item => item.id !== recordId);
deleteGroup ({
recordId: recordId
})
.then(() => {
this.notifyUser('', this.deleteSuccessMsg, 'success');
})
.catch((err) => {
this.error = err;
});
}
}
<template>
<template for:each={availableGroups} for:item="groupShare">
<lightning-pill
data-id={groupShare.Id}
key={groupShare.Id}
label={groupShare.Name}
onremove={handleRemoveSelectedItem}>
</lightning-pill>
</template>
</template>
我很感激任何帮助。谢谢!