diff --git a/app/assets/javascripts/app/views/publisher/mention_view.js b/app/assets/javascripts/app/views/publisher/mention_view.js index d1c98b829..36ab541cf 100644 --- a/app/assets/javascripts/app/views/publisher/mention_view.js +++ b/app/assets/javascripts/app/views/publisher/mention_view.js @@ -80,6 +80,8 @@ app.views.PublisherMention = app.views.SearchBase.extend({ return; } + this.cleanMentionedPeople(); + // result[1] is the string between the last '@' and the current caret position this.typeaheadInput.typeahead("val", result[1]); this.typeaheadInput.typeahead("open"); @@ -122,7 +124,6 @@ app.views.PublisherMention = app.views.SearchBase.extend({ * Listens for user input and opens results dropdown when input contains the trigger char */ onInputBoxInput: function() { - this.cleanMentionedPeople(); this.updateTypeaheadInput(); }, @@ -178,5 +179,10 @@ app.views.PublisherMention = app.views.SearchBase.extend({ isVisible: function() { return this.$(".tt-menu").is(":visible"); + }, + + getMentionedPeople: function() { + this.cleanMentionedPeople(); + return this.mentionedPeople; } }); diff --git a/app/assets/javascripts/app/views/publisher_view.js b/app/assets/javascripts/app/views/publisher_view.js index a06ac3387..c19ff863e 100644 --- a/app/assets/javascripts/app/views/publisher_view.js +++ b/app/assets/javascripts/app/views/publisher_view.js @@ -192,9 +192,6 @@ app.views.Publisher = Backbone.View.extend({ // empty poll answer and failing validation. this.viewPollCreator.removeLastAnswer(); - //add missing mentions at end of post: - this.handleTextchange(); - var serializedForm = $(evt.target).closest("form").serializeObject(); // disable input while posting, must be after the form is serialized this.setInputEnabled(false); @@ -320,13 +317,10 @@ app.views.Publisher = Backbone.View.extend({ }, createPostPreview: function() { - //add missing mentions at end of post: - this.handleTextchange(); - var serializedForm = $("#new_status_message").serializeObject(); var text = serializedForm["status_message[text]"]; var photos = this.getUploadedPhotos(); - var mentionedPeople = this.mention.mentionedPeople; + var mentionedPeople = this.mention.getMentionedPeople(); var poll = this.getPollData(serializedForm); var locationCoords = serializedForm["location[coords]"]; if(!locationCoords || locationCoords === "") { diff --git a/spec/javascripts/app/views/publisher_mention_view_spec.js b/spec/javascripts/app/views/publisher_mention_view_spec.js index 4cd9e5ae6..666a62d96 100644 --- a/spec/javascripts/app/views/publisher_mention_view_spec.js +++ b/spec/javascripts/app/views/publisher_mention_view_spec.js @@ -229,6 +229,22 @@ describe("app.views.PublisherMention", function() { expect(this.view.closeSuggestions).not.toHaveBeenCalled(); expect(this.view.typeaheadInput.val()).toBe("user"); }); + + it("doesn't call 'cleanMentionedPeople' if there is no '@' in front of the caret", function() { + spyOn(this.view, "cleanMentionedPeople"); + this.view.inputBox.val("user1337 Text before @user1 text after"); + this.view.inputBox[0].setSelectionRange(9, 9); + this.view.updateTypeaheadInput(); + expect(this.view.cleanMentionedPeople).not.toHaveBeenCalled(); + }); + + it("calls 'cleanMentionedPeople' if there is an '@' in front of the caret", function() { + spyOn(this.view, "cleanMentionedPeople"); + this.view.inputBox.val("@user1337 Text before @user1 text after"); + this.view.inputBox[0].setSelectionRange(9, 9); + this.view.updateTypeaheadInput(); + expect(this.view.cleanMentionedPeople).toHaveBeenCalled(); + }); }); describe("prefillMention", function() { @@ -352,12 +368,6 @@ describe("app.views.PublisherMention", function() { this.view = new app.views.PublisherMention({ el: "#publisher" }); }); - it("calls 'cleanMentionedPeople'", function() { - spyOn(this.view, "cleanMentionedPeople"); - this.view.onInputBoxInput(); - expect(this.view.cleanMentionedPeople).toHaveBeenCalled(); - }); - it("calls 'updateTypeaheadInput'", function() { spyOn(this.view, "updateTypeaheadInput"); this.view.onInputBoxInput(); @@ -422,4 +432,22 @@ describe("app.views.PublisherMention", function() { expect(this.view.typeaheadInput.val()).toBe(""); }); }); + + describe("getMentionedPeople", function() { + beforeEach(function() { + this.view = new app.views.PublisherMention({el: "#publisher"}); + }); + + it("calls 'cleanMentionedPeople'", function() { + spyOn(this.view, "cleanMentionedPeople"); + this.view.getMentionedPeople(); + expect(this.view.cleanMentionedPeople).toHaveBeenCalled(); + }); + + it("returns the cleaned mentionedPeople", function() { + this.view.inputBox.val("@{user1@pod.tld} user2@pod.tld"); + this.view.mentionedPeople = [{name: "user1", handle: "user1@pod.tld"}, {name: "user2", handle: "user2@pod.tld"}]; + expect(this.view.getMentionedPeople()).toEqual([{name: "user1", handle: "user1@pod.tld"}]); + }); + }); }); diff --git a/spec/javascripts/app/views/publisher_view_spec.js b/spec/javascripts/app/views/publisher_view_spec.js index a0bd9c292..0b3bc8993 100644 --- a/spec/javascripts/app/views/publisher_view_spec.js +++ b/spec/javascripts/app/views/publisher_view_spec.js @@ -203,12 +203,6 @@ describe("app.views.Publisher", function() { }); describe("createStatusMessage", function(){ - it("calls handleTextchange to complete missing mentions", function(){ - spyOn(this.view, "handleTextchange"); - this.view.createStatusMessage($.Event()); - expect(this.view.handleTextchange).toHaveBeenCalled(); - }); - it("adds the status message to the stream", function() { app.stream = { addNow: $.noop }; spyOn(app.stream, "addNow"); @@ -224,14 +218,6 @@ describe("app.views.Publisher", function() { }); }); - describe("createPostPreview", function(){ - it("calls handleTextchange to complete missing mentions", function(){ - spyOn(this.view, "handleTextchange"); - this.view.createPostPreview(); - expect(this.view.handleTextchange).toHaveBeenCalled(); - }); - }); - describe('#setText', function() { it("sets the content text", function() { this.view.setText("FOO bar");