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: display hero image in news carousel #901

Merged
merged 4 commits into from
Jan 17, 2023
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
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
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
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
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