From b6b0ee9c2ef43f4eef4df403897bcd8fd9ad2957 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Wed, 2 May 2018 14:59:28 -0400 Subject: move utils to lib --- frontend/__tests__/AddToCart.test.js | 2 +- frontend/__tests__/Cart.test.js | 4 +-- frontend/__tests__/EditUser.test.js | 6 ++-- frontend/__tests__/Nav.test.js | 2 +- frontend/__tests__/Order.test.js | 2 +- frontend/__tests__/Pagination.test.js | 2 +- frontend/__tests__/PleaseSignIn.test.js | 7 ++-- frontend/__tests__/RemoveFromCart.test.js | 2 +- frontend/__tests__/ResetRequest.test.js | 2 +- frontend/__tests__/Signup.test.js | 2 +- frontend/__tests__/SingleItem.test.js | 2 +- frontend/__tests__/TakeMyMoney.test.js | 4 +-- frontend/__tests__/testUtils.js | 55 ------------------------------- frontend/jest.setup.js | 13 -------- frontend/lib/testUtils.js | 55 +++++++++++++++++++++++++++++++ 15 files changed, 73 insertions(+), 87 deletions(-) delete mode 100644 frontend/__tests__/testUtils.js create mode 100644 frontend/lib/testUtils.js diff --git a/frontend/__tests__/AddToCart.test.js b/frontend/__tests__/AddToCart.test.js index c2fc004..5d205f1 100644 --- a/frontend/__tests__/AddToCart.test.js +++ b/frontend/__tests__/AddToCart.test.js @@ -2,7 +2,7 @@ import React from 'react'; import { mount } from 'enzyme'; import toJSON from 'enzyme-to-json'; import AddToCart from '../components/AddToCart'; -import mountOptions from './testUtils'; +import mountOptions from '../lib/testUtils'; describe('', () => { it('renders and matches snapshot', () => { diff --git a/frontend/__tests__/Cart.test.js b/frontend/__tests__/Cart.test.js index 0694a79..9f4e98f 100644 --- a/frontend/__tests__/Cart.test.js +++ b/frontend/__tests__/Cart.test.js @@ -1,11 +1,11 @@ import React from 'react'; import { shallow, mount } from 'enzyme'; import toJSON from 'enzyme-to-json'; -import mountOptions from './testUtils'; +import mountOptions from '../lib/testUtils'; import wait from 'waait'; import Cart from '../components/Cart'; import { MockedProvider } from 'react-apollo/test-utils'; -import { fakeOrder } from './testUtils'; +import { fakeOrder } from '../lib/testUtils'; import { LOCAL_STATE_QUERY } from '../queries/queries'; const mocks = [ diff --git a/frontend/__tests__/EditUser.test.js b/frontend/__tests__/EditUser.test.js index 7734a4a..2ec0bee 100644 --- a/frontend/__tests__/EditUser.test.js +++ b/frontend/__tests__/EditUser.test.js @@ -2,14 +2,14 @@ import React from 'react'; import { shallow } from 'enzyme'; import toJSON from 'enzyme-to-json'; import EditUser from '../components/EditUser'; -import { mountWithApollo } from './testUtils'; +import { mountWithApollo } from '../lib/testUtils'; import wait from 'waait'; const currentUser = { me: { name: 'Wes Bos', }, - refetch() {}, + refetch() { }, }; describe('', () => { @@ -43,7 +43,7 @@ describe('', () => { mutation.client.mutate = jest.fn(); const nameInput = wrapper.find('[name="name"]'); nameInput.simulate('change', { target: { name: 'name', value: 'Scott' } }); - wrapper.find('form').simulate('submit', { preventDefault() {} }); + wrapper.find('form').simulate('submit', { preventDefault() { } }); expect(mutation.client.mutate).toHaveBeenCalledWithVariables({ name: 'Scott' }); }); diff --git a/frontend/__tests__/Nav.test.js b/frontend/__tests__/Nav.test.js index 532ed3e..4989439 100644 --- a/frontend/__tests__/Nav.test.js +++ b/frontend/__tests__/Nav.test.js @@ -3,7 +3,7 @@ import { shallow, mount } from 'enzyme'; import toJSON from 'enzyme-to-json'; import Nav from '../components/Nav'; import { MockedProvider } from 'react-apollo/test-utils'; -import { fakeUser, fakeOrder } from './mockMang'; +import { fakeUser, fakeOrder } from 'testUtils'; import { CURRENT_USER_QUERY } from '../queries/queries'; import wait from 'waait'; // Mock the router diff --git a/frontend/__tests__/Order.test.js b/frontend/__tests__/Order.test.js index 1f2d691..b9948cf 100644 --- a/frontend/__tests__/Order.test.js +++ b/frontend/__tests__/Order.test.js @@ -4,7 +4,7 @@ import toJSON from 'enzyme-to-json'; import wait from 'waait'; import Order from '../components/Order'; import { MockedProvider } from 'react-apollo/test-utils'; -import { fakeOrder } from './testUtils'; +import { fakeOrder } from '../lib/testUtils'; import { SINGLE_ORDER_QUERY } from '../queries/queries'; const mocks = [ diff --git a/frontend/__tests__/Pagination.test.js b/frontend/__tests__/Pagination.test.js index 90078b8..4aa590e 100644 --- a/frontend/__tests__/Pagination.test.js +++ b/frontend/__tests__/Pagination.test.js @@ -3,7 +3,7 @@ import { shallow, mount } from 'enzyme'; import toJSON from 'enzyme-to-json'; import Pagination from '../components/Pagination'; import { MockedProvider } from 'react-apollo/test-utils'; -import { fakeItem } from './testUtils'; +import { fakeItem } from '../lib/testUtils'; import { ALL_ITEMS_QUERY } from '../queries/queries'; const data = { diff --git a/frontend/__tests__/PleaseSignIn.test.js b/frontend/__tests__/PleaseSignIn.test.js index 7a7e275..ea5b6d7 100644 --- a/frontend/__tests__/PleaseSignIn.test.js +++ b/frontend/__tests__/PleaseSignIn.test.js @@ -1,11 +1,10 @@ import React from 'react'; -import { shallow, mount } from 'enzyme'; -import toJSON from 'enzyme-to-json'; +import { mount } from 'enzyme'; +import wait from 'waait'; import PleaseSignIn from '../components/PleaseSignIn'; import { MockedProvider } from 'react-apollo/test-utils'; -import { fakeUser, fakeOrder } from './mockMang'; +import { fakeUser } from '../lib/testUtils'; import { CURRENT_USER_QUERY } from '../queries/queries'; -import wait from 'waait'; const notSignedInMocks = [ { diff --git a/frontend/__tests__/RemoveFromCart.test.js b/frontend/__tests__/RemoveFromCart.test.js index 8bce7de..a640722 100644 --- a/frontend/__tests__/RemoveFromCart.test.js +++ b/frontend/__tests__/RemoveFromCart.test.js @@ -5,7 +5,7 @@ import { ApolloConsumer } from 'react-apollo'; import { MockedProvider } from 'react-apollo/test-utils'; import wait from 'waait'; import RemoveFromCart from '../components/RemoveFromCart'; -import { mountWithApollo, fakeCartItem, fakeUser } from './testUtils'; +import { mountWithApollo, fakeCartItem, fakeUser } from '../lib/testUtils'; import { CURRENT_USER_QUERY, REMOVE_FROM_CART_MUTATION } from '../queries/queries'; const mocks = [ diff --git a/frontend/__tests__/ResetRequest.test.js b/frontend/__tests__/ResetRequest.test.js index 803f6b6..a08f3bc 100644 --- a/frontend/__tests__/ResetRequest.test.js +++ b/frontend/__tests__/ResetRequest.test.js @@ -2,7 +2,7 @@ import React from 'react'; import { mount } from 'enzyme'; import toJSON from 'enzyme-to-json'; import ResetRequest from '../components/ResetRequest'; -import mountOptions from './testUtils'; +import mountOptions from '../lib/testUtils'; describe('', () => { it('renders and matches snapshot', async () => { diff --git a/frontend/__tests__/Signup.test.js b/frontend/__tests__/Signup.test.js index aa6e1a2..fbeb513 100644 --- a/frontend/__tests__/Signup.test.js +++ b/frontend/__tests__/Signup.test.js @@ -3,7 +3,7 @@ import { mount } from 'enzyme'; import toJSON from 'enzyme-to-json'; import wait from 'waait'; import Signup from '../components/Signup'; -import mountOptions from './testUtils'; +import mountOptions from '../lib/testUtils'; function type(wrapper, name, value) { wrapper.find(`input[name="${name}"]`).simulate('change', { diff --git a/frontend/__tests__/SingleItem.test.js b/frontend/__tests__/SingleItem.test.js index b6b5020..3494f44 100644 --- a/frontend/__tests__/SingleItem.test.js +++ b/frontend/__tests__/SingleItem.test.js @@ -5,7 +5,7 @@ import toJSON from 'enzyme-to-json'; import SingleItem from '../components/SingleItem'; import { SINGLE_ITEM_QUERY } from '../queries/queries'; import { MockedProvider } from 'react-apollo/test-utils'; -import { fakeItem } from './testUtils'; +import { fakeItem } from '../lib/testUtils'; const data = { items: [fakeItem()], diff --git a/frontend/__tests__/TakeMyMoney.test.js b/frontend/__tests__/TakeMyMoney.test.js index 484ca7f..a7d2c98 100644 --- a/frontend/__tests__/TakeMyMoney.test.js +++ b/frontend/__tests__/TakeMyMoney.test.js @@ -5,9 +5,9 @@ import NProgress from 'nprogress'; import TakeMyMoney from '../components/TakeMyMoney'; import Router from 'next/router'; import wait from 'waait'; -import mountOptions from './testUtils'; +import mountOptions from '../lib/testUtils'; -Router.router = { push() {} }; +Router.router = { push() { } }; describe('', () => { it('renders', async () => { diff --git a/frontend/__tests__/testUtils.js b/frontend/__tests__/testUtils.js deleted file mode 100644 index 6b7132a..0000000 --- a/frontend/__tests__/testUtils.js +++ /dev/null @@ -1,55 +0,0 @@ -import casual from 'casual'; - -// seed it so we get consistent results -casual.seed(777); - -const fakeItem = () => ({ - __typename: 'Item', - id: '123', - price: 5000, - user: null, - image: 'dog-small.jpg', - title: 'dogs are best', - description: 'dogs', - largeImage: 'dog.jpg', -}); - -const fakeUser = () => ({ - __typename: 'User', - id: '4234', - name: casual.name, - email: casual.email, - permissions: ['ADMIN'], - orders: [], - cart: [], -}); - -const fakeOrderItem = () => ({ - id: casual.uuid, - image: `${casual.word}.jpg`, - title: casual.words(), - price: 4234, - quantity: 1, - description: casual.words(), -}); - -const fakeOrder = () => ({ - __typename: 'Order', - id: 'ord123', - charge: 'ch_123', - total: 40000, - items: [fakeOrderItem(), fakeOrderItem()], - createdAt: '2018-04 - 06T19: 24: 16.000Z', - user: fakeUser(), -}); - -const fakeCartItem = overrides => ({ - __typename: 'CartItem', - id: 'omg123', - quantity: 3, - item: fakeItem(), - user: fakeUser(), - ...overrides, -}); - -export { fakeItem, fakeUser, fakeCartItem, fakeOrder, fakeOrderItem }; diff --git a/frontend/jest.setup.js b/frontend/jest.setup.js index ad01898..82edfc9 100644 --- a/frontend/jest.setup.js +++ b/frontend/jest.setup.js @@ -2,16 +2,3 @@ import { configure } from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; configure({ adapter: new Adapter() }); - -expect.extend({ - toHaveBeenCalledWithVariables(received, argument) { - const receivedVariables = received.mock.calls[0][0].variables; - const pass = this.equals(receivedVariables, argument); - const message = () => - `expected ${this.utils.printExpected(argument)} - -to match ${this.utils.printReceived(receivedVariables)}`; - - return { message, pass }; - }, -}); diff --git a/frontend/lib/testUtils.js b/frontend/lib/testUtils.js new file mode 100644 index 0000000..6b7132a --- /dev/null +++ b/frontend/lib/testUtils.js @@ -0,0 +1,55 @@ +import casual from 'casual'; + +// seed it so we get consistent results +casual.seed(777); + +const fakeItem = () => ({ + __typename: 'Item', + id: '123', + price: 5000, + user: null, + image: 'dog-small.jpg', + title: 'dogs are best', + description: 'dogs', + largeImage: 'dog.jpg', +}); + +const fakeUser = () => ({ + __typename: 'User', + id: '4234', + name: casual.name, + email: casual.email, + permissions: ['ADMIN'], + orders: [], + cart: [], +}); + +const fakeOrderItem = () => ({ + id: casual.uuid, + image: `${casual.word}.jpg`, + title: casual.words(), + price: 4234, + quantity: 1, + description: casual.words(), +}); + +const fakeOrder = () => ({ + __typename: 'Order', + id: 'ord123', + charge: 'ch_123', + total: 40000, + items: [fakeOrderItem(), fakeOrderItem()], + createdAt: '2018-04 - 06T19: 24: 16.000Z', + user: fakeUser(), +}); + +const fakeCartItem = overrides => ({ + __typename: 'CartItem', + id: 'omg123', + quantity: 3, + item: fakeItem(), + user: fakeUser(), + ...overrides, +}); + +export { fakeItem, fakeUser, fakeCartItem, fakeOrder, fakeOrderItem }; -- cgit v1.3