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
|
import React from 'react';
import { shallow, mount } from 'enzyme';
import toJSON from 'enzyme-to-json';
import NProgress from 'nprogress';
import TakeMyMoney from '../components/TakeMyMoney';
import Router from 'next/router';
import wait from 'waait';
import mountOptions from './mockMang';
Router.router = { push() {} };
describe('<TakeMyMoney />', () => {
it('renders', async () => {
const wrapper = mount(<TakeMyMoney />, mountOptions);
// wait for it to load
await wait();
wrapper.update();
// find the button
const checkoutButton = wrapper.find('ReactStripeCheckout');
expect(toJSON(checkoutButton)).toMatchSnapshot();
});
it('creates an order onToken', async () => {
// TODO can this be done with a resolve jest fn?
const createOrderSpy = jest.fn(() =>
Promise.resolve({ data: { createOrder: { id: 'xyz789' } } })
);
const wrapper = mount(<TakeMyMoney />, mountOptions);
// manually run onToken
wrapper.instance().onToken({ id: 'abc123' }, createOrderSpy);
// check it
expect(createOrderSpy).toBeCalled();
expect(createOrderSpy).toBeCalledWith({ variables: { token: 'abc123' } });
});
it('turns the progress bar on', () => {
const createOrderSpy = jest.fn().mockResolvedValue({
data: { createOrder: { id: 'xyz789' } },
});
// spy on .start()
NProgress.start = jest.fn();
const wrapper = mount(<TakeMyMoney />, mountOptions);
wrapper.instance().onToken({ id: 'abc123' }, createOrderSpy);
expect(NProgress.start).toHaveBeenCalled();
});
it('routes to the order page when completed', async () => {
const createOrderSpy = jest.fn().mockResolvedValue({
data: { createOrder: { id: 'xyz789' } },
});
Router.router.push = jest.fn();
const wrapper = mount(<TakeMyMoney />, mountOptions);
wrapper.instance().onToken({ id: 'abc123' }, createOrderSpy);
await wait();
expect(Router.router.push).toHaveBeenCalledWith({
pathname: '/order',
query: { id: 'xyz789' },
});
});
});
|