1

I have two wire calls to getPicklistValues method in my LWC component. In my jest test I am calling getPicklistValues twice to emit the test data based on different configs. However the mocking is not working and not emiting the test data.

import { getObjectInfo, getPicklistValues } from 'lightning/uiObjectInfoApi';

...
...
    @wire(getPicklistValues,
        {
            recordTypeId: '$objectInfo.data.defaultRecordTypeId',
            fieldApiName: STRATEGY_FIELD
        }
    )
    _strategyOptions;
    @wire(getPicklistValues,
        {
            recordTypeId: '$objectInfo.data.defaultRecordTypeId',
            fieldApiName: STATUS_FIELD
        }
    )
    _statusOptions;

My jest test looks like this

import { getObjectInfo, getPicklistValues } from 'lightning/uiObjectInfoApi';
...
...
    getPicklistValues.emit({ data: MOCK_STATUS_OPTIONS }, 
      (config) => {
        return (config.recordTypeId === '012000000000000AAA' && config.fieldApiName === 'Status__c');
      });
    getPicklistValues.emit({ data: MOCK_STRATEGY_OPTIONS }, 
      (config) => {
        return (config.recordTypeId === '012000000000000AAA' && config.fieldApiName === 'Strategy__c');
      });

Thanks for any help in advance!

2
  • Can you please add actual code instead of individual code snippets? perhaps at least one test method! Currently, it's hard to tell what's the issue exactly. Apr 1, 2022 at 17:20
  • Would you mind linking the doc where you've seen this syntax getPicklistValues.emit({ data: {}}, () => {}) ?? I've never seen this. Apr 1, 2022 at 18:06

2 Answers 2

1

If you will try to console.log config in emit(), you will see following:

{
  fieldApiName: {
    objectApiName: 'SomeObject',
    fieldApiName: 'SomeApiName'
  },
  recordTypeId: 'xxxxxxxxxxxxxxxxxx'
}

Solution that worked for me:

import { getObjectInfo, getPicklistValues } from 'lightning/uiObjectInfoApi';

...

const mockGetObjectInfo = require('./data/getObjectInfo.json');
const mockFirstPicklistValues = require('./data/firstPicklistValues.json');
const mockSecondPicklistValues = require('./data/secondPicklistValues.json');

...

describe('SomeName', () => {
  
  ...

  async function flushPromises() {
    return Promise.resolve();
  }

  ...

  it('should emit correctly', async () => {
    
    ...

    getObjectInfo.emit(mockGetObjectInfo);

    await flushPromises();

    getPicklistValues.emit(mockFirstPicklistValues, (config) => {
      return config.recordTypeId === 'xxxxxxxxxxxxxxxxxx' 
        && config.fieldApiName?.fieldApiName === 'Your fieldApiName';
    });
    getPicklistValues.emit(mockSecondPicklistValues, (config) => {
      return config.recordTypeId === 'xxxxxxxxxxxxxxxxxx' 
        && config.fieldApiName?.fieldApiName === 'Your fieldApiName';
    });

    await flushPromises();

    ...

  });

});

Also suggest visiting a Git repository that provides further assistance on this issue: click

P.S. If you don't want to use json with data, you can set mocks directly in test.

-2

One way to solve the problem is by using a data file instead. In the __tests__ folder, place another folder called data. Fill those two files with the json expected to be returned from the @wire.

Add these lines to the test:

const STRATEGY_PICKLIST = require("./data/getStrategyValues.json");
const STATUS_PICKLIST = require("./data/getStatusValues.json");

Add, in the body of each test:

getPicklistValues.emit(STRATEGY_PICKLIST);
getPicklistValues.emit(STATUS_PICKLIST);
2
  • Thanks @Arlene, however this wont work. My component uses both the picklist values in the same unit test. In which case, suggest solution would always emit the last json in last emit call.
    – Amit
    May 18, 2022 at 13:53
  • Is it possible to alias the same item in the import statment, like: import { getPicklistValues, getPicklistValues as picklists} from 'lightning/uiObjectInfoApi'; ? I've never deliberately imported the same item twice with an alias on one of them.
    – Arlene
    May 19, 2022 at 18:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .