Skip to content

Enable usage of createContext with independent mount #16997

@brunnerh

Description

@brunnerh

Describe the problem

@accuser made this observation here:

I like the simplicity of this approach, but it seems to take away the ability to pass context when mounting a component during testing. Or am I missing something obvious?

I used to do this:

mount(MyComponent, { props, context: { CONTEXT_TOKEN: mockContext }, target: document.body });

Now that the context key is opaque, I don't seem to have a way to do this.

Describe the proposed solution

Maybe the key should be exposed (e.g. on the setter) to enable something like:

mount(MyComponent, { context: { [setContext.key]: mockContext }, /* ... */ });

Or maybe a function for creating a context entry (key/value pair) could be provided, this way the type checking could still be automatically enforced:

mount(MyComponent, {
  context: new Map([
      setContext.entry(mockContext), // [{}, T]
      // ... other contexts, e.g. via `...Object.entries({ ... })`
  ]),
  // ...
});

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions