-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ContentWarning * Test * Check cookie for content warning * Cookie test coverage * Fix proxy for OSWebLoader * Update fixture file * Use Cookies package * Update fixture file 2 * Fix cookie key * Fix cookie key; trap tab navigation * Add label to content warning dialog * Revert changes to Modal; add overlay * Fix Trap Tab --------- Co-authored-by: staxly[bot] <35789409+staxly[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4142fc1
commit 7fee251
Showing
11 changed files
with
160 additions
and
7 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
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,48 @@ | ||
import React from 'react'; | ||
import { renderToDom } from '../../../test/reactutils'; | ||
import ContentWarning from './ContentWarning'; | ||
import { useServices } from '../../context/Services'; | ||
import { OSWebBook } from '../../../gateways/createOSWebLoader'; | ||
import { BookWithOSWebData } from '../types'; | ||
import { act } from 'react-dom/test-utils'; | ||
import TestContainer from '../../../test/TestContainer'; | ||
import ReactTestUtils from 'react-dom/test-utils'; | ||
|
||
const dummyBook = ({ | ||
id: 'dummy-id', | ||
} as unknown) as BookWithOSWebData; | ||
|
||
const dummyBookInfo = ({ | ||
content_warning_text: 'some warning text', | ||
id: 72, | ||
} as unknown) as OSWebBook; | ||
|
||
const services = { | ||
osWebLoader: { | ||
getBookFromId: jest.fn(() => Promise.resolve(dummyBookInfo)), | ||
}, | ||
}; | ||
|
||
jest.mock('../../context/Services', () => ({ | ||
...jest.requireActual('../../context/Services'), | ||
useServices: jest.fn(), | ||
})); | ||
|
||
(useServices as jest.Mock).mockReturnValue(services); | ||
|
||
describe('ContentWarning', () => { | ||
it('renders warning modal', async() => { | ||
renderToDom(<TestContainer><ContentWarning book={dummyBook} /></TestContainer>); | ||
|
||
expect(services.osWebLoader.getBookFromId).toBeCalledWith(dummyBook.id); | ||
|
||
await act(() => new Promise((resolve) => setTimeout(resolve, 1))); | ||
|
||
const root = document?.body; | ||
const b = root?.querySelector('button'); | ||
|
||
expect(b).toBeTruthy(); | ||
act(() => ReactTestUtils.Simulate.click(b!)); | ||
expect(root?.querySelector('button')).toBeFalsy(); | ||
}); | ||
}); |
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,88 @@ | ||
import React from 'react'; | ||
import { useServices } from '../../context/Services'; | ||
import { OSWebBook } from '../../../gateways/createOSWebLoader'; | ||
import { Book } from '../types'; | ||
import { HTMLDivElement } from '@openstax/types/lib.dom'; | ||
import styled from 'styled-components/macro'; | ||
import Button from '../../components/Button'; | ||
import Modal from './Modal'; | ||
import theme from '../../theme'; | ||
import Cookies from 'js-cookie'; | ||
import { useTrapTabNavigation } from '../../reactUtils'; | ||
|
||
// tslint:disable-next-line | ||
const WarningDiv = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
gap: 4rem; | ||
justify-content: center; | ||
align-items: center; | ||
font-size: 1.8rem; | ||
padding: 2rem; | ||
min-height: 50vh; | ||
top: 25vh; | ||
z-index: 4; | ||
> div { | ||
max-width: 80rem; | ||
} | ||
`; | ||
|
||
function WarningDivWithTrap({ | ||
text, | ||
dismiss, | ||
}: { | ||
text: string; | ||
dismiss: () => void; | ||
}) { | ||
const ref = React.useRef<HTMLDivElement>(null); | ||
|
||
React.useEffect(() => ref.current?.focus(), []); | ||
|
||
useTrapTabNavigation(ref); | ||
|
||
return ( | ||
<WarningDiv tabIndex='-1' ref={ref}> | ||
<div>{text}</div> | ||
<Button type='button' onClick={dismiss}> | ||
Ok | ||
</Button> | ||
</WarningDiv> | ||
); | ||
} | ||
|
||
export default function ContentWarning({ book }: { book: Book }) { | ||
const services = useServices(); | ||
const [bookInfo, setBookInfo] = React.useState<OSWebBook | undefined>(); | ||
const cookieKey = `content-warning-${bookInfo?.id}`; | ||
const dismiss = React.useCallback(() => { | ||
// This is only called when bookInfo is populated | ||
Cookies.set(cookieKey, 'true', { expires: 28 }); | ||
setBookInfo(undefined); | ||
}, [cookieKey]); | ||
|
||
React.useEffect(() => { | ||
services.osWebLoader.getBookFromId(book.id).then(setBookInfo); | ||
}, [book, services]); | ||
|
||
if (!bookInfo?.content_warning_text || Cookies.get(cookieKey)) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Modal | ||
ariaLabel='Content warning' | ||
tabIndex='-1' | ||
scrollLockProps={{ | ||
mediumScreensOnly: false, | ||
overlay: true, | ||
zIndex: theme.zIndex.highlightSummaryPopup, | ||
}} | ||
> | ||
<WarningDivWithTrap | ||
text={bookInfo.content_warning_text} | ||
dismiss={dismiss} | ||
/> | ||
</Modal> | ||
); | ||
} |
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
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
File renamed without changes.
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