lucid-ui
Version:
A UI component library from AppNexus.
53 lines • 2.96 kB
JavaScript
import _isEqual from "lodash/isEqual";
import React from 'react';
import assert from 'assert';
import { partitionText, getCombinedChildText, propsSearch } from './text-manipulation';
describe('text-manipulation', function () {
describe('#partitionText', function () {
var pattern = new RegExp('pattern', 'i');
it('should return the passed in text as `post` if it does not match the pattern', function () {
assert(_isEqual(partitionText('text', pattern, 4), ['', '', 'text']));
});
it('should return the matched text as `match`, and the remaining text as `post`, if there is a match at the beginning of the string', function () {
assert(_isEqual(partitionText('patternpost', pattern, 7), ['', 'pattern', 'post']));
});
it('should return the prefix text as `pre`, the matched text as `match`, and the remaining text as `post`, if there is a match mid-string', function () {
assert(_isEqual(partitionText('prepatternpost', pattern, 7), ['pre', 'pattern', 'post']));
});
});
describe('#getCombinedChildText', function () {
it("should return '' if the passed in node has no children", function () {
var element = /*#__PURE__*/React.createElement("div", null);
assert.equal(getCombinedChildText(element.props), '');
});
it("should return the node's `children` if it is a string", function () {
var element = /*#__PURE__*/React.createElement("div", null, "child");
assert.equal(getCombinedChildText(element.props), 'child');
});
it('should recursively combine strings from children, ignoring other types', function () {
var element = /*#__PURE__*/React.createElement("div", null, "1", /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", null, "2"), /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", null, '3')), /*#__PURE__*/React.createElement(React.Fragment, null), /*#__PURE__*/React.createElement("div", null), /*#__PURE__*/React.createElement("div", null, null), /*#__PURE__*/React.createElement("div", null, true), /*#__PURE__*/React.createElement("div", null, 4)));
assert.equal(getCombinedChildText(element.props), '123');
});
});
describe('#propsSearch', function () {
it('should return true if the searchText is undefined', function () {
assert(propsSearch());
});
it('should return true if the searchText is null', function () {
assert(propsSearch(null));
});
it('should return true if the searchText is empty string', function () {
assert(propsSearch(''));
});
it("should return true if the searchText matches the option's text", function () {
assert(propsSearch('search', {
children: 'search'
}));
});
it("should return false if the searchText does not match the option's text", function () {
assert(!propsSearch('search', {
children: 'miss'
}));
});
});
});