From 8a5825d52271d712ec7c8348447d433067e13ef2 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 13 Sep 2018 14:18:56 -0400 Subject: DONE --- .../66/frontend/__tests__/TakeMyMoney.test.js | 98 ++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100755 stepped-solutions/66/frontend/__tests__/TakeMyMoney.test.js (limited to 'stepped-solutions/66/frontend/__tests__/TakeMyMoney.test.js') diff --git a/stepped-solutions/66/frontend/__tests__/TakeMyMoney.test.js b/stepped-solutions/66/frontend/__tests__/TakeMyMoney.test.js new file mode 100755 index 0000000..3d68502 --- /dev/null +++ b/stepped-solutions/66/frontend/__tests__/TakeMyMoney.test.js @@ -0,0 +1,98 @@ +import { mount } from 'enzyme'; +import wait from 'waait'; +import toJSON from 'enzyme-to-json'; +import NProgress from 'nprogress'; +import Router from 'next/router'; +import { MockedProvider } from 'react-apollo/test-utils'; +import { ApolloConsumer } from 'react-apollo'; +import TakeMyMoney, { CREATE_ORDER_MUTATION } from '../components/TakeMyMoney'; +import { CURRENT_USER_QUERY } from '../components/User'; +import { fakeUser, fakeCartItem } from '../lib/testUtils'; + +Router.router = { push() {} }; + +const mocks = [ + { + request: { query: CURRENT_USER_QUERY }, + result: { + data: { + me: { + ...fakeUser(), + cart: [fakeCartItem()], + }, + }, + }, + }, +]; + +describe('', () => { + it('renders and matches snapshot', async () => { + const wrapper = mount( + + + + ); + await wait(); + wrapper.update(); + const checkoutButton = wrapper.find('ReactStripeCheckout'); + expect(toJSON(checkoutButton)).toMatchSnapshot(); + }); + it('creates an order ontoken', async () => { + const createOrderMock = jest.fn().mockResolvedValue({ + data: { createOrder: { id: 'xyz789' } }, + }); + const wrapper = mount( + + + + ); + const component = wrapper.find('TakeMyMoney').instance(); + // manully call that onToken method + component.onToken({ id: 'abc123' }, createOrderMock); + expect(createOrderMock).toHaveBeenCalled(); + expect(createOrderMock).toHaveBeenCalledWith({ variables: { token: 'abc123' } }); + }); + + it('turns the progress bar on', async () => { + const wrapper = mount( + + + + ); + await wait(); + wrapper.update(); + NProgress.start = jest.fn(); + const createOrderMock = jest.fn().mockResolvedValue({ + data: { createOrder: { id: 'xyz789' } }, + }); + const component = wrapper.find('TakeMyMoney').instance(); + // manully call that onToken method + component.onToken({ id: 'abc123' }, createOrderMock); + expect(NProgress.start).toHaveBeenCalled(); + }); + + it('routes to the order page when completed', async () => { + const wrapper = mount( + + + + ); + await wait(); + wrapper.update(); + const createOrderMock = jest.fn().mockResolvedValue({ + data: { createOrder: { id: 'xyz789' } }, + }); + const component = wrapper.find('TakeMyMoney').instance(); + Router.router.push = jest.fn(); + // manully call that onToken method + component.onToken({ id: 'abc123' }, createOrderMock); + await wait(); + expect(Router.router.push).toHaveBeenCalled(); + expect(Router.router.push).toHaveBeenCalledWith({ + pathname: '/order', + query: { + id: 'xyz789', + }, + }); + }); +}); -- cgit v1.3