-
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.
Refactor: Upgrade testing library, react types and use React's "act" …
…instead of the deprecated act from the testing library
- Loading branch information
1 parent
c1ea03f
commit 1de3a67
Showing
6 changed files
with
190 additions
and
311 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,71 +1,69 @@ | ||
import React from 'react'; | ||
import React, { act } from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
import { act } from 'react-dom/test-utils'; | ||
import FontLoader from './FontLoader'; | ||
|
||
|
||
describe('FontLoader', () => { | ||
let container = null; | ||
let root = null; | ||
|
||
beforeEach(() => { | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
document.head.querySelectorAll('link, style').forEach(el => el.remove()); | ||
container.remove(); | ||
container = null; | ||
root.unmount(); | ||
}); | ||
let container = null; | ||
let root = null; | ||
|
||
beforeEach(() => { | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
it('renders without crashing', () => { | ||
act(() => { | ||
root = createRoot(container); | ||
root.render(<FontLoader />); | ||
afterEach(() => { | ||
document.head.querySelectorAll('link, style').forEach(el => el.remove()); | ||
container.remove(); | ||
container = null; | ||
root.unmount(); | ||
}); | ||
expect(container.innerHTML).toBe(''); | ||
}); | ||
|
||
it('loads the font and sets the global font style', async () => { | ||
const fontUrl = 'https://fonts.googleapis.com/css?family=Roboto'; | ||
const fontType = 'heading'; | ||
|
||
act(() => { | ||
root = createRoot(container); | ||
root.render(<FontLoader fontUrl={fontUrl} fontType={fontType} />); | ||
it('renders without crashing', () => { | ||
act(() => { | ||
root = createRoot(container); | ||
root.render(<FontLoader />); | ||
}); | ||
expect(container.innerHTML).toBe(''); | ||
}); | ||
|
||
const linkElement = document.querySelector('link[href="' + fontUrl + '"]'); | ||
const styleElement = document.querySelector('style'); | ||
it('loads the font and sets the global font style', async () => { | ||
const fontUrl = 'https://fonts.googleapis.com/css?family=Roboto'; | ||
const fontType = 'heading'; | ||
|
||
expect(linkElement).not.toBeNull(); | ||
expect(styleElement).not.toBeNull(); | ||
expect(styleElement.innerHTML).toContain('font-family: "Roboto", sans-serif;'); | ||
act(() => { | ||
root = createRoot(container); | ||
root.render(<FontLoader fontUrl={fontUrl} fontType={fontType} />); | ||
}); | ||
|
||
act(() => { | ||
root.unmount(); | ||
}); | ||
const linkElement = document.querySelector('link[href="' + fontUrl + '"]'); | ||
const styleElement = document.querySelector('style'); | ||
|
||
expect(linkElement).not.toBeNull(); | ||
expect(styleElement).not.toBeNull(); | ||
expect(styleElement.innerHTML).toContain('font-family: "Roboto", sans-serif;'); | ||
|
||
const removedLinkElement = document.querySelector('link[href="' + fontUrl + '"]'); | ||
const removedStyleElement = document.querySelector('style'); | ||
act(() => { | ||
root.unmount(); | ||
}); | ||
|
||
expect(removedLinkElement).toBeNull(); | ||
expect(removedStyleElement).toBeNull(); | ||
}); | ||
const removedLinkElement = document.querySelector('link[href="' + fontUrl + '"]'); | ||
const removedStyleElement = document.querySelector('style'); | ||
|
||
it('does not load the font if fontUrl is not provided', async () => { | ||
act(() => { | ||
root = createRoot(container); | ||
root.render(<FontLoader />); | ||
expect(removedLinkElement).toBeNull(); | ||
expect(removedStyleElement).toBeNull(); | ||
}); | ||
|
||
const linkElement = document.querySelector('link'); | ||
const styleElement = document.querySelector('style'); | ||
it('does not load the font if fontUrl is not provided', async () => { | ||
act(() => { | ||
root = createRoot(container); | ||
root.render(<FontLoader />); | ||
}); | ||
|
||
expect(linkElement).toBeNull(); | ||
expect(styleElement).toBeNull(); | ||
}); | ||
}); | ||
const linkElement = document.querySelector('link'); | ||
const styleElement = document.querySelector('style'); | ||
|
||
expect(linkElement).toBeNull(); | ||
expect(styleElement).toBeNull(); | ||
}); | ||
}); |
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