Code Monkey home page Code Monkey logo

ink-testing-library's Introduction

ink-testing-library test

Utilities for testing Ink apps

Install

$ npm install --save-dev ink-testing-library

Usage

import React from 'react';
import {Text} from 'ink';
import {render} from 'ink-testing-library';

const Counter = ({count}) => <Text>Count: {count}</Text>;

const {lastFrame, rerender} = render(<Counter count={0}/>);
lastFrame() === 'Count: 0'; //=> true

rerender(<Counter count={1}/>);
lastFrame() === 'Count: 1'; //=> true

API

render(tree)

tree

Type: ReactElement

React component to render.

render(<MyApp/>);

This function returns an object, which contains the following methods and properties.

lastFrame()

Type: function

Shortcut to stdout.lastFrame.

frames

Type: array

Shortcut to stdout.frames.

rerender(tree)

Type: function

tree

Type: ReactElement

Rerender root component with different props or replace with another component.

const {rerender} = render(<OldApp/>);
rerender(<NewApp/>);

unmount()

Type: function

Unmount current component.

const {unmount} = render(<Test/>);
unmount();

stdin

Type: object

write()

Type: function

Write data to current component's stdin stream.

import {useInput, Text} from 'ink';

const Test = () => {
	useInput(input => {
		console.log(input);
		//=> 'hello'
	});

	return ;
};

const {stdin} = render(<Test/>);
stdin.write('hello');

stdout

Type: object

lastFrame()

Type: function

Return the last rendered frame (output) from stdout stream.

const Test = () => <Text>Hello</Text>;

const {stdout} = render(<Test/>);
stdout.lastFrame(); //=> 'Hello'
frames

Type: array

Array of all rendered frames, where the last frame is also the last item in that array.

const Counter = ({count}) => <Text>Count: {count}</Text>;

const {stdout, rerender} = render(<Counter count={0}/>);
rerender(<Counter count={1}/>);

console.log(stdout.frames); //=> ['Count: 0', 'Count: 1']

stderr

Type: object

lastFrame()

Type: function

Same as lastFrame in stdout, but for stderr stream.

frames

Type: array

Same as frames in stdout, but for stderr stream.

ink-testing-library's People

Contributors

vadimdemedes avatar maadhattah avatar amitdahan avatar g-rath avatar omrimn avatar threepointone avatar isaacs avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.