forked from viamrobotics/prime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test: add
createToastContext
fixture (viamrobotics#550)
- Loading branch information
Showing
4 changed files
with
86 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** prime-core test fixtures. */ | ||
import { readable, writable } from 'svelte/store'; | ||
import { noop } from 'lodash-es'; | ||
|
||
// NOTE: imported directly from `$lib/toast/context` to avoid | ||
// exporting implementation details in `$lib/toast/index` | ||
import { | ||
ToastContextKey, | ||
type ToastContext, | ||
type Toast, | ||
} from '$lib/toast/context'; | ||
|
||
/** | ||
* Create a no-op toast context. | ||
* | ||
* @param toast - The `toast` function to return from `useToast` (defaults to `noop`) | ||
* | ||
* @example | ||
* ```ts | ||
* import { vi } from 'vitest'; | ||
* import { render } from '@testing-library/svelte'; | ||
* | ||
* import { createNoopToastContext } from '@viamrobotics/prime-core/__fixtures__'; | ||
* | ||
* import Subject from '../cool-component.svelte'; | ||
* | ||
* const toast = vi.fn(); | ||
* const renderSubject = () => { | ||
* const toastContext = createNoopToastContext(toast) | ||
* return render(Subject, { props: {}, context: new Map([toastContext]) }); | ||
* } | ||
* ``` | ||
*/ | ||
export const createNoopToastContext = (toast?: Toast): [unknown, unknown] => [ | ||
ToastContextKey, | ||
{ | ||
state: { toasts: readable([]), pageIsVisible: writable(false) }, | ||
toast: toast ?? noop, | ||
} satisfies ToastContext, | ||
]; |
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