1.1.0 • Published 2 years ago

context-thief v1.1.0

Weekly downloads
56
License
MIT
Repository
github
Last release
2 years ago

Context Thief

Helper function to steal React context values for testing

npm version check status license: MIT

Description

Context Thief stores the current context value for the inputted context for use in tests.

If you test components with contexts and don't want to create a new function or component every time, this module is for you!

Installation

npm install --save-dev context-thief

Usage

createContextThief(context)

Returns: {ContextThief, contextValue} Object containing the component to render and the outputted value

ParameterTypeDescription
contextContextInstance of React.Context to consume

Example

example-component.tsx

import React from "react";

export const ExampleContext = React.createContext(false);
export const ExampleComponent: React.FC<{children: React.ReactNode}> = ({children}) => {
    return (
        <TestContext.Provider value={true}>
            {children}
        </TestContext.Provider>
    );
};

example-component.test.tsx

import React from "react";
import {render} from "@testing-library/react";
import {createContextThief} from "context-thief";
import {ExampleContext, ExampleComponent} from "./example-component";

it("should have the default current context value", () => {
    const {ContextThief, contextValue} = createContextThief(ExampleContext);

    render(
        <ExampleComponent>
            <ContextThief />
        </ExampleComponent>
    );

    expect(contextValue.current).toBe(true);
});

License

Copyright Evelyn Hathaway, MIT License

Icon Glyph

Icon glyph made by Gregor Cresnar

1.1.0

2 years ago

1.0.2

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago