How to define default mock data for relay?

810 views Asked by At

I am doing unit tests on a react app that is using relay as a graphql client when testing , i get mocked data with like this :

        title: '<mock-value-for-field-"title">',

How can i define default mock data ?

1

There are 1 answers

0
Jack Gore On BEST ANSWER

To mock the specific value of a field in Relay you will want to use MockPayloadGenerator.

https://relay.dev/docs/guides/testing-relay-components/#mock-payload-generator-and-the-relay_test_operation-directive

Will need more code to give a complete example but essentially will need to do something similar to:

    environment.mock.resolveMostRecentOperation(operation =>
      MockPayloadGenerator.generate(operation, {
        String(context) {
          if (context.name === 'title') {
            return 'value you want to be used for title field'
          }
        },
      }),
    );