import React from 'react';
import { generateMarkedComponent } from './generateMarkedComponent';
describe('generateMarkedComponent', function () {
it(`should be the plain text, if title is empty string`, () => {
const title = '';
const result = generateMarkedComponent(title, 'keyword');
expect(result).toBe(title);
});
it.each(['', null, undefined])(`should be the plain text, if keyword is empty('%s)`, (keyword) => {
const title = 'Hello, world!';
const result = generateMarkedComponent(title, keyword);
expect(result).toBe(title);
});
it(`should be return the plain text, if keyword is not matched`, () => {
const title = 'Starbucks Coffee';
const result = generateMarkedComponent(title, 'ABC');
expect(result).toBe(title);
});
it(`should be add mark, if upper keyword is matched`, () => {
const result = generateMarkedComponent('Starbucks Coffee', 'Star');
expect(result.length).toBe(2);
expect(React.isValidElement(result[0])).toBeTruthy();
expect(result).toMatchSnapshot();
});
it(`should be add mark, if lower keyword is matched`, () => {
const result = generateMarkedComponent('Starbucks Coffee', 'coff');
expect(result.length).toBe(3);
expect(React.isValidElement(result[1])).toBeTruthy();
expect(result).toMatchSnapshot();
});
it.each(['+66223', '66'])(
`should be add mark in the first word, if phone keyword '%s' is matched`,
(keyword: string) => {
const result = generateMarkedComponent('66-223-1234', keyword);
expect(result.length).toBe(2);
expect(React.isValidElement(result[0])).toBeTruthy();
expect(result).toMatchSnapshot();
}
);
it.each(['223', '23', '312', '123', '6223', '3123'])(
`should be add mark in the middle word, if phone keyword '%s' is matched`,
(keyword: string) => {
const result = generateMarkedComponent('66-223-1234', keyword);
expect(result.length).toBe(3);
expect(React.isValidElement(result[1])).toBeTruthy();
expect(result).toMatchSnapshot();
}
);
it.each(['234', '3-1234'])(
`should be add mark in the last word, if phone keyword '%s' is matched`,
(keyword: string) => {
const message = '66-223-1234';
const result = generateMarkedComponent(message, keyword);
expect(result.length).toBe(2);
// @ts-ignore
expect(result[0].props.children).toBe(message.split(keyword)[0]);
expect(React.isValidElement(result[1])).toBeTruthy();
expect(result).toMatchSnapshot();
}
);
it.each(['66 123 1234', '66 123 1234', '66--123--1234', '66 - 123 - 1234', '+66-10-123-1234'])(
`should be add mark, if message is '%s' matched with phone keyword`,
(message: string) => {
const keyword = '123';
const result = generateMarkedComponent(message, keyword);
const firstWord = message.split(new RegExp(keyword))[0];
expect(result.length).toBe(3);
// @ts-ignore
expect(result[0].props.children).toBe(firstWord);
expect(React.isValidElement(result[1])).toBeTruthy();
// @ts-ignore
expect(result[2].props.children).toBe(message.split(`${firstWord}${keyword}`)[1]);
expect(result).toMatchSnapshot();
}
);
});
keywrod matching
getSpecialCharEscapedRegex
getNumberByRegex
isKeywordMatched / isPhoneNumberString
marking with react