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

@depack/context

v1.1.2

Published

The Test Context To Render JSX Into Strings For Mask Testing With Zoroaster.

Downloads

29

Readme

@depack/context

npm version

@depack/context is The Test Context To Render JSX Into Strings For Mask Testing With Zoroaster.

yarn add -E @depack/context

Table Of Contents

API

The package is available by importing its default function:

import JSXContext from '@depack/context'

class JSXContext

This instance of the test context provides the testing API for JSX components.

getVNode(  input: string,  context?: *,): Preact.VNode

Transforms the string input into JSX VNode. The global variables and references can be passed in the context.

/* yarn example/ */
import JSXContext from '@depack/context'

const Container = ({ children }) => (<body>{
  children
}</body>)

const context = new JSXContext()
const s = context.getVNode(
  `
<Container>
  <div id="id" className="Class" required>
    <span>Example</span>
  </div>
</Container>`,
  { Container },
)
VNode {
  nodeName: [Function: Container],
  children: 
   [ VNode {
       nodeName: 'div',
       children: 
        [ VNode {
            nodeName: 'span',
            children: [ 'Example' ],
            attributes: {},
            key: undefined } ],
       attributes: 
        { id: 'id',
          className: 'Class',
          required: '' },
       key: undefined } ],
  attributes: {},
  key: undefined }

render(  vnode: VNode,  opts?: RenderConfig,  contexts?: {},): string

Renders the JSX into the string.

import('preact').VNode VNode

RenderConfig: Rendering options.

| Name | Type | Description | Default | | ---------- | --------- | ----------------------------------------------------------------------------------------------------------------- | ------- | | addDoctype | boolean | Adds the <!doctype html> at the beginning of the return string. | false | | shallow | boolean | If true, renders nested Components as HTML elements (<Foo a="b" />). | false | | xml | boolean | If true, uses self-closing tags for elements without children. | false | | pretty | boolean | If true, adds whitespace for readability. Pass a string to indicate the indentation character, e.g., \t. | false | | lineLength | number | The number of characters on one line above which the line should be split in the pretty mode. | 40 |

import JSXContext from '@depack/context'

const context = new JSXContext()
const s = context.render(
  <div id="id" className="Class" required>
    <span>Example</span>
  </div>,
  { pretty: true })
console.log(s)
<div id="id" class="Class" required>
  <span>Example</span>
</div>

Using In A Test

The test context is made for mask-testing of the applications. A mask test will have the setup script called test/mask/default.js that points to the result mask in its body.

import { makeTestSuite } from 'zoroaster'
import JSXContext from '@depack/context'
import render from '../../src'

export default makeTestSuite('test/result/index.html', {
  /**
   * @param {string} input
   * @param {JSXContext} c
   */
  getResults(input, { getVNode }) {
    const test = getVNode(input)
    const res = render(test)
    return res
  },
  context: [JSXContext],
})

The actual mappings of inputs to outputs are put in the mask test/result/index.html:

// returns the correct output
<div className="test">
  <span>Hello</span> <a href="#">World</a>
</div>

/* expected */
<div class="test"><span>Hello</span> <a href="#">World</a></div>
/**/

Each mask setup will pass its properties and point to other input files:

export const pretty = makeTestSuite('test/result/pretty.html', {
  /**
   * @param {string} input
   * @param {JSXContext} c
   */
  getResults(input, { getVNode }) {
    const test = getVNode(input)
    const res = render(test, { pretty: true })
    return res
  },
  context: [JSXContext],
})

For example, the pretty-printing adds the additional attribute and points to the results at test/result/pretty.html. The file extension just enables syntax highlighting in those files so it's easier on the eye.

// returns the correct output
<div className="test" required data-attr="render" data-test id="i500" style="display:block;" >
  <span className="test" required data-attr="render" data-test id="i501" style="display:block;">
    Hello World
  </span>
  <textarea>
    Example Textarea That Has Large Input According To The Preact Logic. We must not new line this value.
  </textarea>
</div>

/* expected */
<div class="test" required
  data-attr="render" data-test id="i500"
  style="display:block;">
  <span class="test" required
    data-attr="render" data-test id="i501"
    style="display:block;">
    Hello World
  </span>
  <textarea>Example Textarea That Has Large Input According To The Preact Logic. We must not new line this value.</textarea>
</div>
/**/

// prettifies dangerously set inner html
<small id="hi70984" class="form-text text-muted" dangerouslySetInnerHTML={{__html: 'Your name, your name, what is your name?'}}></small>

/* expected */
<small id="hi70984"
  class="form-text text-muted">
  Your name, your name, what is your name?
</small>
/**/

Copyright