From b1600adec47a04f60e1da07d94a3ed3906ff5aee Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 14 Jun 2018 16:44:21 -0400 Subject: starter files --- .../frontend/__tests__/TakeMyMoney.test.js | 98 ++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 finished-application/frontend/__tests__/TakeMyMoney.test.js (limited to 'finished-application/frontend/__tests__/TakeMyMoney.test.js') diff --git a/finished-application/frontend/__tests__/TakeMyMoney.test.js b/finished-application/frontend/__tests__/TakeMyMoney.test.js new file mode 100644 index 0000000..1c41410 --- /dev/null +++ b/finished-application/frontend/__tests__/TakeMyMoney.test.js @@ -0,0 +1,98 @@ +import { mount } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import NProgress from 'nprogress'; +import Router from 'next/router'; +import wait from 'waait'; +import { MockedProvider } from 'react-apollo/test-utils'; +import TakeMyMoney from '../components/TakeMyMoney'; +import { fakeUser, fakeCartItem } from '../lib/testUtils'; +import { CURRENT_USER_QUERY } from '../components/User'; + +Router.router = { push() {} }; + +const mocks = [ + { + request: { query: CURRENT_USER_QUERY }, + result: { + data: { + me: { + ...fakeUser(), + cart: [fakeCartItem()], + }, + }, + }, + }, +]; +describe('', () => { + it('renders', async () => { + const wrapper = mount( + + + + ); + // 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 () => { + const createOrderSpy = jest.fn().mockResolvedValue({ data: { createOrder: { id: 'xyz789' } } }); + + const wrapper = mount( + + + + ); + const component = wrapper.find('TakeMyMoney'); + component.instance().onToken({ id: 'abc123' }, createOrderSpy); + // check it + expect(createOrderSpy).toBeCalled(); + expect(createOrderSpy).toBeCalledWith({ variables: { token: 'abc123' } }); + }); + + it('turns the progress bar on', async () => { + const wrapper = mount( + + + + ); + await wait(); + wrapper.update(); + NProgress.start = jest.fn(); + + const createOrderSpy = jest.fn().mockResolvedValue({ data: { createOrder: { id: 'xyz789' } } }); + const component = wrapper.find('TakeMyMoney'); + component.instance().onToken({ id: 'abc123' }, createOrderSpy); + wrapper.find('button').simulate('click'); + expect(NProgress.start).toHaveBeenCalled(); + }); + + it('routes to the order page when completed', async () => { + // Mount the wrapper + const wrapper = mount( + + + + ); + await wait(); + wrapper.update(); + + // Spy on Router Push + Router.router.push = jest.fn(); + const createOrderSpy = jest.fn().mockResolvedValue({ data: { createOrder: { id: 'xyz789' } } }); + + const component = wrapper.find('TakeMyMoney'); + component.instance().onToken({ id: 'abc123' }, createOrderSpy); + + // submit the form + wrapper.find('button').simulate('click'); + await wait(); + expect(Router.router.push).toHaveBeenCalledWith({ + pathname: '/order', + query: { id: 'xyz789' }, + }); + }); +}); -- cgit v1.3