forked from andychase/gbajs2
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wrap all context access, more tests, mocking strategy
- not sure if im sold on how I'm mocking so far, but this is food for thought - seems like this may be the best way for me to interact with the global context in tests
- Loading branch information
Nick VanCise
authored and
Nick VanCise
committed
Jan 4, 2024
1 parent
ecdfdca
commit 3f12872
Showing
46 changed files
with
315 additions
and
204 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
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,54 @@ | ||
import { screen } from '@testing-library/react'; | ||
import { userEvent } from '@testing-library/user-event'; | ||
import { describe, expect, it, vi } from 'vitest'; | ||
|
||
import { AboutModal } from './about.tsx'; | ||
import { renderWithContext } from '../../../test/render-with-context.tsx'; | ||
import * as contextHooks from '../../hooks/context.tsx'; | ||
import { productTourLocalStorageKey } from '../product-tour/consts.tsx'; | ||
|
||
describe('<AboutModal>', () => { | ||
it('triggers product tour and closes modal when taking a tour', async () => { | ||
const setIsModalOpenSpy = vi.fn(); | ||
const setItemSpy = vi.spyOn(Storage.prototype, 'setItem'); | ||
const { useModalContext: original } = await vi.importActual< | ||
typeof contextHooks | ||
>('../../hooks/context.tsx'); | ||
|
||
vi.spyOn(contextHooks, 'useModalContext').mockImplementation(() => ({ | ||
...original(), | ||
setIsModalOpen: setIsModalOpenSpy | ||
})); | ||
|
||
renderWithContext(<AboutModal />); | ||
|
||
// click the close button | ||
const closeButton = screen.getByText('Take a tour', { selector: 'button' }); | ||
expect(closeButton).toBeInTheDocument(); | ||
await userEvent.click(closeButton); | ||
|
||
expect(setItemSpy).toHaveBeenCalledWith(productTourLocalStorageKey, '{}'); | ||
expect(setIsModalOpenSpy).toHaveBeenCalledWith(false); | ||
}); | ||
|
||
it('closes modal using the close button', async () => { | ||
const setIsModalOpenSpy = vi.fn(); | ||
const { useModalContext: original } = await vi.importActual< | ||
typeof contextHooks | ||
>('../../hooks/context.tsx'); | ||
|
||
vi.spyOn(contextHooks, 'useModalContext').mockImplementation(() => ({ | ||
...original(), | ||
setIsModalOpen: setIsModalOpenSpy | ||
})); | ||
|
||
renderWithContext(<AboutModal />); | ||
|
||
// click the close button | ||
const closeButton = screen.getByText('Close', { selector: 'button' }); | ||
expect(closeButton).toBeInTheDocument(); | ||
await userEvent.click(closeButton); | ||
|
||
expect(setIsModalOpenSpy).toHaveBeenCalledWith(false); | ||
}); | ||
}); |
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
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,38 @@ | ||
import { screen } from '@testing-library/react'; | ||
import { userEvent } from '@testing-library/user-event'; | ||
import { describe, expect, it, vi } from 'vitest'; | ||
|
||
import { renderWithContext } from '../../../test/render-with-context.tsx'; | ||
import * as contextHooks from '../../hooks/context.tsx'; | ||
import { LegalModal } from './legal.tsx'; | ||
|
||
describe('<LegalModal>', () => { | ||
it('closes modal using the close button', async () => { | ||
const setIsModalOpenSpy = vi.fn(); | ||
const { useModalContext: original } = await vi.importActual< | ||
typeof contextHooks | ||
>('../../hooks/context.tsx'); | ||
|
||
vi.spyOn(contextHooks, 'useModalContext').mockImplementation(() => ({ | ||
...original(), | ||
setIsModalOpen: setIsModalOpenSpy | ||
})); | ||
|
||
renderWithContext(<LegalModal />); | ||
|
||
// click the close button | ||
const closeButton = screen.getByText('Close', { selector: 'button' }); | ||
expect(closeButton).toBeInTheDocument(); | ||
await userEvent.click(closeButton); | ||
|
||
expect(setIsModalOpenSpy).toHaveBeenCalledWith(false); | ||
}); | ||
|
||
it('renders with current year in copywright', () => { | ||
vi.setSystemTime(new Date(2023, 0)); | ||
|
||
renderWithContext(<LegalModal />); | ||
|
||
expect(screen.getByText(/© 2023,/)).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.