Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vadimdemedes/ink-testing-library
Utilities for testing Ink apps
https://github.com/vadimdemedes/ink-testing-library
Last synced: about 1 month ago
JSON representation
Utilities for testing Ink apps
- Host: GitHub
- URL: https://github.com/vadimdemedes/ink-testing-library
- Owner: vadimdemedes
- License: mit
- Created: 2019-03-06T05:56:00.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-06-28T23:54:38.000Z (6 months ago)
- Last Synced: 2024-10-29T22:38:34.103Z (about 1 month ago)
- Language: TypeScript
- Size: 19.5 KB
- Stars: 109
- Watchers: 5
- Forks: 17
- Open Issues: 8
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- awesome-list - ink-testing-library
- stars - ink-testing-library
- stars - ink-testing-library
README
# ink-testing-library ![test](https://github.com/vadimdemedes/ink-testing-library/workflows/test/badge.svg)
> Utilities for testing [Ink](https://github.com/vadimdemedes/ink) apps
## Install
```
$ npm install --save-dev ink-testing-library
```## Usage
```jsx
import React from 'react';
import {Text} from 'ink';
import {render} from 'ink-testing-library';const Counter = ({count}) => Count: {count};
const {lastFrame, rerender} = render();
lastFrame() === 'Count: 0'; //=> truererender();
lastFrame() === 'Count: 1'; //=> true
```## API
### render(tree)
#### tree
Type: `ReactElement`
React component to render.
```jsx
render();
```This function returns an object, which contains the following methods and properties.
#### lastFrame()
Type: `function`
Shortcut to [`stdout.lastFrame`](#lastframe-1).
#### frames
Type: `array`
Shortcut to [`stdout.frames`](#frames-1).
#### rerender(tree)
Type: `function`
##### tree
Type: `ReactElement`
Rerender root component with different props or replace with another component.
```jsx
const {rerender} = render();
rerender();
```#### unmount()
Type: `function`
Unmount current component.
```jsx
const {unmount} = render();
unmount();
```#### stdin
Type: `object`
##### write()
Type: `function`
Write data to current component's stdin stream.
```jsx
import {useInput, Text} from 'ink';const Test = () => {
useInput(input => {
console.log(input);
//=> 'hello'
});return …;
};const {stdin} = render();
stdin.write('hello');
```#### stdout
Type: `object`
##### lastFrame()
Type: `function`
Return the last rendered frame (output) from stdout stream.
```jsx
const Test = () => Hello;const {stdout} = render();
stdout.lastFrame(); //=> 'Hello'
```##### frames
Type: `array`
Array of all rendered frames, where the last frame is also the last item in that array.
```jsx
const Counter = ({count}) => Count: {count};const {stdout, rerender} = render();
rerender();console.log(stdout.frames); //=> ['Count: 0', 'Count: 1']
```#### stderr
Type: `object`
##### lastFrame()
Type: `function`
Same as [`lastFrame`](#lastframe-1) in [`stdout`](#stdout), but for stderr stream.
##### frames
Type: `array`
Same as [`frames`](#frames-1) in [`stdout`](#stdout), but for stderr stream.