-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Next > Album): Display thumbnail images
- Loading branch information
Showing
7 changed files
with
169 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
|
||
import Img from '../index' | ||
|
||
const src = 'test.png' | ||
const alt = 'test' | ||
const renderComponent = (props = {}) => { | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
const utils = render(<Img src={src} alt={alt} {...props} />) | ||
const image = utils.queryByAltText(alt) | ||
return { ...utils, image } | ||
} | ||
|
||
describe('<Img />', () => { | ||
test('should render an <img> tag', () => { | ||
const { image } = renderComponent() | ||
expect(image).toBeInTheDocument() | ||
expect(image.tagName).toBe('IMG') | ||
}) | ||
|
||
test('should throw when no alt attribute is specified', () => { | ||
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => { | ||
/* noop */ | ||
}) | ||
render(<Img src={src} />) | ||
expect(consoleSpy).toHaveBeenCalledTimes(1) | ||
consoleSpy.mockRestore() | ||
}) | ||
|
||
test('should have a src attribute', () => { | ||
const { image } = renderComponent() | ||
expect(image).toHaveAttribute('src', src) | ||
}) | ||
|
||
test('should not have a class attribute', () => { | ||
const { image } = renderComponent() | ||
expect(image).not.toHaveAttribute('class') | ||
}) | ||
|
||
test('should adopt a className attribute', () => { | ||
const className = 'test' | ||
const { image } = renderComponent({ className }) | ||
expect(image).toHaveClass(className) | ||
}) | ||
|
||
test('should not adopt a srcset attribute', () => { | ||
const srcset = 'test-HD.png 2x' | ||
const { image } = renderComponent({ srcset }) | ||
expect(image).not.toHaveAttribute('srcset') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React from 'react' | ||
|
||
function Img({ alt, className, src }) { | ||
if (!alt) console.error('Missing ALT attribute on IMG') // eslint-disable-line no-console | ||
return <img className={className} src={src} alt={alt} /> | ||
} | ||
|
||
export default Img |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { useState } from 'react' | ||
import styled, { css } from 'styled-components' | ||
|
||
import Img from '../Img' | ||
|
||
const Bullet = styled.li` | ||
width: 195px; | ||
height: 110px; | ||
background-color: #545454; | ||
padding-bottom: 6px; | ||
float: left; | ||
margin: 6px; | ||
` | ||
|
||
const Caption = styled.span` | ||
font-weight: bold; | ||
font-size: 11px; | ||
margin: 0 5px; | ||
` | ||
|
||
const ImgButton = styled.a` | ||
display: block; | ||
border-style: solid; | ||
border-width: 5px 5px 20px; | ||
border-color: #545454; | ||
:hover { | ||
border-color: orange; | ||
} | ||
${({ viewed }) => viewed && css` | ||
border-color: white; | ||
`} | ||
` | ||
|
||
const ThumbImg = ({ | ||
onClick, | ||
caption, | ||
href, | ||
src, | ||
}) => { | ||
const [viewed, setViewed] = useState(false) | ||
const handleClick = (event) => { | ||
setViewed(true) | ||
event.preventDefault() | ||
onClick() | ||
} | ||
|
||
return ( | ||
<Bullet> | ||
<ImgButton viewed={viewed} href={href} onClick={handleClick}> | ||
<Img src={src} alt="Preview thumbnail (scaled down dimensions)" /> | ||
</ImgButton> | ||
<Caption>{caption}</Caption> | ||
</Bullet> | ||
) | ||
} | ||
|
||
export default ThumbImg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
|
||
import ThumbImg from '../index' | ||
|
||
describe('Component - <ThumbImg />', () => { | ||
const expectedValues = { | ||
alt: 'Preview thumbnail (scaled down dimensions)', | ||
src: 'test.png', | ||
} | ||
|
||
test('should match src attribute value', () => { | ||
const { container } = render(<ThumbImg src={expectedValues.src} />) | ||
const received = container.querySelector('img').src | ||
const expected = expect.stringContaining(expectedValues.src) | ||
expect(received).toEqual(expected) | ||
}) | ||
|
||
test('should have an alt attribute value', () => { | ||
const { container } = render(<ThumbImg />) | ||
const received = container.querySelector('img').alt | ||
const expected = expectedValues.alt | ||
expect(received).toEqual(expected) | ||
}) | ||
|
||
test('should not adopt a srcset attribute', () => { | ||
const { container } = render(<ThumbImg srcset="test-HD.png 2x" />) | ||
const received = container.querySelector('img').srcset | ||
const expected = '' | ||
expect(received).toEqual(expected) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters