Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

context-thief

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

context-thief

Helper function to steal React context values for testing

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source
Context Thief icon

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

Keywords

FAQs

Package last updated on 21 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc