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
95 lines • 4.32 kB
JavaScript
/*
* 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
require("@testing-library/jest-dom");
const react_1 = require("@testing-library/react");
const lodash_1 = require("lodash");
const React = __importStar(require("react"));
const index_1 = require("../index");
/**
* https://testing-library.com/docs/react-testing-library/api/#render
*/
describe("use-state-safely", () => {
test("useIsComponentMounted works", () => {
let isComponentMounted = undefined;
const TestComponent = () => {
isComponentMounted = (0, index_1.useIsComponentMounted)();
return (React.createElement("p", null, "Test component"));
};
// https://testing-library.com/docs/react-testing-library/api/#render-result
const result = (0, react_1.render)(React.createElement(TestComponent, null));
expect(isComponentMounted).toBeDefined();
expect(isComponentMounted.current).toEqual(true);
expect(result.getByText("Test component")).toBeInTheDocument();
result.unmount();
expect(isComponentMounted).toBeDefined();
expect(isComponentMounted.current).toEqual(false);
});
test("useStateSafely as object works", () => {
const TestComponent = () => {
const { value, setValue } = (0, index_1.useStateSafely)("foo");
return (React.createElement("div", null,
React.createElement("p", null, value),
React.createElement("button", { onClick: () => setValue("bar") }, "click-me")));
};
const result = (0, react_1.render)(React.createElement(TestComponent, null));
expect(result.getByText("foo")).toBeInTheDocument();
react_1.fireEvent.click(result.getByText("click-me"));
expect(result.getByText("bar")).toBeInTheDocument();
});
test("useStateSafely as array works", () => {
const TestComponent = () => {
const [value, setValue] = (0, index_1.useStateSafely)("foo").asArray();
return (React.createElement("div", null,
React.createElement("p", null, value),
React.createElement("button", { onClick: () => setValue("bar") }, "click-me")));
};
const result = (0, react_1.render)(React.createElement(TestComponent, null));
expect(result.getByText("foo")).toBeInTheDocument();
react_1.fireEvent.click(result.getByText("click-me"));
expect(result.getByText("bar")).toBeInTheDocument();
});
});
test("StateHolder works", () => {
const value = "";
const setValue = lodash_1.noop;
const sh = new index_1.StateHolder(value, setValue);
const array = sh.asArray();
expect(array[0]).toBe(sh.value);
expect(array[1]).toBe(sh.setValue);
});
//# sourceMappingURL=react-hook-utils-useStateSafely.test.jsdom.js.map
;