Skip to content

Commit

Permalink
feat: display hero image in news carousel (#901)
Browse files Browse the repository at this point in the history
* feat: display hero image in news carousel

* test: modify storybook to show hero image in carousel

Co-authored-by: Shauna Keating <59394696+shkeating@users.noreply.github.com>
Co-authored-by: Abigail Young <abbyoung@gmail.com>
  • Loading branch information
3 people authored Jan 17, 2023
1 parent 0388f66 commit 34fefac
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/components/NewsCarousel/NewsCarousel.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const mockArticles: ArticleListItemRecord[] = [
],
publishedDate: 'Aug 03, 2022',
preview: "It's a thing",
hero: { url: '/assets/images/hero.jpeg' },
},
{
id: '2',
Expand Down
8 changes: 7 additions & 1 deletion src/components/NewsCarouselItem/NewsCarouselItem.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
rgba(101, 114, 180, 1) 100%
);

.carouselImage {
.ussfLogo {
width: 50%;
}

.hero {
width: 100%;
max-height: 534px;
object-fit: cover;
}
}

.gridContainer {
Expand Down
8 changes: 8 additions & 0 deletions src/components/NewsCarouselItem/NewsCarouselItem.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ const mockArticle: ArticleListItemRecord = {
export const DefaultNewsCarouselItem = () => (
<NewsCarouselItem article={mockArticle} />
)

const articleWithHero = {
...mockArticle,
hero: { url: '/assets/images/hero.jpeg' },
}
export const NewsCarouselItemWithHero = () => (
<NewsCarouselItem article={articleWithHero} />
)
45 changes: 44 additions & 1 deletion src/components/NewsCarouselItem/NewsCarouselItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,55 @@ const mockArticle = {
preview: 'This is the preview text!',
publishedDate: '2022-05-18T17:18:40.802Z',
labels: [],
hero: {
url: 'http://cms.example.com/images/image.png',
},
}

describe('NewsCarouselItem component', () => {
it('renders the component with a mock article', () => {
test('renders the component with a mock article', () => {
render(<NewsCarouselItem article={mockArticle} />)
expect(screen.getAllByText('Announcing the dev blog')).toHaveLength(1)
expect(screen.getAllByText('This is the preview text!')).toHaveLength(1)
})

test("renders the article's hero image", () => {
render(<NewsCarouselItem article={mockArticle} />)

// should have no USSF logo
const logo = screen.queryByRole('img', {
name: 'USSF logo',
})
expect(logo).not.toBeInTheDocument()

// expect the hero image to be there
const img = screen.getByRole('img', { name: 'article hero graphic' })
expect(img).toBeInTheDocument()
expect(img).toHaveAttribute('src')
expect(img.getAttribute('src')).toEqual(
'http://cms.example.com/images/image.png'
)
expect(img).toHaveClass('hero')
})

test('does not render an img tag if article has no hero url', () => {
const mockWithNoHero = { ...mockArticle, hero: undefined }
render(<NewsCarouselItem article={mockWithNoHero} />)

const img = screen.queryByRole('img', {
name: 'article hero graphic',
})
expect(img).not.toBeInTheDocument()

// expect the default space force logo
const logo = screen.getByRole('img', {
name: 'USSF logo',
})
expect(logo).toBeInTheDocument()
expect(logo).toHaveAttribute('src')
expect(logo.getAttribute('src')).toEqual(
'/assets/images/Seal_of_the_United_States_Space_Force.svg'
)
expect(logo).toHaveClass('ussfLogo')
})
})
22 changes: 13 additions & 9 deletions src/components/NewsCarouselItem/NewsCarouselItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ const NewsCarouselItem = ({ article }: { article: ArticleListItemRecord }) => {
return (
<Grid row className={styles.carouselItemContainer}>
<Grid col={5} className={styles.imageContainer}>
{/*
TODO: current image is a placeholder. Will need to add a check
for determining if the article has an image.
*/}
<img
src="/assets/images/Seal_of_the_United_States_Space_Force.svg"
alt="USSF logo"
className={styles.carouselImage}
/>
{article.hero ? (
<img
src={article.hero.url}
alt="article hero graphic"
className={styles.hero}
/>
) : (
<img
src="/assets/images/Seal_of_the_United_States_Space_Force.svg"
alt="USSF logo"
className={styles.ussfLogo}
/>
)}
</Grid>
<Grid col={'fill'} className={styles.gridContainer}>
<Grid className={styles.textContainer}>
Expand Down
3 changes: 3 additions & 0 deletions src/operations/cms/queries/getInternalNewsCarouselArticles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export const GET_INTERNAL_NEWS_CAROUSEL_ARTICLES = gql`
name
type
}
hero {
url
}
}
}
`
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export type ArticleListItemRecord = {
sourceName?: string
sourceLink?: string
labels?: LabelRecord[]
hero?: {
url: string
}
}

/* ArticleRecord is the complete article used when viewing the single article page */
Expand Down

0 comments on commit 34fefac

Please sign in to comment.