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

Add support for @wire mocks #6

Open
divmain opened this issue May 22, 2024 · 3 comments
Open

Add support for @wire mocks #6

divmain opened this issue May 22, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@divmain
Copy link
Contributor

divmain commented May 22, 2024

Currently, there is no ergonomic way for a test author to mock the behavior of an @wire dependency. A mechanism should be built so that the test environment can directly call or modify the state of a mock, on a per-test basis. Once this is in place, an additional layer should be built on top of the first to enable a test to modify the behavior of a wire.

Example component code:

import { LightningElement, api, wire } from "lwc";
import { getRecord } from "lightning/uiRecordApi";
import ACCOUNT_NAME_FIELD from "@salesforce/schema/Account.Name";

export default class Record extends LightningElement {
  @api recordId;

  @wire(getRecord, { recordId: "$recordId", fields: [ACCOUNT_NAME_FIELD] })
  record;
}

Example test code:

import recordApiMock from 'mock{getRecord}:lightning/uiRecordApi';
import mockWire from '@lwc/test-runner';

describe(() => {
  beforeEach(async () => {
    await recordApiMock.reset();
  });

  it('should be possible to mock the wire value', async () => {
    const recordApiWire = await mockWire(recordApiMock, 'getRecord');
    await recordApiWire.setWiredData({
      Name: {
        value: 'foo'
      }
    });
    // Test the component's subsequent behavior.
  });
});
@divmain divmain added the enhancement New feature or request label May 22, 2024
@cardoso
Copy link
Contributor

cardoso commented Jun 10, 2024

I've been taking a stab at this locally. Should this feature be built upon @salesforce/wire-service-jest-util ?

@divmain
Copy link
Contributor Author

divmain commented Jun 13, 2024

Possibly. That library is largely agnostic to the test runner in which it is invoked, despite the name. We are looking at this one actively right now, although it is proving to be tricky to solve. We may need to revisit some of the baseline mock functionality to really make this ergonomic.

I'd be interested to hear what experiments/thinking you've done on this subject. Would pulling in the Jest util meet your needs? Or are there particular improvements of functionality that you'd like to see?

@cardoso
Copy link
Contributor

cardoso commented Jun 22, 2024

@divmain I'm not sure. My priority was to get the wire decorator working and get closer to supporting the components I'm maintaining in prod.

I guess this issue is solved by #21 though.

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

No branches or pull requests

2 participants