UNPKG

5.87 kBJavaScriptView Raw
1'use strict';
2
3/* globals describe, it, before */
4
5const assert = require('assert');
6const XRegExp = require('xregexp');
7const regex = XRegExp('(?:^|\\s|\\>|;)(@[\\p{L}\\d\\-_.]+)', 'g');
8
9const db = require.main.require('./test/mocks/databasemock');
10
11// use core slugify module
12const slugify = require.main.require('./src/slugify');
13
14describe('regex', function () {
15 const matcher = new RegExp(regex);
16 const strings = [
17 '@testUser',
18 '@testUser some text',
19 'some text @testUser',
20 '<a href="/user/testuser">@testUser</a>',
21 '<a href="/user/testuser">@testUser</a> some text',
22 'some text <a href="/user/testuser">@testUser</a>',
23 'end of sentence. @testUser',
24 '@testUser.',
25 '@testUser\'s some text',
26 ]
27 it('should match a mention in all strings', () => {
28 strings.forEach(string => {
29 const matches = string.match(matcher);
30 assert(matches);
31 assert.equal(slugify(matches[0]), 'testuser');
32 });
33 });
34
35 // TODO: Test for unicode/non-latin mention
36 // TODO: Ideally the regex matcher should be its own utility function in `lib/`
37});
38
39describe('splitter', function () {
40 const utility = require('../lib/utility');
41 const testHTMLText = 'this is a post with <code>stuff in code</code> and a\n\n<blockquote>blockquote or two</blockquote>';
42 const testMdText = 'this is a post with `stuff in code` and a \n\n>blockquote or two';
43 const testCodefirstMdText = '`code starts` with regular text afterwards';
44 const testCodefirstHTMLText = '<code>code starts</code> with regular text afterwards';
45
46 it('should not error', () => {
47 const results = [
48 utility.split(testHTMLText, false, true, false),
49 utility.split(testHTMLText, false, false, true),
50 utility.split(testHTMLText, false, true, true),
51 utility.split(testMdText, true, true, false),
52 utility.split(testMdText, true, false, true),
53 utility.split(testMdText, true, true, true),
54 ];
55 results.forEach(test => assert(test));
56 });
57
58 describe('HTML text', () => {
59 it('should split blockquotes properly', () => {
60 const result = utility.split(testHTMLText, false, true, false);
61 assert.equal(result[0], 'this is a post with <code>stuff in code</code> and a\n\n');
62 assert.equal(result[1], '<blockquote>blockquote or two</blockquote>');
63 });
64
65 it('should split inline code properly', () => {
66 const result = utility.split(testHTMLText, false, false, true);
67 assert.equal(result[0], 'this is a post with ');
68 assert.equal(result[1], '<code>stuff in code</code>');
69 assert.equal(result[2], ' and a\n\n<blockquote>blockquote or two</blockquote>');
70 });
71
72 it('should split both blockquotes and code properly', () => {
73 const result = utility.split(testHTMLText, false, true, true);
74 assert.equal(result[0], 'this is a post with ');
75 assert.equal(result[1], '<code>stuff in code</code>');
76 assert.equal(result[2], ' and a\n\n');
77 assert.equal(result[3], '<blockquote>blockquote or two</blockquote>');
78 });
79
80 it('should split properly if a post starts with a code block', () => {
81 const result = utility.split(testCodefirstMdText, true, true, true);
82 assert.equal(result[0], '');
83 assert.equal(result[1], '`code starts`');
84 assert.equal(result[2], ' with regular text afterwards');
85 });
86
87 it('should split properly if a post starts with a code block', () => {
88 const result = utility.split(testCodefirstHTMLText, false, true, true);
89 assert.equal(result[0], '');
90 assert.equal(result[1], '<code>code starts</code>');
91 assert.equal(result[2], ' with regular text afterwards');
92 });
93
94 it('should split HTML code blocks that are wrapped with only a <pre>', () => {
95 const testString = '<p dir="auto">test text</p>\n<pre>var value = \'@admin\';</pre>\nafter text';
96 const result = utility.split(testString, false, false, true);
97 assert.equal(result[0], '<p dir="auto">test text</p>\n');
98 assert.equal(result[1], '<pre>var value = \'@admin\';</pre>');
99 assert.equal(result[2], '\nafter text');
100 });
101
102 it('should not accidentally split on <annotation> HTML tag', () => {
103 const testString = '<p dir="auto">wonderful</p><annotation>what is an annotation anyway</annotation><a href="/">what</a>';
104 const results = [
105 utility.split(testString, false, false, false),
106 utility.split(testString, false, false, true),
107 utility.split(testString, false, true, false),
108 utility.split(testString, false, true, true),
109 ];
110
111 results.forEach(result => assert.strictEqual(result[0], '<p dir="auto">wonderful</p><annotation>what is an annotation anyway</annotation>'));
112 results.forEach(result => assert.strictEqual(result[1], '<a href="/">what</a>'));
113 });
114 });
115
116 describe('Markdown text', () => {
117 it('should split blockquotes properly', () => {
118 const result = utility.split(testMdText, true, true, false);
119 assert.equal(result[0], 'this is a post with `stuff in code` and a \n\n');
120 assert.equal(result[1], '>blockquote or two');
121 });
122
123 it('should split inline code properly', () => {
124 const result = utility.split(testMdText, true, false, true);
125 assert.equal(result[0], 'this is a post with ');
126 assert.equal(result[1], '`stuff in code`');
127 assert.equal(result[2], ' and a \n\n>blockquote or two');
128 });
129
130 it('should split both blockquotes and code properly', () => {
131 const result = utility.split(testMdText, true, true, true);
132 assert.equal(result[0], 'this is a post with ');
133 assert.equal(result[1], '`stuff in code`');
134 assert.equal(result[2], ' and a \n\n');
135 assert.equal(result[3], '>blockquote or two');
136 });
137
138 it('should split code fences properly', () => {
139 const testString = 'this is some text\n\n```\nvar a = \'@admin\';\n```\nafter text';
140 const result = utility.split(testString, true, false, true);
141 assert.equal(result[0], 'this is some text\n\n');
142 assert.equal(result[1], '```\nvar a = \'@admin\';\n```');
143 assert.equal(result[2], '\nafter text');
144 });
145 });
146});