npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-ioc-widgets-test-renderer

v1.0.5

Published

Test harness for widgets

Downloads

3

Readme

React IoC Widgets Test Renderer

This library works alongside React IoC Widgets to create an easy to use renderer based on react-testing-library that makes writing unit tests for your components straightforward.

Installation

npm install --save-dev react-ioc-widgets-test-renderer

Writing a test

You’ll have built a module or two that add editors or layouts perhaps something like this:

example-module.js

import React from "react"
import {useLayout, useDesign, useTabs} from "react-ioc-widgets"

function TestRender() {
    const [design] = useDesign()
    return <div>{design.title}</div>
}

function TestEditor() {
    const [design, update] = useDesign()
    return <input data-testid="input" value={design.title} onChange={e=>update({title: e.target.value})}/>
}

export default function (widgets) {
    widgets.configure("test", ()=>{
        useLayout({content: [TestRender]})
    })
    widgets.editor("test", ()=>{
        useTabs(TestEditor)
    })
}

Now you want to write a unit test to make sure that the renderer renders and the editor updates.

The React IoC Widgets Test Renderer makes this simple by providing a single call that will render a Widgets component and editor using react-testing-library and provide a couple of extra properties on the return to enable you to interact with it.

Firstly you can pass in some special parameters to the render call:

  • modules - an array of functions exported from standard modules to bind to a widget event emitter. You can just bind everything to globalWidgets and omit this, it depends on how you’ve built your module.
  • design - a design document, the test renderer will provide an empty one if you don’t supply a value
  • document - a document, the test renderer will provide an empty one if you don’t supply a value
  • type - a type of the document

After the render finished you will have an object result that contains a number of properties in addition to the ones provided by react-testing-library (getByText, getByTestId, asFragment etc).

  • design - the design used for the widgets
  • document - the document used for the widgets
  • bag - the bag used for the component
  • setEditMode(bool) - a function used to turn edit mode on and off
  • selectEditorTab(tabTitleOrName) - a function used to select an editor tab
  • widgets - a WidgetEvents emitter to interact with the rendered modules

So now some tests for your module might look like this: example-module.test.js

import testModule from "./example-module"
import {render, fireEvent} from "react-ioc-widgets-test-renderer"

describe("Example Module", ()=>{

    it("Should render into the document", ()=>{
        const {getByText} = render({modules:[testModule], design: {title: "test"}, type: "test"})
        getByText("test")
    })

    it("should have an editor which updates the rendering", ()=>{
        const {getByText, setEditMode, getByTestId, selectEditorTab, asFragment} = render({modules: [testModule], design: {title: "testing"}, type: "test"})
        selectEditorTab("general")
        fireEvent.change(getByTestId("input"), {target: {value: "updated"}})
        setEditMode(false)
        expect(getByText("updated").nodeName).toBe("DIV")
        expect(asFragment()).toMatchSnapshot()
    })
})

Writing a test for a module that uses globalWidgets

example-global.js

import React from "react"
import {globalWidgets, useLayout} from "react-ioc-widgets"

function TestRenderer() {
    return <div>Test Global 1</div>
}

globalWidgets.configure("test", function() {
    useLayout({content: [TestRenderer]})
})

Now you can write a test like this:

import {render} from "react-ioc-test-renderer"
import "./example-global"

it("should be able to render global widgets", function() {
    const {getByText} = render({type: "test"})
    getByText("Test Global 1")
})