Skip to content

Commit

Permalink
Press page
Browse files Browse the repository at this point in the history
All this just to get coverage for one branch in clipped-image.tsx!
(But also additional code in TSX with coverage, plus a couple fixes)
  • Loading branch information
RoyEJohnson committed Jul 30, 2024
1 parent f8e912e commit cfa5fcb
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 390 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
"esbuild-loader": "^3.0.1",
"js-cookie": "^3.0.5",
"lodash": "^4.17.21",
"react": "npm:@preact/compat",
"react-dom": "npm:@preact/compat",
"react-accessible-accordion": "^4.0.0",
"react-aria": "^3.31.1",
"react-aria-carousel": "^0.1.0",
"react-aria-carousel": "^0.2.0",
"react-intl": "^6.3.2",
"react-lazyload": "^3.2.0",
"react-loadable": "^5.5.0",
Expand Down
10 changes: 5 additions & 5 deletions src/app/helpers/use-page-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,27 @@ async function fetchDataAndExpandImages(
return data;
}

export function fetchPageData(
export function fetchPageData<T>(
slug: string,
preserveWrapping: boolean,
noCamelCase: boolean
): Promise<Data> {
): Promise<T> {
const camelCaseOrNot = noCamelCase ? (obj: object) => obj : camelCaseKeys;

return fetchDataAndExpandImages(slug, preserveWrapping).then(
camelCaseOrNot
);
}

export default function usePageData(
export default function usePageData<T>(
slug: string,
preserveWrapping = false,
noCamelCase = false
) {
const [data, setData] = React.useState<Data>();
const [data, setData] = React.useState<T>();

React.useEffect(() => {
fetchPageData(slug, preserveWrapping, noCamelCase).then(setData);
fetchPageData<T>(slug, preserveWrapping, noCamelCase).then(setData);
}, [slug, preserveWrapping, noCamelCase]);

return data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import buildContext from '~/components/jsx-helpers/build-context';
import usePageData from '~/helpers/use-page-data';

type FeaturedInRollup = {
name: string;
url: string;
image: string;
};

// This will need more filling in as other components move to TSX
type PressPageData = {
meta: object;
mentions: {
date: string;
featuredIn: boolean;
headline: string;
source: {
id: number;
logo: string;
name: string;
};
url: string;
}[];
featuredIn: FeaturedInRollup[];
}

function useContextValue() {
const value = usePageData('press');
const value = usePageData<PressPageData>('press');

if (value) {
value.featuredIn = value.mentions.filter(
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/press/press-releases/press-releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MoreFewer from '~/components/more-fewer/more-fewer';
import {convertedDate, asDate, PressExcerpt} from '../helpers';
import './press-releases.scss';

export default function PressReleases({excludeSlug, Container = MoreFewer}) {
export default function PressReleases({excludeSlug='', Container = MoreFewer}) {
const pageData = usePageContext();

if (!pageData?.releases) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ function MainPage() {

useCanonicalLink(true);
React.useEffect(() => {
if (pageData) {
setPageTitleAndDescriptionFromBookData(pageData);
}
setPageTitleAndDescriptionFromBookData(pageData);
}, [pageData]);

return (
<React.Fragment>
<div>In the press</div>
<section id='in-the-press'>
<Banner />
</section>
Expand Down
7 changes: 6 additions & 1 deletion src/app/pages/subjects/new/specific/find-translation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import type {LocaleEntry} from '~/components/language-selector/language-selector
import useLanguageContext from '~/contexts/language';
import {useIntl} from 'react-intl';

type PageData = {
error?: string;
translations: LocaleEntry[][];
}

export default function FindTranslation() {
const subject = useParams().subject;
const pageData = usePageData(`pages/${subject}-books`);
const pageData = usePageData<PageData>(`pages/${subject}-books`);

if (!pageData) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/fetch-mocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ global.fetch = jest.fn().mockImplementation((...args) => {
const isOsNews = (/openstax-news/).test(args[0]);
const isPartner = (/pages\/partners/).test(args[0]);
const isPolishPhysics = (/fizyka/).test(args[0]);
const isPress = (/api\/press\/?$/).test(args[0]);
const isPressArticle = (/api\/press\/./).test(args[0]);
const isPress = (/api\/press\/\?/).test(args[0]);
const isPressArticle = (/api\/press\/[a-z]/).test(args[0]);
const isPrintOrder = (/pages\/print-order/).test(args[0]);
const isRenewal = args[0].includes('renewal?account_uuid');
const isRoles = (/snippets\/roles/).test(args[0]);
Expand Down
577 changes: 205 additions & 372 deletions test/src/data/press.js

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions test/src/pages/press/press.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react';
import {render, screen, waitFor} from '@testing-library/preact';
import {describe, it} from '@jest/globals';
import {MemoryRouter} from 'react-router-dom';
import Press from '~/pages/press/press';

const mockPathname = jest.fn();
const mockNavigate = jest.fn();
const mockCarousel = ({children}: React.PropsWithChildren<object>) => <div>{children}</div>;

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: () => ({
get pathname() {return mockPathname();}
}),
Navigate: () => mockNavigate()
}));
jest.mock('~/components/carousel/carousel', () => mockCarousel);

describe('press page', () => {
it('renders main press page', async () => {
mockPathname.mockReturnValueOnce('').mockReturnValueOnce('');
render(
<MemoryRouter initialEntries={['/']}>
<Press />
</MemoryRouter>
);
await screen.findByText('In the press');
await waitFor(() => expect(document.title).toMatch('- OpenStax'));
document.title = '';
});
it('renders press article page', async () => {
render(
<MemoryRouter initialEntries={['/article-slug']}>
<Press />
</MemoryRouter>
);
expect(await screen.findAllByRole('heading')).toHaveLength(2);
});
it('tries to Navigate on slash', async () => {
mockPathname.mockReturnValueOnce('/').mockReturnValueOnce('');
mockNavigate.mockReturnValue(<div>This is navigate</div>);
render(
<MemoryRouter initialEntries={['/']}>
<Press />
</MemoryRouter>
);
await screen.findByText('This is navigate');
});
});
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const config = {
react: 'preact/compat',
'react-dom/test-utils': 'preact/test-utils',
'react-dom': 'preact/compat',
'react/jsx-runtime': 'preact/jsx-runtime',
'~': path.resolve(__dirname, 'src/app')
},
modules: [path.resolve(__dirname, 'src/app'), 'node_modules'],
Expand Down
18 changes: 14 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10787,10 +10787,10 @@ react-accessible-accordion@^4.0.0:
resolved "https://registry.yarnpkg.com/react-accessible-accordion/-/react-accessible-accordion-4.0.0.tgz#83bf0c3e403e7627dbf3109c628b597142923937"
integrity sha512-MovuWj2Uweo57LSgTIPpB83IYq8BNdZJ44j4NmDKYxaHC/H0JjYiqt8OfNMt+YK+XN8qRON13ERQnLfM73vmqw==

react-aria-carousel@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/react-aria-carousel/-/react-aria-carousel-0.1.0.tgz#9aca5777c6552e2c63dec6f7eeff754d256f37b4"
integrity sha512-Jk9fVKa0Hd41iL/iGSxL4vH+KuhdTgQuAsxEIXQqJpG3yN1uQ96+/i83spetHOBGYWQ04Ub4f7On1wfKkuNCTA==
react-aria-carousel@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/react-aria-carousel/-/react-aria-carousel-0.2.0.tgz#7cde3bbbeda7d2d5e7acb01d43ac8901d97f76f3"
integrity sha512-XSQ2qnn7lVo0pE+3oR6nutoZEVpRsasEJ1uu3NAzV8i5Tf59HiM0xbqTrFjjcBuwGsQ8Ny9Lo+5/XrQf2cThRw==

react-aria@^3.31.1:
version "3.33.1"
Expand Down Expand Up @@ -10835,6 +10835,11 @@ react-aria@^3.31.1:
"@react-aria/visually-hidden" "^3.8.12"
"@react-types/shared" "^3.23.1"

"react-dom@npm:@preact/compat":
version "17.1.2"
resolved "https://registry.yarnpkg.com/@preact/compat/-/compat-17.1.2.tgz#928756713b07af6faf7812f6a56840d8ce6fed37"
integrity sha512-7pOZN9lMDDRQ+6aWvjwTp483KR8/zOpfS83wmOo3zfuLKdngS8/5RLbsFWzFZMGdYlotAhX980hJ75bjOHTwWg==

react-intl@^6.3.2:
version "6.6.8"
resolved "https://registry.yarnpkg.com/react-intl/-/react-intl-6.6.8.tgz#cb60c90502d0025caf9f86ec298cdc4348da17c2"
Expand Down Expand Up @@ -10952,6 +10957,11 @@ react-stately@^3.29.1:
"@react-stately/tree" "^3.8.1"
"@react-types/shared" "^3.23.1"

"react@npm:@preact/compat":
version "17.1.2"
resolved "https://registry.yarnpkg.com/@preact/compat/-/compat-17.1.2.tgz#928756713b07af6faf7812f6a56840d8ce6fed37"
integrity sha512-7pOZN9lMDDRQ+6aWvjwTp483KR8/zOpfS83wmOo3zfuLKdngS8/5RLbsFWzFZMGdYlotAhX980hJ75bjOHTwWg==

read-pkg-up@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-7.0.1.tgz#f3a6135758459733ae2b95638056e1854e7ef507"
Expand Down

0 comments on commit cfa5fcb

Please sign in to comment.