From 093c172d16732ba8d01faa313e0af99d26902205 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Fri, 7 Sep 2018 12:15:53 -0400 Subject: getting there --- .../60/frontend/__tests__/SingleItem.test.js | 57 ++++++++++++++++++ .../__snapshots__/SingleItem.test.js.snap | 32 ++++++++++ .../60/frontend/components/SingleItem.js | 70 ++++++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100755 stepped-solutions/60/frontend/__tests__/SingleItem.test.js create mode 100755 stepped-solutions/60/frontend/__tests__/__snapshots__/SingleItem.test.js.snap create mode 100755 stepped-solutions/60/frontend/components/SingleItem.js (limited to 'stepped-solutions/60') diff --git a/stepped-solutions/60/frontend/__tests__/SingleItem.test.js b/stepped-solutions/60/frontend/__tests__/SingleItem.test.js new file mode 100755 index 0000000..2963bde --- /dev/null +++ b/stepped-solutions/60/frontend/__tests__/SingleItem.test.js @@ -0,0 +1,57 @@ +import { mount } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import wait from 'waait'; +import SingleItem, { SINGLE_ITEM_QUERY } from '../components/SingleItem'; +import { MockedProvider } from 'react-apollo/test-utils'; +import { fakeItem } from '../lib/testUtils'; + +describe('', () => { + it('renders with proper data', async () => { + const mocks = [ + { + // when someone makes a request with this query and variable combo + request: { query: SINGLE_ITEM_QUERY, variables: { id: '123' } }, + // return this fake data (mocked data) + result: { + data: { + item: fakeItem(), + }, + }, + }, + ]; + const wrapper = mount( + + + + ); + expect(wrapper.text()).toContain('Loading...'); + await wait(); + wrapper.update(); + // console.log(wrapper.debug()); + expect(toJSON(wrapper.find('h2'))).toMatchSnapshot(); + expect(toJSON(wrapper.find('img'))).toMatchSnapshot(); + expect(toJSON(wrapper.find('p'))).toMatchSnapshot(); + }); + + it('Errors with a not found item', async () => { + const mocks = [ + { + request: { query: SINGLE_ITEM_QUERY, variables: { id: '123' } }, + result: { + errors: [{ message: 'Items Not Found!' }], + }, + }, + ]; + const wrapper = mount( + + + + ); + await wait(); + wrapper.update(); + console.log(wrapper.debug()); + const item = wrapper.find('[data-test="graphql-error"]'); + expect(item.text()).toContain('Items Not Found!'); + expect(toJSON(item)).toMatchSnapshot(); + }); +}); diff --git a/stepped-solutions/60/frontend/__tests__/__snapshots__/SingleItem.test.js.snap b/stepped-solutions/60/frontend/__tests__/__snapshots__/SingleItem.test.js.snap new file mode 100755 index 0000000..2f41d4c --- /dev/null +++ b/stepped-solutions/60/frontend/__tests__/__snapshots__/SingleItem.test.js.snap @@ -0,0 +1,32 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` Errors with a not found item 1`] = ` +

+ + Shoot! + + Items Not Found! +

+`; + +exports[` renders with proper data 1`] = ` +

+ Viewing + dogs are best +

+`; + +exports[` renders with proper data 2`] = ` +dogs are best +`; + +exports[` renders with proper data 3`] = ` +

+ dogs +

+`; diff --git a/stepped-solutions/60/frontend/components/SingleItem.js b/stepped-solutions/60/frontend/components/SingleItem.js new file mode 100755 index 0000000..145b5ae --- /dev/null +++ b/stepped-solutions/60/frontend/components/SingleItem.js @@ -0,0 +1,70 @@ +import React, { Component } from 'react'; +import gql from 'graphql-tag'; +import { Query } from 'react-apollo'; +import Error from './ErrorMessage'; +import styled from 'styled-components'; +import Head from 'next/head'; + +const SingleItemStyles = styled.div` + max-width: 1200px; + margin: 2rem auto; + box-shadow: ${props => props.theme.bs}; + display: grid; + grid-auto-columns: 1fr; + grid-auto-flow: column; + min-height: 800px; + img { + width: 100%; + height: 100%; + object-fit: contain; + } + .details { + margin: 3rem; + font-size: 2rem; + } +`; + +const SINGLE_ITEM_QUERY = gql` + query SINGLE_ITEM_QUERY($id: ID!) { + item(where: { id: $id }) { + id + title + description + largeImage + } + } +`; +class SingleItem extends Component { + render() { + return ( + + {({ error, loading, data }) => { + if (error) return ; + if (loading) return

Loading...

; + if (!data.item) return

No Item Found for {this.props.id}

; + const item = data.item; + return ( + + + Sick Fits | {item.title} + + {item.title} +
+

Viewing {item.title}

+

{item.description}

+
+
+ ); + }} +
+ ); + } +} + +export default SingleItem; +export { SINGLE_ITEM_QUERY }; -- cgit v1.3