Skip to content
GitHub

withTheme

function in meocord/testing Since 4.1.0

withTheme<T>(theme: DeepReadonly<MeoCordTheme> | RootTheme, fn: () => T): T

Runs 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

NameTypeDescription
themeDeepReadonly<MeoCordTheme> | RootTheme

A theme createMockTheme made, used as it is, or the roles to change, merged over the defaults as createMockTheme merges them.

fn() => T

The code to run.

Returns

T

What fn returns, a promise included.

Throws

  • Error naming each token that is not valid.

Examples

TypeScript
import { expect } from 'vitest'

const saved = (what: string) => `${useTheme().emojis.success} ${what} saved`
expect(withTheme({ emojis: { success: '🎉' } }, () => saved('Profile'))).toBe('🎉 Profile saved')