UNPKG

r3bl-ts-utils

Version:

The `r3bl-ts-utils` package is a set of useful TypeScript functions and classes that can be used in Node.js and browser environments. They are inspired by Kotlin stdlib, and Rust to write code as expressions rather than statements, colorized text, powerfu

98 lines 4.46 kB
"use strict"; /* * Copyright (c) 2022 R3BL LLC. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const delay_1 = __importDefault(require("delay")); const events_1 = __importDefault(require("events")); const ink_1 = require("ink"); const ink_testing_library_1 = require("ink-testing-library"); const React = __importStar(require("react")); const index_1 = require("../index"); const lang_utils_1 = require("../lang-utils"); const test_helpers_1 = require("./test-helpers"); test("useKeyboard node-keypress works", async () => { const flag = new test_helpers_1.Flag(); const Test = ({ options, index }) => { const { keyPress, inRawMode } = (0, index_1.useKeyboardBuilder)(options); return (React.createElement(ink_1.Box, { flexDirection: "column" }, keyPress && React.createElement(Row_Debug, { inRawMode: inRawMode, keyPress: keyPress.toString() }), React.createElement(ink_1.Text, null, index, " - Your example goes here!"))); }; const Row_Debug = ({ keyPress, inRawMode }) => inRawMode ? React.createElement(ink_1.Text, { color: "magenta" }, "keyPress: ", keyPress) : React.createElement(ink_1.Text, { color: "gray" }, "keyb disabled"); const createShortcutsFn = () => (0, index_1._also)((0, index_1.createNewShortcutToActionMap)(), map => map.set("q", flag.set)); const matchKeypressFn = input => { (0, lang_utils_1._callIfSome)(input, input => { if (input.matches("q")) flag.set(); }); }; const options = { isActive: true }; const configArray = [ { type: "node-keypress", args: { type: "fun", matchKeypressFn, options } }, { type: "node-keypress", args: { type: "map", map: createShortcutsFn(), options } }, { type: "node-keypress", args: { type: "map-cached", createShortcutsFn, options } }, ]; for (const [index, options] of configArray.entries()) { flag.reset(); const emitter = new events_1.default(); const eventName = "testing-keypress"; const input = "q"; const key = { ctrl: false, meta: false, shift: false }; const optionsWithTesting = { ...options, testing: { emitter, eventName } }; const ink = (0, ink_testing_library_1.render)(React.createElement(Test, { index: index, options: optionsWithTesting })); await (0, delay_1.default)(100); const keypress = (0, index_1.createFromKeypress)(key, input); emitter.emit(eventName, keypress); await (0, delay_1.default)(100); expect(flag.isSet()).toBeTruthy(); expect(ink.lastFrame()).toContain("keyPress"); ink.unmount(); emitter.removeAllListeners(); } }); //# sourceMappingURL=use-keyboard-ink-node-keypress.test.js.map