Terminals test utils

This package presents a list of functions that you can use to simplify testing terminals' functionality.

Function

Description

createTerminal

Creates a web terminal

renderTerminal

Renders a web terminal with a SOM as default content

textshot

Generates a visual representation of the terminal's content

writeChars

Simulates the action of typing characters in the terminal

focus

Focuses a web terminal

blur

Removes the focus from a web terminal

sendKey

Sends a key to a web terminal

moveCursor

Moves the cursor to a specified location in the terminal

select

Selects the specified lines and columns of a web terminal

mouseDown

Emulates the mouse down event

clickAt

Moves the cursor position with a click

copy

Copies the selected text

paste

Pastes the text into the terminal

mockResponse

Simulates a terminal response

Installation

This library is packed as a part of the @orion/terminals module. You only need to import like: import { test-utils you need } from @orion/terminals/test-utils on your tests in order to be able to use them.

createTerminal

Creates a terminal for the current test. It does not renders the created terminal.

Usage
Copy
createTerminal({ terminalId?: string, maxColumns?: number, viewportHeight?: number }): void
  • terminalId: String [OPTIONAL]

    Default "terminal1"; identifier for the new terminal

  • maxColumns: Number [OPTIONAL]

    Change the default max columns for testing of the new terminal

  • viewportHeigh: Number [OPTIONAL]

    Change the default viewport height in pixels for testing of the new terminal

Returns

Nothing.

renderTerminal

Renders a web terminal with a SOM as default content.

Usage
Copy
renderTerminal(terminalProps: Object): RenderResult
  • terminalProps? Object

    The props to be sent to the web terminal.

Returns

RenderResult

textshot

Generates a visual representation of the terminal's content.

Usage
Copy
textShot(terminal: container): [String]
  • terminal Terminal Container

    Terminal rendered with terminalTestUtils.renderTerminal.

Returns
  • [String]

    It returns the visible content of the terminal which is currently shown at the viewport. Each item of the array represents a line of the terminal's content.

Textshot works if the content is the first element and it is not inside a wrapper.

Example
Copy
test('textshot works also if the first element is the terminal without any wrapper', () => {
  const { container } = renderTerminal();
  const terminalWithoutWrapper = getByTestId(container, 'terminal');

  focus(terminalWithoutWrapper);
  expect(textshot(terminalWithoutWrapper)).toEqual(['>█']);
});

Note: Textshot returns only the visible content of the terminal which is shown at the terminal's viewport.

writeChars

Simulates the action of typing characters in the terminal.

Usage
Copy
writeChars(terminal: container, chars: String): void
  • terminal Terminal Container

    Terminal rendered with terminalTestUtils.renderTerminal.

  • chars String

    The characters to be typed.

focus

Focuses a web terminal.

The terminal's cursor appears when the terminal is focused.

Usage
Copy
focus(terminal: Terminal container): void
  • terminal Terminal container

    Terminal rendered with terminalTestUtils.renderTerminal.

blur

Removes the focus from a web terminal.

Usage
Copy
blur(terminal: Terminal container): void
  • terminal Terminal container

    Terminal rendered with terminalTestUtils.renderTerminal.

sendKey

Sends a key to a web terminal.

Useful information: You can send a combination of keys. For example Shift + Arrow Down is equal to S-ArrowDown.

See also: Key Values

Usage
Copy
sendKey(terminal: Terminal container, key: String): void
  • terminal Terminal container

    Terminal rendered with terminalTestUtils.renderTerminal.

  • key String

    Key to be executed.

moveCursor

Moves the cursor to a specified position.

Usage
Copy
moveCursor(terminalId: String, to: Object): void
  • terminalId: String [OPTIONAL]

    Default "terminal1"; identifier for the new terminal

  • to Object

    • line Number

      Line of the to position.

    • column Number

      Column of the to position.

select

Selects the specified lines and columns of a web terminal.

Using the textshot method represents the selection with the following characters:

  • ▛: Start of selection.

  • ▟: End of selection.

  • ▐: The character appears at the beginning of the line if the start and end of the selection are from other lines, meaning that the line containing this character is entirely selected.

  • █: The terminal cursor.

Usage
Copy
select(terminal: Terminal container, anchorOffset: Object, focusOffset: Object): void
  • terminal Terminal container

    Terminal rendered with terminalTestUtils.renderTerminal.

  • anchorOffset Object

    The anchor offset of the selection.

    • line Number

      Line of the offset.

    • column Number

      Column of the offset.

  • focusOffset Object

    The focus offset of the selection.

    • line Number

      Line of the offset.

    • column Number

      Column of the offset.

mouseDown

Emulates the mouse down event. It returns an object with mouseUp and mouseMove functions.

Note: This method is used by terminalsTestUtils.select and terminalsTestUtils.clickAt. Consider using those methods rather than repeating the same logic of both approaches.

Usage
Copy
mouseDown(terminal: Terminal container, downOffset: Object): { mouseUp: Function, mouseMove: Function }
  • terminal Terminal container

    Terminal rendered with terminalTestUtils.renderTerminal.

  • downOffset Object

    The anchor offset of the selection.

    • line Number

      Line of the offset.

    • column Number

      Column of the offset.

Returns
  • [Object]

    • [mouseUp]

      Function that receives an offset to fire the mouse up event.

    • [mouseMove]

      Function that receives an offset move the mouse.

clickAt

Moves the cursor position with a click.

Usage
Copy
clickAt(terminalProps: Object, offset: Object): void
  • terminal Terminal container

    Terminal rendered with terminalTestUtils.renderTerminal.

  • offset Object

    The anchor offset of the selection.

    • line Number

      Line of the offset.

    • column Number

      Column of the offset.

copy

Copies the selected text.

Usage
Copy
copy(terminal: Terminal container): String
  • terminal Terminal container

    Terminal rendered with terminalTestUtils.renderTerminal.

Returns
  • [String]

    Returns the selected text.

paste

Pastes the text into the terminal.

Usage
Copy
paste(terminal: Terminal container, text: String): void
  • terminal Terminal container

    Terminal rendered with terminalTestUtils.renderTerminal.

  • text String

    Text to be pasted.

mockResponse

Simulate a terminal response for the specified entry.

Usage
Copy
mockResponse({ crypticEntry: String? }): Object
  • configuration Object [OPTIONAL]

    Configuration for the mocked response.

    • crypticEntry String [OPTIONAL]

      Specific entry to response.

Returns
  • [Object]

    • [reply]

      Function to mock the response.

    • [clean]

      Clean existing mocks.

    • [persist]

      Avoid removing the mock after sending a response.