Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrade to react 18, refactor tests (#335)
* upgrade react to 18 * remove deprecated method * upgrade to v6 * temporarily remove markdown loader * rewrite tests * switch out markdown loading * update mocks * bump gh-pages * remove unused package * expect assertions * move dependencies into dev dependencies
- Loading branch information
Showing
16 changed files
with
2,793 additions
and
4,028 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,6 @@ | ||
module.exports = { | ||
presets: [ | ||
'@babel/preset-env', | ||
['@babel/preset-react', { runtime: 'automatic' }], | ||
], | ||
}; |
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,8 @@ | ||
const config = { | ||
moduleNameMapper: { | ||
'^.+\\.(css|less|scss)$': 'babel-jest', | ||
'^.+\\.md$': 'markdown-to-jsx', | ||
}, | ||
}; | ||
|
||
module.exports = config; |
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
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,107 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import '@testing-library/jest-dom'; | ||
import '@testing-library/react'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import { act } from 'react-dom/test-utils'; | ||
import App from '../App'; | ||
|
||
describe('renders the app', () => { | ||
// mocks the fetch API used on the stats page and the about page. | ||
const jsonMock = jest.fn(() => Promise.resolve({})); | ||
const textMock = jest.fn(() => Promise.resolve('')); | ||
global.fetch = jest.fn(() => Promise.resolve({ | ||
json: jsonMock, | ||
text: textMock, | ||
})); | ||
// mocks the scrollTo API used when navigating to a new page. | ||
window.scrollTo = jest.fn(); | ||
|
||
let container; | ||
|
||
beforeEach(async () => { | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
await act(async () => { | ||
await ReactDOM.createRoot(container).render(<App />); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(container); | ||
container = null; | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should render the app', async () => { | ||
expect(document.body).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render the title', async () => { | ||
expect(document.title).toBe("Michael D'Angelo"); | ||
}); | ||
|
||
it('can navigate to /about', async () => { | ||
expect.assertions(7); | ||
const aboutLink = document.querySelector('#header > nav > ul > li:nth-child(1) > a'); | ||
expect(aboutLink).toBeInTheDocument(); | ||
await act(async () => { | ||
await aboutLink.click(); | ||
}); | ||
expect(document.title).toContain('About |'); | ||
expect(window.location.pathname).toBe('/about'); | ||
expect(window.scrollTo).toHaveBeenNthCalledWith(1, 0, 0); | ||
expect(global.fetch).toHaveBeenCalledTimes(1); | ||
expect(jsonMock).toHaveBeenCalledTimes(0); | ||
expect(textMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can navigate to /resume', async () => { | ||
expect.assertions(3); | ||
const contactLink = document.querySelector('#header > nav > ul > li:nth-child(2) > a'); | ||
expect(contactLink).toBeInTheDocument(); | ||
await act(async () => { | ||
await contactLink.click(); | ||
}); | ||
expect(document.title).toContain('Resume |'); | ||
expect(window.location.pathname).toBe('/resume'); | ||
}); | ||
|
||
it('can navigate to /projects', async () => { | ||
expect.assertions(3); | ||
const contactLink = document.querySelector('#header > nav > ul > li:nth-child(3) > a'); | ||
expect(contactLink).toBeInTheDocument(); | ||
await act(async () => { | ||
await contactLink.click(); | ||
}); | ||
expect(document.title).toContain('Projects |'); | ||
expect(window.location.pathname).toBe('/projects'); | ||
}); | ||
|
||
it('can navigate to /stats', async () => { | ||
expect.assertions(5); | ||
const contactLink = document.querySelector('#header > nav > ul > li:nth-child(4) > a'); | ||
expect(contactLink).toBeInTheDocument(); | ||
await act(async () => { | ||
await contactLink.click(); | ||
}); | ||
expect(document.title).toContain('Stats |'); | ||
expect(window.location.pathname).toBe('/stats'); | ||
expect(global.fetch).toHaveBeenCalledTimes(1); | ||
expect(jsonMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can navigate to /contact', async () => { | ||
expect.assertions(3); | ||
const contactLink = document.querySelector('#header > nav > ul > li:nth-child(5) > a'); | ||
expect(contactLink).toBeInTheDocument(); | ||
await act(async () => { | ||
await contactLink.click(); | ||
}); | ||
expect(document.title).toContain('Contact |'); | ||
expect(window.location.pathname).toBe('/contact'); | ||
}); | ||
}); |
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,41 +1,43 @@ | ||
import React from 'react'; | ||
import React, { useState, useEffect } from 'react'; | ||
import { Link } from 'react-router-dom'; | ||
import ReactMarkdown from 'react-markdown'; | ||
import raw from 'raw.macro'; | ||
import Markdown from 'markdown-to-jsx'; | ||
|
||
import Main from '../layouts/Main'; | ||
|
||
// uses babel to load contents of file | ||
const markdown = raw('../data/about.md'); | ||
const About = () => { | ||
const [markdown, setMarkdown] = useState(''); | ||
|
||
const count = markdown.split(/\s+/) | ||
.map((s) => s.replace(/\W/g, '')) | ||
.filter((s) => s.length).length; | ||
useEffect(() => { | ||
import('../data/about.md') | ||
.then((res) => { | ||
fetch(res.default) | ||
.then((r) => r.text()) | ||
.then(setMarkdown); | ||
}).catch(console.error); | ||
}); | ||
|
||
// Make all hrefs react router links | ||
const LinkRenderer = ({ ...children }) => <Link {...children} />; | ||
const count = markdown.split(/\s+/) | ||
.map((s) => s.replace(/\W/g, '')) | ||
.filter((s) => s.length).length; | ||
|
||
const About = () => ( | ||
<Main | ||
title="About" | ||
description="Learn about Michael D'Angelo" | ||
> | ||
<article className="post markdown" id="about"> | ||
<header> | ||
<div className="title"> | ||
<h2 data-testid="heading"><Link to="/about">About Me</Link></h2> | ||
<p>(in about {count} words)</p> | ||
</div> | ||
</header> | ||
<ReactMarkdown | ||
source={markdown} | ||
renderers={{ | ||
Link: LinkRenderer, | ||
}} | ||
escapeHtml={false} | ||
/> | ||
</article> | ||
</Main> | ||
); | ||
return ( | ||
<Main | ||
title="About" | ||
description="Learn about Michael D'Angelo" | ||
> | ||
<article className="post markdown" id="about"> | ||
<header> | ||
<div className="title"> | ||
<h2><Link to="/about">About Me</Link></h2> | ||
<p>(in about {count} words)</p> | ||
</div> | ||
</header> | ||
<Markdown> | ||
{markdown} | ||
</Markdown> | ||
</article> | ||
</Main> | ||
); | ||
}; | ||
|
||
export default About; |
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
Oops, something went wrong.