graphql-kit / graphql-lodash

🛠 Data manipulation for GraphQL queries with lodash syntax
https://apis.guru/graphql-lodash/
MIT License
1.23k stars 50 forks source link

Mocking graphqlLodash with Jest #8

Closed aperkaz closed 6 years ago

aperkaz commented 6 years ago

Hello,

First of all, thanks for the nice project. I am trying to mock graphql-lodash with Jest, in order to test components decorated with it. Is there any de-facto / preferred way of mocking it? Below I add my code snippets.

Tests:

import React from 'react';
import chai, { expect } from 'chai';
import { shallow } from 'enzyme';
import chaiEnzyme from 'chai-enzyme';

import Foo from './Foo';

describe('Foo', () => {
  const shallowed = shallow(<Foo />);

  it('test Foo', () => {
   {...}
  });
});

chai.use(chaiEnzyme());

I have tried multiple ways to mock, but i keep on getting errors.

Thanks :slightly_smiling_face:

IvanGoncharov commented 6 years ago

Hi @aperkaz, Glad that you like our project 🎉

I am trying to mock graphql-lodash with Jest, in order to test components decorated with it.

Not sure that I understand your use case probably because I never wrote any test for React components 😞 Can you please provide more details? Maybe you can provide a simplified version of your test that reproduces issue?

aperkaz commented 6 years ago

Hello @IvanGoncharov, Thanks for the reply 🙂 . In my case, it seems that the issues when mocking graphql-lodash where side effects from the wrong mock of other modules 😬

IvanGoncharov commented 6 years ago

where side effects from the wrong mock of other modules

@aperkaz Phew 😇

BTW. Do you use this package in production? Would be great to see a few examples of your queries if possible? It will help us to understand where to go with future versions.

aperkaz commented 6 years ago

@IvanGoncharov right now we are using it to transform the results of graphQL queries to a CMS (Contentful) to the right props that our React components require. Using graphql-lodash allows to easily change the props to what the stateless components are expecting, and also easy to extend if the props in components are chagned/renamed. Below I attach an example query:

query getPost($id: ID!) {
  post(id: $id) {
    photo: postPhoto {
      alt: title
      src: url
    }
    headline
    subtitle: postPerex
    content
    author: postAuthor @_(get: "name") {
      name
    }
    date
  }
}
IvanGoncharov commented 6 years ago

@aperkaz Thanks for example 🎉