From bd45d4c7aac6d1c6b054185058a96b2e01669828 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Wed, 14 Mar 2018 21:00:08 -0400 Subject: tests --- frontend/__tests__/AddToCart.test.js | 24 ++++++++++ frontend/__tests__/Cart.test.js | 30 ++++++++++++ frontend/__tests__/TakeMyMoney.test.js | 55 ++++++++++++++++++++++ .../__tests__/__snapshots__/AddToCart.test.js.snap | 9 ++++ .../__snapshots__/TakeMyMoney.test.js.snap | 18 +++++++ 5 files changed, 136 insertions(+) create mode 100644 frontend/__tests__/AddToCart.test.js create mode 100644 frontend/__tests__/Cart.test.js create mode 100644 frontend/__tests__/TakeMyMoney.test.js create mode 100644 frontend/__tests__/__snapshots__/AddToCart.test.js.snap create mode 100644 frontend/__tests__/__snapshots__/TakeMyMoney.test.js.snap (limited to 'frontend/__tests__') diff --git a/frontend/__tests__/AddToCart.test.js b/frontend/__tests__/AddToCart.test.js new file mode 100644 index 0000000..2285b3b --- /dev/null +++ b/frontend/__tests__/AddToCart.test.js @@ -0,0 +1,24 @@ +import React from 'react'; +import { shallow, mount } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import { AddToCart } from '../components/AddToCart'; + +const currentUser = { + me: {}, + refetch() {}, +}; + +describe('', () => { + it('renders and matches snapshot', () => { + const wrapper = shallow( {}} currentUser={currentUser} />); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + + it('adds an item to the cart', () => { + const addToCart = jest.fn(() => Promise.resolve()); + const wrapper = shallow(); + wrapper.simulate('click'); + expect(addToCart).toHaveBeenCalled(); + expect(addToCart).toHaveBeenCalledWith({ variables: { id: 'abc123' } }); + }); +}); diff --git a/frontend/__tests__/Cart.test.js b/frontend/__tests__/Cart.test.js new file mode 100644 index 0000000..0936e97 --- /dev/null +++ b/frontend/__tests__/Cart.test.js @@ -0,0 +1,30 @@ +import React from 'react'; +import { shallow, mount } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import { Cart } from '../components/Cart'; + +const cartItem = { + item: { price: 5000 }, + quantity: 10, +}; + +const currentUser = { + me: { + cart: [cartItem, cartItem, cartItem], + }, + refetch() {}, +}; + +describe('', () => { + it('renders', () => { + const wrapper = shallow(); + }); + + xit('opens', () => { + const wrapper = shallow(); + expect('wes').toEqual('figure out how to call test context'); + }); + // it('renders', () => { + // shallow(); + // }); +}); diff --git a/frontend/__tests__/TakeMyMoney.test.js b/frontend/__tests__/TakeMyMoney.test.js new file mode 100644 index 0000000..2b33ca6 --- /dev/null +++ b/frontend/__tests__/TakeMyMoney.test.js @@ -0,0 +1,55 @@ +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'; + +const wait = amount => new Promise(resolve => setTimeout(resolve, amount)); + +Router.router = { push() {} }; + +const cartItem = { + item: { price: 5000 }, + quantity: 10, +}; + +const currentUser = { + me: { + cart: [cartItem, cartItem, cartItem], + }, + refetch() {}, +}; + +describe('', () => { + it('renders', () => { + const wrapper = shallow( {}} currentUser={currentUser} />); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + + it('creates an order onToken', () => { + const createOrderSpy = jest.fn(() => Promise.resolve({ data: { createOrder: { id: 'xyz789' } } })); + const wrapper = shallow(); + // manually run onToken + wrapper.instance().onToken({ id: 'abc123' }); + expect(createOrderSpy).toBeCalled(); + expect(createOrderSpy).toBeCalledWith({ variables: { token: 'abc123' } }); + }); + + it('turns the progress bar on', () => { + const createOrderSpy = jest.fn(() => Promise.resolve({ data: { createOrder: { id: 'xyz789' } } })); + NProgress.start = jest.fn(); + const wrapper = shallow(); + wrapper.instance().onToken({ id: 'abc123' }); + expect(NProgress.start).toHaveBeenCalled(); + }); + + it('routes to the order page', async () => { + Router.router.push = jest.fn(); + const createOrderSpy = jest.fn(() => Promise.resolve({ data: { createOrder: { id: 'xyz789' } } })); + const wrapper = shallow(); + wrapper.instance().onToken({ id: 'abc123' }); + await wait(0); + expect(Router.router.push).toHaveBeenCalledWith({ pathname: '/order/xyz789', query: { id: 'xyz789' } }); + }); +}); diff --git a/frontend/__tests__/__snapshots__/AddToCart.test.js.snap b/frontend/__tests__/__snapshots__/AddToCart.test.js.snap new file mode 100644 index 0000000..999161c --- /dev/null +++ b/frontend/__tests__/__snapshots__/AddToCart.test.js.snap @@ -0,0 +1,9 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` renders and matches snapshot 1`] = ` + +`; diff --git a/frontend/__tests__/__snapshots__/TakeMyMoney.test.js.snap b/frontend/__tests__/__snapshots__/TakeMyMoney.test.js.snap new file mode 100644 index 0000000..8c77e01 --- /dev/null +++ b/frontend/__tests__/__snapshots__/TakeMyMoney.test.js.snap @@ -0,0 +1,18 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` renders 1`] = ` + +`; -- cgit v1.3