Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing wire with emit and error doesn't work in v1.1.0 #268

Open
cjames9001 opened this issue Mar 31, 2022 · 1 comment
Open

Testing wire with emit and error doesn't work in v1.1.0 #268

cjames9001 opened this issue Mar 31, 2022 · 1 comment

Comments

@cjames9001
Copy link

cjames9001 commented Mar 31, 2022

Description

When using the emit and error functions and version 1.1.0 an error is thrown "getAccountList.error" is not a function and "getAccountList.emit is not a function" This previously worked in v0.13.0, I can switch between versions and see the tests pass and then see them fail with v1.1.0.

The example code is from the Write a jest test for wire service trailhead module

Steps to Reproduce

import { createElement } from 'lwc';
import WireApex from 'c/wireApex';
import getAccountList from '@salesforce/apex/AccountController.getAccountList';

const mockGetAccountList = require('./data/getAccountList.json');
const mockGetAccountListNoRecords = require('./data/getAccountListNoRecords.json');

describe('c-wire-apex', () => {
    afterEach(() => {
      while (document.body.firstChild) {
        document.body.removeChild(document.body.firstChild);
      }
      // Prevent data saved on mocks from leaking between tests
      jest.clearAllMocks();
    });

    describe('getAccountsList @wire data', () => {
        it('renders six records', () => {
            const element = createElement('c-wire-apex', {
                is: WireApex
            });
            document.body.appendChild(element);

            getAccountList.emit(mockGetAccountList);

            return Promise.resolve()
                .then(() => {
                    const accountElements = element.shadowRoot.querySelectorAll('p');
                    expect(accountElements.length).toBe(mockGetAccountList.length);
                    expect(accountElements[0].textContent).toBe(mockGetAccountList[0].Name);
                });
        });
    });

    describe('getAccountsList @wire error', () => {
        it('shows error panel element', () => {
            const element = createElement('c-wire-apex', {
                is: WireApex
            });

            document.body.appendChild(element);

            getAccountList.error();

            return Promise.resolve()
                .then(() => {
                    const errorElement = element.shadowRoot.querySelector('p');
                    expect(errorElement).not.toBeNull();
                    expect(errorElement.textContent).toBe('No accounts found.');
                });
        });
    });
});
<template>
    <lightning-card title="Wire Apex" icon-name="custom:custom107">
        <template if:true={accounts}>
            <template for:each={accounts} for:item="account">
                <p key={account.Id}>{account.Name}</p>
            </template>
        </template>
        <template if:true={error}>
            <p>No accounts found.</p>
        </template>
    </lightning-card>
</template>
import { LightningElement, wire } from 'lwc';
import getAccountList from '@salesforce/apex/AccountController.getAccountList';

export default class WireApex extends LightningElement {
    accounts;
    error;

    @wire(getAccountList)
    wiredAccounts({ error, data}) {
        if(data) {
            this.accounts = data;
            this.error = undefined;
        }
        else if(error){
            this.error = error;
            this.accounts = undefined;
        }
    }
}
// Jest config overrides (if any)
// No Jest config overrides.
# Command to repro
sfdx-lwc-jest

Expected Results

I'm expecting to be able to use emit and error in v1.1.0 like v0.13.0

Actual Results

I get the following errors when running the tests:

TypeError: getAccountList.emit is not a function
TypeError: getAccountList.error is not a function

Version

  • @salesforce/sfdx-lwc-jest: 1.1.0
  • Node: 16.13.2

Possible Solution

I was able to use the deprecated registerTestWireAdapter to get the emit function and use that to emit data and error properties in order to get my tests running, but that doesn't seem to be the right solution here.

import { registerTestWireAdapter } from "@salesforce/sfdx-lwc-jest"; //Deprecated
const getAccountListAdapter = registerTestWireAdapter(
  getAccountList 
);
describe("workaround", () => {
  it("can be used like this in the meantime", () => {
    // To mock a successful call
    getAccountListAdapter.emit({ "data" : [ {}, {}, {} ]});
    // To mock an error call
    getAccountListAdapter.emit({ "error": { "body": { "message": "Some Mock Error" } } });
  });
});

Additional context/Screenshots

If I'm just misunderstanding the api, please let me know and point me in the direction to whatever has replaced it.

@MarkHMorrison
Copy link

This is still not working in version 1.1.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants