import { createElement } from 'lwc';
import CustomPathApplication from 'c/customPathApplication';
import { getPicklistValues, getObjectInfo, getRecord } from 'lightning/uiObjectInfoApi';
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getRecordNotifyChange } from 'lightning/uiRecordApi';
import { RefreshEvent } from 'lightning/refresh';
jest.mock('lightning/uiObjectInfoApi', () => {
return {
getPicklistValues: jest.fn(),
getObjectInfo: jest.fn(),
getRecord: jest.fn(),
};
});
jest.mock('lightning/uiRecordApi', () => {
return {
updateRecord: jest.fn(),
getRecordNotifyChange: jest.fn(),
};
});
describe('c-custom-path-application', () => {
let element;
const mockRecordId = '001XXXXXXXXXXXXXXX';
const mockPicklistValues = {
data: {
values: [
{ label: 'Approved', value: 'Approved' },
{ label: 'Rejected', value: 'Rejected' },
],
},
};
const mockObjectInfo = {
data: {
fields: {
RecordTypeId: { value: '012XXXXXXXXXXXXXXX' },
},
},
};
const mockRecord = {
data: {
fields: {
Status__c: { value: 'Pending' },
RecordTypeId: { value: '012XXXXXXXXXXXXXXX' },
},
},
};
beforeEach(() => {
element = createElement('c-custom-path-application', {
is: CustomPathApplication,
});
element.recordId = mockRecordId;
document.body.appendChild(element);
});
afterEach(() => {
// Clean up the DOM after each test
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
jest.clearAllMocks();
});
it('should get object info on wired getObjectInfo', async () => {
getObjectInfo.mockResolvedValue(mockObjectInfo);
await flushPromises();
expect(element.defaultRecordTypeId).toBe('012XXXXXXXXXXXXXXX');
});
it('should get record on wired getRecord', async () => {
getRecord.mockResolvedValue(mockRecord);
await flushPromises();
expect(element.currentValue).toBe('Pending');
});
it('should get picklist values on wired getPicklistValues', async () => {
getPicklistValues.mockResolvedValue(mockPicklistValues);
await flushPromises();
expect(element.picklistValues).toEqual([
{ pItem: 'Approved' },
{ pItem: 'Rejected' },
]);
});
it('should update record and show success toast', async () => {
const updateRecordResponse = JSON.stringify({ success: true, stageName: 'Approved', message: 'Stage updated successfully' });
updateRecord.mockResolvedValue(updateRecordResponse);
const toastEvent = jest.fn();
element.addEventListener('lightning__showtoast', toastEvent);
element.selectedValue = 'Approved';
await element.handleMarkAsSelected();
await flushPromises();
expect(updateRecord).toHaveBeenCalled();
expect(toastEvent).toHaveBeenCalled();
});
it('should show error toast on update record failure', async () => {
const errorResponse = { body: { message: 'Update failed' } };
updateRecord.mockRejectedValue(errorResponse);
const toastEvent = jest.fn();
element.addEventListener('lightning__showtoast', toastEvent);
await element.handleMarkAsSelected();
await flushPromises();
expect(toastEvent).toHaveBeenCalled();
});
function flushPromises() {
return new Promise(setImmediate);
}
});