dispatch/client/js/utils/__tests__/util.test.js

141 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-06-19 01:54:19 +00:00
import {
trimPrefixChar,
isChannel,
isValidNick,
isValidChannel,
isValidUsername
} from '..';
2017-05-27 04:47:17 +00:00
import linkify from '../linkify';
2020-06-19 01:54:19 +00:00
describe('trimPrefixChar()', () => {
it('trims prefix characters', () => {
expect(trimPrefixChar('##chan', '#')).toBe('chan');
expect(trimPrefixChar('#chan', '#')).toBe('chan');
expect(trimPrefixChar('chan', '#')).toBe('chan');
});
});
2018-04-25 03:36:27 +00:00
describe('isChannel()', () => {
it('it handles strings', () => {
expect(isChannel('#cake')).toBe(true);
expect(isChannel('cake')).toBe(false);
});
it('handles tab objects', () => {
expect(isChannel({ name: '#cake' })).toBe(true);
expect(isChannel({ name: 'cake' })).toBe(false);
});
});
2018-05-16 03:02:48 +00:00
describe('isValidNick()', () => {
it('validates nicks', () =>
Object.entries({
bob: true,
'bob likes cake': false,
'-bob': false,
'bob.': false,
'bob-': true,
'1bob': false,
'[bob}': true,
'': false,
' ': false
}).forEach(([input, expected]) =>
expect(isValidNick(input)).toBe(expected)
));
});
describe('isValidChannel()', () => {
it('validates channels', () =>
Object.entries({
'#chan': true,
'#cak e': false,
'#cake:': false,
'#[cake]': true,
'#ca,ke': false,
'': false,
' ': false,
cake: false
}).forEach(([input, expected]) =>
expect(isValidChannel(input)).toBe(expected)
));
it('handles requirePrefix', () =>
Object.entries({
chan: true,
'cak e': false,
'#cake:': false,
'#[cake]': true,
'#ca,ke': false
}).forEach(([input, expected]) =>
expect(isValidChannel(input, false)).toBe(expected)
));
});
describe('isValidUsername()', () => {
it('validates usernames', () =>
Object.entries({
bob: true,
'bob likes cake': false,
'-bob': true,
'bob.': true,
'bob-': true,
'1bob': true,
'[bob}': true,
'': false,
' ': false,
'b@b': false
}).forEach(([input, expected]) =>
expect(isValidUsername(input)).toBe(expected)
));
});
2017-05-27 04:47:17 +00:00
describe('linkify()', () => {
2018-04-05 23:46:22 +00:00
const proto = href => (href.indexOf('http') !== 0 ? `http://${href}` : href);
2020-06-30 11:24:23 +00:00
const linkTo = href => ({
type: 'link',
url: proto(href),
text: href
});
const buildText = arr => {
for (let i = 0; i < arr.length; i++) {
if (typeof arr[i] === 'string') {
arr[i] = {
type: 'text',
text: arr[i]
};
}
}
return arr;
};
2017-05-27 04:47:17 +00:00
2020-06-30 11:24:23 +00:00
it('returns a text block when no matches are found', () =>
['just some text', ''].forEach(input =>
expect(linkify(input)).toStrictEqual([{ type: 'text', text: input }])
2018-04-05 23:46:22 +00:00
));
2017-05-27 04:47:17 +00:00
2018-04-05 23:46:22 +00:00
it('linkifies text', () =>
Object.entries({
2020-06-30 11:24:23 +00:00
'google.com': [linkTo('google.com')],
2018-04-05 23:46:22 +00:00
'google.com stuff': [linkTo('google.com'), ' stuff'],
'cake google.com stuff': ['cake ', linkTo('google.com'), ' stuff'],
'cake google.com stuff https://google.com': [
'cake ',
linkTo('google.com'),
' stuff ',
linkTo('https://google.com')
],
'cake google.com stuff pie https://google.com ': [
'cake ',
linkTo('google.com'),
' stuff pie ',
linkTo('https://google.com'),
' '
],
' google.com': [' ', linkTo('google.com')],
'google.com ': [linkTo('google.com'), ' '],
'/google.com?': ['/', linkTo('google.com'), '?']
}).forEach(([input, expected]) =>
2020-06-30 11:24:23 +00:00
expect(linkify(input)).toEqual(buildText(expected))
2018-04-05 23:46:22 +00:00
));
2017-05-27 04:47:17 +00:00
});