3

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!

6
  • What API version and Jest version are you using? Also, are you experiencing this from any other test suites?
    – pchittum
    May 19, 2020 at 7:48
  • 1
    following issue on github was opened for this and it is a known bug being fixed.
    – raj
    May 21, 2020 at 11:46
  • @raj Did you get this resolved after the Github issue was merge? If so, it'd be useful to put that as an answer and mark it as the accepted answer to help other. Jul 9, 2020 at 19:55
  • I am having the same issue when I am calling imperative apex in Wired Function. @TrevorBliss Sep 22, 2020 at 11:20
  • @DeepSinghal Can you verify you're on version 2.4.2 (or higher) of @salesforce/wire-service-jest-util? Sep 22, 2020 at 15:30

0

You must log in to answer this question.

Browse other questions tagged .