Fix mentions parser, add test

This commit is contained in:
Ivan Diaz 2018-11-29 15:38:07 -03:00
parent 7b7dc334d8
commit a309314e16
3 changed files with 77 additions and 3 deletions

View File

@ -0,0 +1,71 @@
root = 'ROOT_PATH';
const mentionsParser = requireUnit('lib-app/mentions-parser', {});
describe('MentionsParser library', function () {
it('should not parse text without mentions', function () {
expect(mentionsParser.parse(
'This is an example without mentions'
)).to.equal(
'This is an example without mentions'
);
expect(mentionsParser.parse(
'This is an example without mentions <img src=\'/image.png\' /> abc <br/>',
)).to.equal(
'This is an example without mentions <img src=\'/image.png\' /> abc <br/>',
);
});
it('should parse ticket number mention', function () {
expect(mentionsParser.parse(
'This is an example with #123456'
)).to.equal(
'This is an example with <a href="ROOT_PATH/admin/panel/tickets/view-ticket/123456">#123456</a>'
);
expect(mentionsParser.parse(
'This is an example with #487213 text'
)).to.equal(
'This is an example with <a href="ROOT_PATH/admin/panel/tickets/view-ticket/487213">#487213</a> text'
);
expect(mentionsParser.parse(
'This is an example with #487213text'
)).to.equal(
'This is an example with <a href="ROOT_PATH/admin/panel/tickets/view-ticket/487213">#487213</a>text'
);
expect(mentionsParser.parse(
'This is an example with 4848#777777text'
)).to.equal(
'This is an example with 4848<a href="ROOT_PATH/admin/panel/tickets/view-ticket/777777">#777777</a>text'
);
});
it('should not parse invalid ticket number mention', function () {
expect(mentionsParser.parse(
'This is an example with #12345'
)).to.equal(
'This is an example with #12345'
);
expect(mentionsParser.parse(
'This is an example with #12345abv hello'
)).to.equal(
'This is an example with #12345abv hello'
);
expect(mentionsParser.parse(
'This is an example with #a12345 hello'
)).to.equal(
'This is an example with #a12345 hello'
);
expect(mentionsParser.parse(
'This is an example with # hello'
)).to.equal(
'This is an example with # hello'
);
});
});

View File

@ -37,13 +37,16 @@ class MentionsParser {
isDigit(string){ isDigit(string){
return /[0-9]/.test(string); return /[0-9]/.test(string);
} }
compileSegment(segment, parsingType){ compileSegment(segment, parsingType){
switch(parsingType){ switch(parsingType){
case PARSING_TEXT: case PARSING_TEXT:
return segment; return segment;
case PARSING_MENTION: case PARSING_MENTION:
return '<a href=' + root + '/admin/panel/tickets/view-ticket/' + segment + '>#' + segment + '</a>'; if(segment.length == 6)
return '<a href="' + root + '/admin/panel/tickets/view-ticket/' + segment + '">#' + segment + '</a>';
else
return '#' + segment;
default: default:
return ''; return '';
} }

View File

@ -7,7 +7,7 @@ install:
@bundle install @bundle install
run: export MYSQL_HOST=127.0.0.1 run: export MYSQL_HOST=127.0.0.1
run: export MYSQL_PORT=3306 run: export MYSQL_PORT=4040
run: run:
./run-tests.sh ./run-tests.sh