'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 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jack Gore