-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
839e807
commit b7986ce
Showing
8 changed files
with
216 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export type Category = { | ||
id: string; | ||
id: number; | ||
name: string; | ||
subjectIcon?: string; | ||
}; |
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,106 @@ | ||
import React, {useEffect} from 'react'; | ||
import useBlogContext from './blog-context'; | ||
import { useParams} from 'react-router-dom'; | ||
import {WindowContextProvider} from '~/contexts/window'; | ||
import useDocumentHead from '~/helpers/use-document-head'; | ||
import RawHTML from '~/components/jsx-helpers/raw-html'; | ||
import ExploreBySubject from '~/components/explore-by-subject/explore-by-subject'; | ||
import ExploreByCollection from '~/components/explore-by-collection/explore-by-collection'; | ||
import PinnedArticle from './pinned-article/pinned-article'; | ||
import DisqusForm from './disqus-form/disqus-form'; | ||
import MoreStories from './more-stories/more-stories'; | ||
import SearchBar, {HeadingAndSearchBar} from '~/components/search-bar/search-bar'; | ||
import SearchResults from './search-results/search-results'; | ||
import {ArticleData, ArticleFromSlug} from './article/article'; | ||
import GatedContentDialog from './gated-content-dialog/gated-content-dialog'; | ||
import './blog.scss'; | ||
|
||
function WriteForUs({descriptionHtml, text, link}: { | ||
descriptionHtml: string; | ||
text: string; | ||
link: string; | ||
}) { | ||
return ( | ||
<section className='boxed'> | ||
<RawHTML Tag='h2' className="description" html={descriptionHtml} /> | ||
<a href={link} className="btn primary">{text}</a> | ||
</section> | ||
); | ||
} | ||
|
||
export function SearchResultsPage() { | ||
const {pageDescription, searchFor} = useBlogContext(); | ||
|
||
useDocumentHead({ | ||
title: 'OpenStax Blog Search', | ||
description: pageDescription | ||
}); | ||
|
||
return ( | ||
<React.Fragment> | ||
<div className="boxed left"> | ||
<SearchBar searchFor={searchFor} amongWhat='blog posts' /> | ||
</div> | ||
<SearchResults /> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
// Exported so it can be tested | ||
// eslint-disable-next-line complexity | ||
export function MainBlogPage() { | ||
const { | ||
pinnedStory, pageDescription, searchFor, | ||
subjectSnippet: categories, | ||
collectionSnippet: collections, | ||
footerText, footerButtonText, footerLink | ||
} = useBlogContext(); | ||
const writeForUsData = { | ||
descriptionHtml: footerText || 'Interested in sharing your story?', | ||
text: footerButtonText || 'Write for us', | ||
link: footerLink || '/write-for-us' | ||
}; | ||
|
||
useDocumentHead({ | ||
title: 'OpenStax News', | ||
description: pageDescription | ||
}); | ||
|
||
return ( | ||
<WindowContextProvider> | ||
<div className="boxed"> | ||
<HeadingAndSearchBar searchFor={searchFor} amongWhat='blog posts'> | ||
<h1>OpenStax Blog</h1> | ||
</HeadingAndSearchBar> | ||
<ExploreBySubject categories={categories} analyticsNav='Blog Subjects' /> | ||
<ExploreByCollection collections={collections} analyticsNav='Blog Collections' /> | ||
<PinnedArticle /> | ||
<MoreStories exceptSlug={pinnedStory && pinnedStory.meta.slug} /> | ||
</div> | ||
<div className="write-for-us"> | ||
<WriteForUs {...writeForUsData} /> | ||
</div> | ||
</WindowContextProvider> | ||
); | ||
} | ||
|
||
export function ArticlePage() { | ||
const {slug} = useParams(); | ||
const [articleData, setArticleData] = React.useState<ArticleData>(); | ||
|
||
useEffect( | ||
() => window.scrollTo(0, 0), | ||
[slug] | ||
); | ||
|
||
return ( | ||
<WindowContextProvider> | ||
<ArticleFromSlug slug={`news/${slug}`} onLoad={setArticleData} /> | ||
<DisqusForm /> | ||
<div className="boxed"> | ||
<MoreStories exceptSlug={slug as string} /> | ||
</div> | ||
<GatedContentDialog articleData={articleData} /> | ||
</WindowContextProvider> | ||
); | ||
} |
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
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 |
---|---|---|
@@ -1,35 +1,104 @@ | ||
import React from 'react'; | ||
import {render, screen} from '@testing-library/preact'; | ||
import {render, screen, waitFor} from '@testing-library/preact'; | ||
import {BrowserRouter, MemoryRouter, Routes, Route} from 'react-router-dom'; | ||
import {BlogContextProvider} from '~/pages/blog/blog-context'; | ||
import {MainBlogPage, ArticlePage} from '~/pages/blog/blog'; | ||
import useBlogContext, { | ||
BlogContextProvider, | ||
assertTType | ||
} from '~/pages/blog/blog-context'; | ||
import { | ||
MainBlogPage, | ||
ArticlePage, | ||
SearchResultsPage | ||
} from '~/pages/blog/blog-pages'; | ||
import {MainClassContextProvider} from '~/contexts/main-class'; | ||
import {test, expect} from '@jest/globals'; | ||
|
||
test('blog default page', async () => { | ||
render( | ||
<BrowserRouter> | ||
<BlogContextProvider> | ||
<MainClassContextProvider> | ||
<MainBlogPage /> | ||
</MainClassContextProvider> | ||
</BlogContextProvider> | ||
</BrowserRouter> | ||
); | ||
expect(await screen.findAllByText('Read more')).toHaveLength(3); | ||
expect(screen.queryAllByRole('textbox')).toHaveLength(1); | ||
}); | ||
import {describe, test, expect} from '@jest/globals'; | ||
import * as PDU from '~/helpers/page-data-utils'; | ||
|
||
describe('blog pages', () => { | ||
beforeAll(() => { | ||
const description = document.createElement('meta'); | ||
|
||
description.setAttribute('name', 'description'); | ||
document.head.appendChild(description); | ||
}); | ||
test('Main page', async () => { | ||
render( | ||
<BrowserRouter> | ||
<BlogContextProvider> | ||
<MainClassContextProvider> | ||
<MainBlogPage /> | ||
</MainClassContextProvider> | ||
</BlogContextProvider> | ||
</BrowserRouter> | ||
); | ||
expect(await screen.findAllByText('Read more')).toHaveLength(3); | ||
expect(screen.queryAllByRole('textbox')).toHaveLength(1); | ||
}); | ||
|
||
test('Article page', async () => { | ||
window.scrollTo = jest.fn(); | ||
|
||
render( | ||
<MemoryRouter initialEntries={['/blog/blog-article']}> | ||
<BlogContextProvider> | ||
<Routes> | ||
<Route path="/blog/:slug" element={<ArticlePage />} /> | ||
</Routes> | ||
</BlogContextProvider> | ||
</MemoryRouter> | ||
); | ||
expect(await screen.findAllByText('Read more')).toHaveLength(3); | ||
expect(screen.queryAllByRole('link')).toHaveLength(7); | ||
expect(window.scrollTo).toHaveBeenCalledWith(0, 0); | ||
}); | ||
|
||
test('Search Results page', async () => { | ||
/* eslint-disable camelcase, max-len */ | ||
jest.spyOn(PDU, 'fetchFromCMS').mockResolvedValueOnce( | ||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].map((id) => ({ | ||
id, | ||
slug: `slug-${id}`, | ||
collections: [], | ||
article_subjects: [] | ||
})) | ||
); | ||
|
||
render( | ||
<MemoryRouter initialEntries={['/blog/?q=education']}> | ||
<SearchResultsPage /> | ||
</MemoryRouter> | ||
); | ||
expect(document.head.querySelector('title')?.textContent).toBe( | ||
'OpenStax Blog Search' | ||
); | ||
}); | ||
|
||
test('assertTType throws for invalid value', () => { | ||
expect(() => assertTType('invalid')).toThrowError(); | ||
}); | ||
|
||
test('blog-context searchFor', async () => { | ||
function Inner() { | ||
const {searchFor} = useBlogContext(); | ||
|
||
React.useEffect(() => searchFor('education'), [searchFor]); | ||
|
||
return null; | ||
} | ||
|
||
function Outer() { | ||
return ( | ||
<MemoryRouter initialEntries={['/blog/blog-article']}> | ||
<BlogContextProvider> | ||
<Inner /> | ||
</BlogContextProvider> | ||
</MemoryRouter> | ||
); | ||
} | ||
|
||
window.scrollTo = jest.fn(); | ||
|
||
test('blog Article page', async () => { | ||
render( | ||
<MemoryRouter initialEntries={['/blog/blog-article']}> | ||
<BlogContextProvider> | ||
<Routes> | ||
<Route path='/blog/:slug' element={<ArticlePage />} /> | ||
</Routes> | ||
</BlogContextProvider> | ||
</MemoryRouter> | ||
); | ||
expect(await screen.findAllByText('Read more')).toHaveLength(3); | ||
expect(screen.queryAllByRole('link')).toHaveLength(7); | ||
render(<Outer />); | ||
await waitFor(() => expect(window.scrollTo).toHaveBeenCalledWith(0, 0)); | ||
}); | ||
}); |