-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix-first-last-initial
- Loading branch information
Showing
3 changed files
with
69 additions
and
20 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,28 +1,74 @@ | ||
import React from 'react'; | ||
import { renderToDom } from '../../../test/reactutils'; | ||
import ContentWarning from './ContentWarning'; | ||
import { act } from 'react-dom/test-utils'; | ||
import ReactTestUtils from 'react-dom/test-utils'; | ||
import { book as archiveBook } from '../../../test/mocks/archiveLoader'; | ||
import type rendererType from 'react-test-renderer'; | ||
import type { ComponentType } from 'react'; | ||
import { mockCmsBook } from '../../../test/mocks/osWebLoader'; | ||
import { reactAndFriends, resetModules } from '../../../test/utils'; | ||
import { formatBookData } from '../utils'; | ||
|
||
|
||
const dummyBook = { | ||
...formatBookData(archiveBook, mockCmsBook), | ||
content_warning_text: 'some warning text', | ||
}; | ||
|
||
describe('ContentWarning', () => { | ||
it('renders warning modal', async() => { | ||
renderToDom(<ContentWarning book={dummyBook} />); | ||
let React: ReturnType<typeof reactAndFriends>['React']; // tslint:disable-line:variable-name | ||
let ContentWarningDynamic: ComponentType<any>; // tslint:disable-line:variable-name | ||
|
||
describe('in browser', () => { | ||
let renderToDom: ReturnType<typeof reactAndFriends>['renderToDom']; | ||
let ReactDOMTestUtils: ReturnType<typeof reactAndFriends>['ReactDOMTestUtils']; // tslint:disable-line:variable-name | ||
|
||
beforeEach(() => { | ||
resetModules(); | ||
jest.doMock('react', () => { | ||
const react = (jest as any).requireActual('react'); | ||
return { ...react, useEffect: react.useLayoutEffect }; | ||
}); | ||
|
||
({React, renderToDom, ReactDOMTestUtils} = reactAndFriends()); | ||
|
||
ContentWarningDynamic = require('./ContentWarning').default; | ||
}); | ||
|
||
it('renders warning modal and hides it after clicking', async() => { | ||
renderToDom(<ContentWarningDynamic book={dummyBook} />); | ||
|
||
const b = document!.querySelector('button'); | ||
|
||
expect(b).toBeTruthy(); | ||
// Exercises the when-focus-is-already-in-the-modal branch | ||
b!.focus(); | ||
|
||
ReactDOMTestUtils.act(() => ReactDOMTestUtils.Simulate.click(b!)); | ||
|
||
expect(document!.querySelector('button')).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('outside the browser', () => { | ||
const windowBackup = window; | ||
const documentBackup = document; | ||
|
||
let renderer: typeof rendererType; | ||
|
||
beforeEach(() => { | ||
delete (global as any).window; | ||
delete (global as any).document; | ||
resetModules(); | ||
({React, renderer} = reactAndFriends()); | ||
|
||
ContentWarningDynamic = require('./ContentWarning').default; | ||
}); | ||
|
||
const root = document?.body; | ||
const b = root?.querySelector('button'); | ||
afterEach(() => { | ||
(global as any).window = windowBackup; | ||
(global as any).document = documentBackup; | ||
}); | ||
|
||
expect(b).toBeTruthy(); | ||
// Exercises the when-focus-is-already-in-the-modal branch | ||
b!.focus(); | ||
act(() => ReactTestUtils.Simulate.click(b!)); | ||
expect(root?.querySelector('button')).toBeFalsy(); | ||
it('mounts and unmounts without a dom', () => { | ||
const component = renderer.create(<ContentWarningDynamic book={dummyBook} />); | ||
expect(() => component.unmount()).not.toThrow(); | ||
}); | ||
}); | ||
}); |
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