mirror of https://github.com/msgbyte/tailchat
test: add testcase for bbcode plugin
parent
0c73be8850
commit
60b51f50b5
@ -0,0 +1,41 @@
|
||||
import { preProcessLinkText, preProcessText } from '../index';
|
||||
|
||||
describe('bbcode common', () => {
|
||||
describe('preprocess text', () => {
|
||||
it('simple url parse', () => {
|
||||
const text = preProcessLinkText('http://baidu.com');
|
||||
expect(text).toBe('[url]http://baidu.com[/url]');
|
||||
});
|
||||
|
||||
it('mix text and url parse', () => {
|
||||
const text = preProcessLinkText('open:http://baidu.com');
|
||||
expect(text).toBe('open:[url]http://baidu.com[/url]');
|
||||
});
|
||||
|
||||
it('mix text and more url parse', () => {
|
||||
const text = preProcessLinkText(
|
||||
'open:http://baidu.com and http://google.com'
|
||||
);
|
||||
expect(text).toBe(
|
||||
'open:[url]http://baidu.com[/url] and [url]http://google.com[/url]'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('preProcessText', () => {
|
||||
test.each([
|
||||
['https://baidu.com', '[url]https://baidu.com[/url]'],
|
||||
['[url]https://baidu.com[/url]', '[url]https://baidu.com[/url]'],
|
||||
[
|
||||
'[url=https://baidu.com]百度[/url]',
|
||||
'[url=https://baidu.com]百度[/url]',
|
||||
],
|
||||
[
|
||||
'[url=https://baidu.com alt=test]百度[/url]',
|
||||
'[url=https://baidu.com alt=test]百度[/url]',
|
||||
],
|
||||
])('%s', (input, output) => {
|
||||
expect(preProcessText(input)).toBe(output);
|
||||
});
|
||||
});
|
||||
});
|
@ -0,0 +1,59 @@
|
||||
import bbcodeParser from '../parser';
|
||||
|
||||
describe('bbcode parser', () => {
|
||||
test('simple text', () => {
|
||||
const ast = bbcodeParser.parse('text');
|
||||
|
||||
expect(ast).toMatchObject(['text']);
|
||||
});
|
||||
|
||||
test('simple text in []', () => {
|
||||
const ast = bbcodeParser.parse('[text]');
|
||||
|
||||
expect(ast).toMatchObject(['[text]']);
|
||||
});
|
||||
|
||||
test('non text in bbcode tag', () => {
|
||||
const ast = bbcodeParser.parse('[url][/url]');
|
||||
|
||||
expect(ast).toMatchObject(['[url]']);
|
||||
});
|
||||
|
||||
test('space char in bbcode tag', () => {
|
||||
const ast = bbcodeParser.parse('[url] [/url]');
|
||||
|
||||
expect(ast).toMatchObject([
|
||||
{
|
||||
tag: 'url',
|
||||
attrs: {},
|
||||
content: [' '],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
describe('tag url', () => {
|
||||
test('with plain text', () => {
|
||||
const ast = bbcodeParser.parse('[url]http://baidu.com[/url]');
|
||||
|
||||
expect(ast).toMatchObject([
|
||||
{
|
||||
tag: 'url',
|
||||
attrs: {},
|
||||
content: ['http://baidu.com'],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('with custom text', () => {
|
||||
const ast = bbcodeParser.parse('[url=http://baidu.com]a[/url]');
|
||||
|
||||
expect(ast).toMatchObject([
|
||||
{
|
||||
tag: 'url',
|
||||
attrs: { url: 'http://baidu.com' },
|
||||
content: ['a'],
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
@ -0,0 +1,16 @@
|
||||
import { bbcodeToPlainText } from '../serialize';
|
||||
|
||||
describe('bbcodeToPlainText should be ok', () => {
|
||||
test.each([
|
||||
['normal', 'normal'],
|
||||
['with space', 'with space'],
|
||||
['image [img]http://image.url[/img]', 'image [图片]'],
|
||||
['url [url]http://link.url[/url]', 'url http://link.url'],
|
||||
['url2 [url=http://baidu.com]a[/url]', 'url2 a'],
|
||||
['at [at=uuid]name[/at]', 'at name'],
|
||||
])('%s', (input, output) => {
|
||||
const plain = bbcodeToPlainText(input);
|
||||
|
||||
expect(output).toBe(plain);
|
||||
});
|
||||
});
|
@ -0,0 +1,25 @@
|
||||
import type { AstNodeObj } from '../type';
|
||||
import { getUrlTagRealUrl } from '../utils';
|
||||
|
||||
describe('getUrlTagRealUrl', () => {
|
||||
test.each([
|
||||
[
|
||||
{
|
||||
tag: 'url',
|
||||
attrs: { url: 'https://baidu.com' },
|
||||
content: ['百度'],
|
||||
},
|
||||
'https://baidu.com',
|
||||
],
|
||||
[
|
||||
{
|
||||
tag: 'url',
|
||||
attrs: {},
|
||||
content: ['https://baidu.com'],
|
||||
},
|
||||
'https://baidu.com',
|
||||
],
|
||||
])('%o => %s', (input: AstNodeObj, output) => {
|
||||
expect(getUrlTagRealUrl(input)).toBe(output);
|
||||
});
|
||||
});
|
@ -0,0 +1 @@
|
||||
declare module '@bbob/parser';
|
Loading…
Reference in New Issue