summaryrefslogtreecommitdiffstats
path: root/frontend/__tests__/mockMang.js
blob: 375973cbc600eb196fe3fe4d9395a206473ee49a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { importSchema } from 'graphql-import';
import { makeExecutableSchema } from 'graphql-tools';
import MockClient from 'graphql-mock';
import casual from 'casual';
import PropTypes from 'prop-types';
import { ApolloProvider } from 'react-apollo';
import { mount } from 'enzyme';

// seed it so we get consistent results
casual.seed(777);

// By default graphql-mock will generate random numbers, IDs, and Strings of "Hello World"
// We can overwrite any of those if we need to

const mocks = {
  ID: () => casual.uuid,
  Node: () => casual.uuid,
  DateTime: () => new Date(1400000000000),
  Int: () => casual.integer(400, 5500),
  OrderItem: () => ({
    quantity: casual.integer(1, 10),
    image: 'dog.jpg',
    title: casual.title,
    description: casual.description,
    price: () => 50000,
  }),
  Item: () => ({
    title: casual.title,
    description: casual.description,
    price: () => 50000,
    image: 'dog.jpg',
    largeImage: 'large-dog.jpg',
  }),
  User: () => ({
    email: casual.email,
    name: casual.name,
  }),
};

const fakeItem = () => ({
  __typename: 'Item',
  id: '123',
  price: 5000,
  user: null,
  image: 'dog-small.jpg',
  title: 'dogs are best',
  description: 'dogs',
  largeImage: 'dog.jpg',
});

const resolvers = {
  // Query: {
  //   cartOpen: () => true,
  // },
};

const typeDefs = importSchema('../backend/src/schema.graphql');

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
  resolverValidationOptions: { requireResolversForResolveType: false },
});

// Creates a mocked client
const mocked = new MockClient(schema, mocks);

const mountOptions = {
  context: {
    client: mocked.client,
  },
  childContextTypes: {
    client: PropTypes.object,
  },
};

const mountWithApollo = Component => {
  const wrapper = mount(<ApolloProvider client={mocked.client}>{Component}</ApolloProvider>);
  return { wrapper, component: wrapper.children() };
};

export default mountOptions;

export { mocked, mountWithApollo, fakeItem };