From 5c25629a1427fd9a2e571bcde070f9aead2efd91 Mon Sep 17 00:00:00 2001 From: Kushal Pandya Date: Thu, 11 May 2017 09:05:07 +0000 Subject: [PATCH] Fix slash commands detection in comments --- app/assets/javascripts/notes.js | 2 +- spec/javascripts/notes_spec.js | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/notes.js b/app/assets/javascripts/notes.js index f6fe6d9f0fd..7ba44835741 100644 --- a/app/assets/javascripts/notes.js +++ b/app/assets/javascripts/notes.js @@ -24,7 +24,7 @@ const normalizeNewlines = function(str) { (function() { this.Notes = (function() { const MAX_VISIBLE_COMMIT_LIST_COUNT = 3; - const REGEX_SLASH_COMMANDS = /\/\w+/g; + const REGEX_SLASH_COMMANDS = /^\/\w+/gm; Notes.interval = null; diff --git a/spec/javascripts/notes_spec.js b/spec/javascripts/notes_spec.js index cfd599f793e..be4605a5b89 100644 --- a/spec/javascripts/notes_spec.js +++ b/spec/javascripts/notes_spec.js @@ -376,13 +376,20 @@ import '~/notes'; this.notes = new Notes('', []); }); - it('should return true when comment has slash commands', () => { - const sampleComment = '/wip /milestone %1.0 /merge /unassign Merging this'; + it('should return true when comment begins with a slash command', () => { + const sampleComment = '/wip \n/milestone %1.0 \n/merge \n/unassign Merging this'; const hasSlashCommands = this.notes.hasSlashCommands(sampleComment); expect(hasSlashCommands).toBeTruthy(); }); + it('should return false when comment does NOT begin with a slash command', () => { + const sampleComment = 'Hey, /unassign Merging this'; + const hasSlashCommands = this.notes.hasSlashCommands(sampleComment); + + expect(hasSlashCommands).toBeFalsy(); + }); + it('should return false when comment does NOT have any slash commands', () => { const sampleComment = 'Looking good, Awesome!'; const hasSlashCommands = this.notes.hasSlashCommands(sampleComment); @@ -392,14 +399,20 @@ import '~/notes'; }); describe('stripSlashCommands', () => { - const REGEX_SLASH_COMMANDS = /\/\w+/g; - - it('should strip slash commands from the comment', () => { + it('should strip slash commands from the comment which begins with a slash command', () => { this.notes = new Notes(); - const sampleComment = '/wip /milestone %1.0 /merge /unassign Merging this'; + const sampleComment = '/wip \n/milestone %1.0 \n/merge \n/unassign Merging this'; const stripedComment = this.notes.stripSlashCommands(sampleComment); - expect(REGEX_SLASH_COMMANDS.test(stripedComment)).toBeFalsy(); + expect(stripedComment).not.toBe(sampleComment); + }); + + it('should NOT strip string that has slashes within', () => { + this.notes = new Notes(); + const sampleComment = 'http://127.0.0.1:3000/root/gitlab-shell/issues/1'; + const stripedComment = this.notes.stripSlashCommands(sampleComment); + + expect(stripedComment).toBe(sampleComment); }); });