withTheme
function in meocord/testing Since 4.1.0
withTheme<T>(theme: DeepReadonly<MeoCordTheme> | RootTheme, fn: () => T): TRuns fn with theme as the theme of the call, as a handler's call runs.
Use it for a service or presenter tested without a testing module: useTheme() in fn, and in everything it awaits
or starts, reads theme.
Parameters
| Name | Type | Description |
|---|---|---|
theme | DeepReadonly<MeoCordTheme> | RootTheme | A theme |
fn | () => T | The code to run. |
Returns
TWhat fn returns, a promise included.
Throws
Error naming each token that is not valid.
Examples
import { expect } from 'vitest'
const saved = (what: string) => `${useTheme().emojis.success} ${what} saved`
expect(withTheme({ emojis: { success: '🎉' } }, () => saved('Profile'))).toBe('🎉 Profile saved')