reactjsjestjsrelayjsrelayreact-relay

How to define default mock data for relay?


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 ?


Solution

  • 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'
              }
            },
          }),
        );