1

I am using graphql-tools to mock for my UI (create-react-app) functional tests. I have a question around the MockList

Currently, I am mocking by type and one of the properties is an array but I still need to be able to customise the elements in the list

e.g. I have a LineItem type which has a mock defined as

const LineItem = () =>
  ({
    name: 'Item name'
  } as LineItemType)

and the cart type has a list of LineItem

const Cart = () =>
  ({
    id: 'cart-id',
    lineItems: [...new Array(2)],
  } as ActiveCartType)

Is there a way for the item name to be different for the 2 items in the cart?

I tried to map over the Array like so

const Cart = () =>
  ({
    id: 'cart-id',
    lineItems: [...new Array(2)].map(i => ({...i, id: '123', name: 'new item name'})),
  } as ActiveCartType)

But the name doesn't change in the mock result. It is still set to Item name. Only id changes to 123. Am I missing something here?

Joker
  • 55
  • 1
  • 8

1 Answers1

0

From the documentation for graphql-tools/mocking

we are using casual, a fake data generator for JavaScript, so that we can get a different result every time the field is called. You might want to use a collection of fake objects, or a generator that always uses a consistent seed, if you are planning to use the data for testing.

From your code, you could assign name to a generator function that adds unique names to that field.

  const LineItem = () =>
  ({
    name: functionGoesHere()
  } as LineItemType)

Two libraries that produce generated data for mocking are,

Manny
  • 369
  • 2
  • 7