Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update recently read books widget to use image CDN #181

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
],
"scripts": {
"develop": "yarn workspace www.chrisvogt.me develop:https",
"test": "yarn workspace gatsby-theme-chrisvogt test",
"test:watch": "yarn workspace gatsby-theme-chrisvogt test:watch",
"postinstall": "node index.js",
"prettier:format": "prettier --write ."
},
Expand Down
568 changes: 305 additions & 263 deletions theme/__mocks__/goodreads-widget.mock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions theme/jest-shim.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ArrayBuffer, TextDecoder, TextEncoder, Uint8Array } from 'util';
// import { ArrayBuffer, TextDecoder, TextEncoder, Uint8Array } from 'util';

global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
global.ArrayBuffer = ArrayBuffer;
global.Uint8Array = Uint8Array;
// global.TextEncoder = TextEncoder;
// global.TextDecoder = TextDecoder;
// global.ArrayBuffer = ArrayBuffer;
// global.Uint8Array = Uint8Array;

global.___loader = {
enqueue: jest.fn()
}
enqueue: jest.fn()
}
5 changes: 4 additions & 1 deletion theme/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gatsby-theme-chrisvogt",
"description": "My personal blog and website.",
"version": "0.28.0",
"version": "0.28.1",
"author": "Chris Vogt <[email protected]> (https://www.chrisvogt.me)",
"main": "index.js",
"license": "MIT",
Expand Down Expand Up @@ -39,6 +39,9 @@
"@babel/core": "^7.22.10",
"@babel/plugin-transform-react-jsx": "^7.22.5",
"@babel/preset-react": "^7.22.5",
"@testing-library/dom": "^10.1.0",
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^16.0.0",
"@testing-library/react-hooks": "^8.0.1",
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.7",
"babel-jest": "^29.6.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ exports[`Book matches the snapshot 1`] = `
}
>
<image
data-testid="book-preview-thumbnail"
height="190"
transform="translate(14.53 -3.68) scale(0.5)"
width="128"
Expand Down
8 changes: 7 additions & 1 deletion theme/src/components/artwork/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ const Book = ({ thumbnailURL, title }) => (
clipPath: `url(#ec3c1a72-0104-4d8a-830f-57d2245cb2f4)`
}}
>
<image width='128' height='190' transform='translate(14.53 -3.68) scale(0.5)' xlinkHref={thumbnailURL} />
<image
data-testid='book-preview-thumbnail'
width='128'
height='190'
transform='translate(14.53 -3.68) scale(0.5)'
xlinkHref={thumbnailURL}
/>
</g>
</svg>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ exports[`BookLink matches the snapshot 1`] = `
}
>
<image
data-testid="book-preview-thumbnail"
height="190"
transform="translate(14.53 -3.68) scale(0.5)"
width="128"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ exports[`Goodreads Widget matches the loading state snapshot 1`] = `
className="css-qnzjfx-RecentlyReadBooks"
>
<div
className="rect-shape css-b3ph2j"
className="rect-shape css-1ncz0al"
style={
{
"backgroundColor": "#efefef",
Expand All @@ -210,7 +210,7 @@ exports[`Goodreads Widget matches the loading state snapshot 1`] = `
}
/>
<div
className="rect-shape css-b3ph2j"
className="rect-shape css-1ncz0al"
style={
{
"backgroundColor": "#efefef",
Expand All @@ -221,7 +221,7 @@ exports[`Goodreads Widget matches the loading state snapshot 1`] = `
}
/>
<div
className="rect-shape css-b3ph2j"
className="rect-shape css-1ncz0al"
style={
{
"backgroundColor": "#efefef",
Expand All @@ -232,7 +232,7 @@ exports[`Goodreads Widget matches the loading state snapshot 1`] = `
}
/>
<div
className="rect-shape css-b3ph2j"
className="rect-shape css-1ncz0al"
style={
{
"backgroundColor": "#efefef",
Expand All @@ -243,7 +243,7 @@ exports[`Goodreads Widget matches the loading state snapshot 1`] = `
}
/>
<div
className="rect-shape css-b3ph2j"
className="rect-shape css-1ncz0al"
style={
{
"backgroundColor": "#efefef",
Expand All @@ -254,7 +254,7 @@ exports[`Goodreads Widget matches the loading state snapshot 1`] = `
}
/>
<div
className="rect-shape css-b3ph2j"
className="rect-shape css-1ncz0al"
style={
{
"backgroundColor": "#efefef",
Expand All @@ -265,7 +265,7 @@ exports[`Goodreads Widget matches the loading state snapshot 1`] = `
}
/>
<div
className="rect-shape css-b3ph2j"
className="rect-shape css-1ncz0al"
style={
{
"backgroundColor": "#efefef",
Expand All @@ -276,7 +276,7 @@ exports[`Goodreads Widget matches the loading state snapshot 1`] = `
}
/>
<div
className="rect-shape css-b3ph2j"
className="rect-shape css-1ncz0al"
style={
{
"backgroundColor": "#efefef",
Expand All @@ -287,7 +287,7 @@ exports[`Goodreads Widget matches the loading state snapshot 1`] = `
}
/>
<div
className="rect-shape css-b3ph2j"
className="rect-shape css-1ncz0al"
style={
{
"backgroundColor": "#efefef",
Expand All @@ -298,7 +298,7 @@ exports[`Goodreads Widget matches the loading state snapshot 1`] = `
}
/>
<div
className="rect-shape css-b3ph2j"
className="rect-shape css-1ncz0al"
style={
{
"backgroundColor": "#efefef",
Expand All @@ -309,7 +309,7 @@ exports[`Goodreads Widget matches the loading state snapshot 1`] = `
}
/>
<div
className="rect-shape css-b3ph2j"
className="rect-shape css-1ncz0al"
style={
{
"backgroundColor": "#efefef",
Expand All @@ -320,7 +320,7 @@ exports[`Goodreads Widget matches the loading state snapshot 1`] = `
}
/>
<div
className="rect-shape css-b3ph2j"
className="rect-shape css-1ncz0al"
style={
{
"backgroundColor": "#efefef",
Expand Down
31 changes: 24 additions & 7 deletions theme/src/components/widgets/goodreads/recently-read-books.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { Themed } from '@theme-ui/mdx'

import BookLink from './book-link'

export const HEADLINE ='Books'
export const BODY_TEXT = 'The last 12 books I read and finished.'

const RecentlyReadBooks = ({ books = [], isLoading }) => (
<div className='gallery'>
<div sx={{ mb: 4 }}>
Expand All @@ -17,10 +20,12 @@ const RecentlyReadBooks = ({ books = [], isLoading }) => (
fontSize: [3, 4]
}}
>
Books
{HEADLINE}
</Heading>

<Themed.p>The last 12 books I read and finished.</Themed.p>
<Themed.p>
{BODY_TEXT}
</Themed.p>

<div
sx={{
Expand All @@ -33,13 +38,25 @@ const RecentlyReadBooks = ({ books = [], isLoading }) => (
Array(12)
.fill()
.map((item, idx) => (
<RectShape color='#efefef' key={idx} sx={{ boxShadow: `md`, width: `100%`, minHeight: `140px` }} />
<RectShape
color='#efefef'
key={idx}
sx={{
boxShadow: `md`,
minHeight: `140px`,
width: `100%`,
}}
/>
))}
{!isLoading &&
books.map(book => {
const { infoLink, smallThumbnail: thumbnailURL, title } = book
return <BookLink key={kebabCase(title)} infoLink={infoLink} thumbnailURL={thumbnailURL} title={title} />
})}
books.map(book => (
<BookLink
infoLink={book.infoLink}
key={book.id}
thumbnailURL={`${book.cdnMediaURL}?fm=webp`}
title={book.title}
/>
))}
</div>
</div>
</div>
Expand Down
36 changes: 36 additions & 0 deletions theme/src/components/widgets/goodreads/recently-read-books.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react'
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'

import RecentlyReadBooks, { HEADLINE, BODY_TEXT } from './recently-read-books'
import goodreadsMock from '../../../../__mocks__/goodreads-widget.mock.json'

const mockBooks = goodreadsMock.payload.collections.recentlyReadBooks

describe('Widget/Goodreads/RecentlyReadBooks', () => {
describe('loading state', () => {
it('renders a placeholder for each book expected to render', () => {
const { container } = render(<RecentlyReadBooks books={[]} isLoading={true} />)
expect(container.querySelectorAll('.rect-shape')).toHaveLength(12)
})
})

describe('success sate', () => {
it('renders a headline and paragraph text for the widget', () => {
render(<RecentlyReadBooks books={mockBooks} isLoading={false} />)
expect(screen.getByText(HEADLINE)).toBeInTheDocument()
expect(screen.getByText(HEADLINE)).toHaveTextContent(HEADLINE)
expect(screen.getByText(BODY_TEXT)).toBeInTheDocument()
expect(screen.getByText(BODY_TEXT)).toHaveTextContent(BODY_TEXT)
})

it('renders thumbnails using the image cdn urls', () => {
render(<RecentlyReadBooks books={mockBooks} isLoading={false} />)
const images = screen.getAllByTestId('book-preview-thumbnail')
expect(images).toHaveLength(mockBooks.length)
images.forEach((image, idx) => {
expect(image).toHaveAttribute('xlink:href', `${mockBooks[idx].cdnMediaURL}?fm=webp`)
})
})
})
})
Loading
Loading