We have a class that calls the getRecord
uiAPI and then using the result makes an APEX call to get more details from another custom object.
import { LightningElement, api, wire, track } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import getData from '@salesforce/apex/ApplicationController.getData';
const FIELDS = ['A.some_filed__c']
export default class MyComponent extends LightningElement {
// get progress bar config from account id
@wire(getRecord, { recordId: '$Id', fields: FIELDS })
wiredRecord({error, data}) {
if(error) {
this.error = error
} else if (data) {
getData({ configId: data.fields.some_data__c.value })
.then(result => {
//some code
})
.catch(error => {
this.error = error
});
}
}
}
We have this jest test for this:
import { createElement } from 'lwc';
import getData from '@salesforce/apex/ApplicationController.getData';
import { registerLdsTestWireAdapter } from '@salesforce/sfdx-lwc-jest';
import { getRecord } from 'lightning/uiRecordApi'
// mock data for rendering progress bar
const mockStepListRecords = require('./data/getStepListRecords.json');
const mockStepListNoRecords = require('./data/getStepListNoRecords.json');
const mockAccountRecord = require('./data/getAccountRecord.json');
const getRecordWireAdapter = registerLdsTestWireAdapter(getRecord);
// Mocking imperative Apex method call
jest.mock(
'@salesforce/apex/ApplicationController.getData',
() => {
return {
default: jest.fn()
};
},
{ virtual: true }
);
describe('c-progress-bar', () => {
afterEach(() => {
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
jest.clearAllMocks();
});
function flushPromises() {
return new Promise(resolve => setImmediate(resolve));
}
describe('getStepList @wire data', () => {
it('renders progress bar with steps', () => {
const element = createElement('c-progress-bar', {
is: ProgressBar
});
document.body.appendChild(element);
getRecordWireAdapter.emit(mockAccountRecord)
return flushPromises().then(() => {
getData.mockResolvedValue(mockStepListRecords)
const stepEls = element.shadowRoot.querySelectorAll(
'.progress-bar__step'
);
expect(stepEls.length).toBe(mockStepListRecords.length);
expect(stepEls[0].getAttribute('data-id')).toBe(
mockStepListRecords[0].Id
);
});
});
});
});
We tried Promise.resolve()
but event that doesn't work. We get the following error:
TypeError: Cannot read property 'then' of undefined
18 | this.error = error
19 | } else if (data) {
> 20 | getData({ configId: data.fields.some_data__c.value })
| ^
21 | .then(result => {
22 | // some code
We have mocked getData
but for some reason it cannot find it. Any help is highly appreciated!
@salesforce/wire-service-jest-util
?