-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproduct.spec.tsx
More file actions
86 lines (70 loc) · 2.44 KB
/
product.spec.tsx
File metadata and controls
86 lines (70 loc) · 2.44 KB
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
85
86
import React from 'react';
import { MockedProvider } from '@apollo/react-testing';
import { render } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import { mockAllIsIntersecting } from 'react-intersection-observer/test-utils'
import { DATA_9200000111963040 } from '../../../../server/test/__mocks__/stubs/product-9200000111963040';
import { useGetProductQuery } from '../../graphql/_generated-hooks';
import { GET_PRODUCT } from './graphql/get-product.graphql';
import { Product } from './product';
/** MOCKS */
const mocks = [
{
request: {
query: GET_PRODUCT,
variables: {
id: '9200000111963040'
},
},
result: {
data: {
getProduct: {...DATA_9200000111963040}
},
},
},
];
describe('Product', () => {
// jest.useFakeTimers();
// jest.runAllTicks();
it('passes data correctly', async () => {
const id = '9200000111963040';
const wrapper = ({children}) => (
<MockedProvider mocks={mocks} addTypename={true}>
{children}
</MockedProvider>
);
const { result, waitForNextUpdate } = renderHook(() => useGetProductQuery({
variables: { id },
}), {
wrapper,
});
// component starts with loading state
expect(result.current.loading).toBe(true);
// Wait for the next thick
await waitForNextUpdate();
// correct state and passed variables
expect(result.current.loading).toBe(false);
expect(result.current.variables.id).toBe(id);
expect(result.current.data.getProduct).toEqual(
expect.objectContaining({
id: id,
title: 'Ghost Recon Breakpoint Auroa Edition - PS4'
})
);
});
it('renders correctly', async () => {
const id = '9200000111963040';
const testId = 'product-details';
const { container, queryByTestId, findByText } = render(
<MockedProvider mocks={mocks} addTypename={true}>
<Product id={id} />
</MockedProvider>
);
mockAllIsIntersecting(true);
expect(queryByTestId(testId)).toBe(null);
await findByText(/De Ghost Recon/);
expect(queryByTestId(testId)).toBeDefined();
// generic contract
expect(container).toMatchSnapshot();
});
});