-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e764322
commit 00bfcc4
Showing
9 changed files
with
298 additions
and
209 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
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,25 +1,77 @@ | ||
import * as React from 'react' | ||
import { afterEach, describe, expect, test } from 'vitest' | ||
import { cleanup, render } from '@testing-library/react' | ||
import { cleanup, render, screen, waitFor } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
|
||
import '@testing-library/jest-dom' | ||
import MetaContextProvider from './meta-tags-context' | ||
import MetaTags from './meta-tags' | ||
|
||
const MockRouter = (): React.ReactNode => { | ||
const [showRoute, setShowRoute] = React.useState(false) | ||
|
||
return ( | ||
<MetaContextProvider> | ||
<button onClick={() => setShowRoute((st) => !st)}>Toggle route</button> | ||
{showRoute ? ( | ||
<MetaTags> | ||
<title>Updated title</title> | ||
</MetaTags> | ||
) : ( | ||
<MetaTags> | ||
<title>Tuono</title> | ||
</MetaTags> | ||
)} | ||
</MetaContextProvider> | ||
) | ||
} | ||
|
||
describe('Should correctly render the head tags', () => { | ||
afterEach(() => { | ||
cleanup() | ||
}) | ||
|
||
test('It should correctly render the head element', async () => { | ||
await React.act(async () => { | ||
render( | ||
<MetaContextProvider> | ||
<MetaTags> | ||
<title>Tuono</title> | ||
</MetaTags> | ||
</MetaContextProvider>, | ||
) | ||
}) | ||
render( | ||
<MetaContextProvider> | ||
<MetaTags> | ||
<title>Tuono</title> | ||
</MetaTags> | ||
</MetaContextProvider>, | ||
) | ||
expect(document.title).toEqual('Tuono') | ||
}) | ||
|
||
test('It should remove the properties when unmount', async () => { | ||
const { unmount } = render( | ||
<MetaContextProvider> | ||
<MetaTags> | ||
<title>Tuono</title> | ||
</MetaTags> | ||
</MetaContextProvider>, | ||
) | ||
expect(document.title).toEqual('Tuono') | ||
|
||
unmount() | ||
|
||
expect(document.title).toEqual('') | ||
}) | ||
|
||
test('It should update the existing values with the newly mounted', async () => { | ||
const user = userEvent.setup() | ||
render(<MockRouter />) | ||
|
||
expect(document.title).toEqual('Tuono') | ||
|
||
await user.click(screen.getByText('Toggle route')) | ||
await waitFor(() => { | ||
expect(document.title).toEqual('Updated title') | ||
}) | ||
|
||
await user.click(screen.getByText('Toggle route')) | ||
|
||
await waitFor(() => { | ||
expect(document.title).toEqual('Tuono') | ||
}) | ||
}) | ||
}) |
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.