Skip to content

Instantly share code, notes, and snippets.

@Quidge
Created April 13, 2019 20:41
Show Gist options
  • Save Quidge/73b70d3b55ac9454e2f8508157ecd8f9 to your computer and use it in GitHub Desktop.
Save Quidge/73b70d3b55ac9454e2f8508157ecd8f9 to your computer and use it in GitHub Desktop.
An Item component to be tested and the accompanying unit test suite for that component.
import React from "react";
import Link from "next/link";
import PropTypes from "prop-types";
import formatMoney from "../lib/formatMoney";
import Title from "../components/styles/Title";
import ItemStyles from "../components/styles/ItemStyles";
import PriceTag from "../components/styles/PriceTag";
import DeleteItem from "../components/DeleteItem";
import AddToCart from "../components/AddToCart";
export default class Item extends React.Component {
static propTypes = {
item: PropTypes.shape({
id: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
price: PropTypes.number.isRequired,
description: PropTypes.string.isRequired,
image: PropTypes.string,
largeImage: PropTypes.string
})
};
render() {
const { item } = this.props;
return (
<ItemStyles>
{item.image && <img src={item.image} alt={item.title} />}
<Title>
<Link
href={{
pathname: "/item",
query: { id: item.id }
}}
>
<a>{item.title}</a>
</Link>
</Title>
<PriceTag>{formatMoney(item.price)}</PriceTag>
<p>{item.description}</p>
<div className="buttonList">
<Link
href={{
pathname: "update",
query: { id: item.id }
}}
>
<a>Edit</a>
</Link>
<AddToCart id={item.id} />
<DeleteItem id={item.id}>Delete This Thing</DeleteItem>
</div>
</ItemStyles>
);
}
}
describe("<Item />", () => {
test.todo('it always renders `ItemStyles`')
describe("the rendered `ItemStyles`", () => {
test.todo("wraps all other elements");
// I can't find a way to confirm there aren't any sibling elements to ItemStyles
describe("when `item.image` is defined", () => {
beforeEach(() => {
props.image = "someimage.jpg";
});
test.todo("it renders an `img` tag");
test.todo("it sets the rendered `img` `src` to `item.image`");
test.todo("it sets the rendered `img` `alt` to `item.title`");
});
describe("when `item.image` is not defined", () => {
test.todo("it does not render an `img` tag");
});
test.todo("always renders a `Title`");
describe("rendered `Title`", () => {
test.todo("always renders a `Link`");
describe("rendered `Link`", () => {
test.todo("it receives the correct href prop");
// I hate this. I'm trying to verify that Link received <a>{item.title}</a> as children. Testing what Link does with the passed children is the job Link.test.js, not Item.test.js.
test.todo("the inner html is `<a>{item.title}</a>`");
});
});
test.todo("always renders `PriceTag`");
describe("rendered `PriceTag`", () => {
// Same issue as before. This is not the place to be testing the output of PriceTag, even if I know what it should be.
todo.test("the inner html is `{formatMoney(item.price)}`");
});
test.todo("always renders a `p` tag");
describe("rendered `p` tag", () => {
test.todo("it is equal to the item description");
});
test.todo("always renders a `div.buttonList`");
describe("rendered `.buttonList`", () => {
test.todo("always renders a `Link`");
describe("rendered `Link`", () => {
test.todo("it receives the correct href prop");
test.todo("the inner text is `Edit`");
});
test.todo("always renders an `AddToCart`");
describe("rendered `AddToCart`", () => {
test.todo(
"sets the rendered `AddToCart`'s `id` prop to the same value as `item.id` "
);
});
test.todo("always renders an `DeleteItem`");
describe("rendered `DeleteItem`", () => {
test.todo(
"sets the rendered `DeleteItem`'s `id` prop to the same value as `item.id` "
);
test.todo("has innertext that equals `Delete This Thing`");
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment