Skip to content
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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openstax/os-webview",
"version": "2.145.2",
"version": "2.146.2",
"description": "OpenStax webview",
"scripts": {
"test": "jest --coverage ./test/src",
Expand Down
8 changes: 7 additions & 1 deletion src/app/components/shell/router-helpers/use-link-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ export default function useLinkHandler() {
const navigate = useNavigate();
const navigateTo = useCallback(
(path: Location['href'], state: State = {x: 0, y: 0}) => {
navigate(linkHelper.stripOpenStaxDomain(path), state);
const stripped = linkHelper.stripOpenStaxDomain(path);

if (stripped.startsWith('http')) {
window.location.href = stripped;
} else {
navigate(stripped, state);
}
},
[navigate]
);
Expand Down
13 changes: 3 additions & 10 deletions src/app/layouts/default/header/sticky-note/sticky-note.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import React from 'react';
import RawHTML from '~/components/jsx-helpers/raw-html';
import {usePutAway} from '../../shared';
import {usePutAway, StickyDataWithBanner} from '../../shared';
import './sticky-note.scss';

export type StickyNoteProps = {
stickyData: {
mode: 'emergency';
emergency_content: string;
} | object | null;
};

export default function StickyNote({stickyData}: StickyNoteProps) {
export default function StickyNote({stickyData}: {stickyData: StickyDataWithBanner | null}) {
const [closed, PutAway] = usePutAway();

if (!stickyData || closed || !('emergency_content' in stickyData)) {
if (!stickyData || closed || stickyData.mode !== 'emergency') {
return null;
}

Expand Down
7 changes: 4 additions & 3 deletions src/app/layouts/default/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ type BannerInfo = {
type StickyDataRaw = {
start: string;
expires: string;
emergency_expires: string;
emergency_expires: string | null;
emergency_content: string;
show_popup: boolean;
};

type StickyDataWithBanner = StickyDataRaw & {
export type StickyDataWithBanner = StickyDataRaw & {
bannerInfo: BannerInfo;
mode: 'emergency' | 'popup' | 'banner' | null;
};
Expand Down Expand Up @@ -62,7 +63,7 @@ function getMode(stickyData: StickyDataRaw | null): 'emergency' | 'popup' | 'ban
return null;
}

const expireDate = new Date(stickyData.emergency_expires);
const expireDate = new Date(stickyData.emergency_expires ?? 0);
const useEmergency = stickyData.emergency_expires && Date.now() < expireDate.getTime();

if (useEmergency) {
Expand Down
45 changes: 44 additions & 1 deletion test/src/layouts/default/default.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import userEvent from '@testing-library/user-event';
import {
useSeenCounter,
usePutAway,
useStickyData
useStickyData,
StickyDataWithBanner
} from '~/layouts/default/shared';
import DefaultLayout from '~/layouts/default/default';
import StickyNote from '~/layouts/default/header/sticky-note/sticky-note';
import stickyData from './data/sticky.json';
import fundraiserData from './data/fundraiser.json';
import footerData from './data/footer.json';
Expand Down Expand Up @@ -226,6 +228,47 @@ describe('Layouts Default TypeScript Conversions', () => {
);
mockCmsFetch.mockImplementation(basicImplementation);
});
test('useStickyData hook handles null emergency_expires', async () => {
mockCmsFetch.mockImplementation((path: string) => {
if (path === 'sticky/') {
/* eslint-disable camelcase */
return Promise.resolve({
...stickyData,
emergency_expires: null
});
/* eslint-enable camelcase */
}
return basicImplementation(path);
});
const TestComponent = () => {
const data = useStickyData();

return (
<div data-testid="sticky-data">
{data && data.mode === 'banner'
? 'mode is banner'
: 'no data'}
</div>
);
};

render(<TestComponent />);
expect(await screen.findByTestId('sticky-data')).toHaveTextContent(
'mode is banner'
);
mockCmsFetch.mockImplementation(basicImplementation);
});
test('sticky-note (emergency) renders', () => {
const data = {
mode: 'emergency',
emergency_content: 'The message' // eslint-disable-line camelcase
} as StickyDataWithBanner;

render(<StickyNote stickyData={data} />);
const alert = screen.getByRole('alert');

expect(alert.textContent).toBe('The message');
});
});
});

Expand Down
Loading