1

I am working on testing my Components using Appolo Mock provider. However, I have this mutation query wherein one of my variables is set to a random UUID. How could I test it? It is giving me an error of no mock response for this query since my query does not match my mock please help tnx.

Component

const [createMenuProduct, { loading }] = useMutation(CREATE_MENU_PRODUCTS);

createMenuProduct({
  variables: {
    menuId: menuId,
    id: uuid(),
    productId: selectedProduct,
  },
});

test Mock

 {
    request: {
      query: CREATE_MENU_PRODUCTS,
      variables: {
        menuId: menuId,
        id: uuid(),
        productId: '4b1b6048-6cb1-46e0-ab4d-80fd11ebeacb',
      },
    },
    result: {
      data: {
        insertMenuProducts: {
          returning: [
            {
              menu_id: 'b591993d-af18-4bf5-88ad-26f08691afc7',
              product_id: '4b1b6048-6cb1-46e0-ab4d-80fd11ebeacb',
              product: {
                variant: {
                  id: '04befbe6-9635-4dde-abc2-673af13eb462',
                  isDeleted: false,
                  productVariantAddOns: [],
                },
              },
            },
          ],
        },
      },
    },
  },

currenly iam encountering this error due to I cannot match my mock variable with what is expected enter image description here enter image description here

1 Answers1

0

You could mock the returned value by uuid() to have the same as in the mock

const uuidVariable = 'mocked-uuid';

...
{
  request: {
    query: CREATE_MENU_PRODUCTS,
    variables: {
      menuId: menuId,
      id: uuidVariable,
      productId: '4b1b6048-6cb1-46e0-ab4d-80fd11ebeacb',

...

in your test

import uuid from 'uuid/v4';
jest.mock('uuid/v4');

describe('some component', () => {
  it('call mutation', () => {
    uuid.mockImplementation(() => uuidVariable);
    // here render component and interact to fire the mutation
  });  
});
diedu
  • 19,277
  • 4
  • 32
  • 49
  • Hi need to pass that mock because i still have other mutation after that one currently iam encountering this error No more mocked responses for the query: mutation insertMenuProducts – Eileen De Guzman Oct 22 '21 at 04:08
  • @EileenDeGuzman do you still get the error even with the changes I proposed in my answer? – diedu Oct 22 '21 at 04:17