summaryrefslogtreecommitdiffstats
path: root/apps/comments/tests/js/commentstabviewSpec.js
blob: b991e0d6804346b00df5c807eca5c91164f0d059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
OC.L10N.register(
    "updatenotification",
    {
    "{version} is available. Get more information on how to update." : "{version} er tilgjengelig. Få mer informasjon om å oppdatere.",
    "Update notifications" : "Oppdateringsvarsel",
    "Channel updated" : "Kanal oppdatert",
    "The update server could not be reached since %d days to check for new updates." : "Har ikke oppnådd kontakt med oppdateringsserveren på %d dager for å se etter nye oppdateringer.",
    "Please check the Nextcloud and server log files for errors." : "Se i Nextcloud- og server-loggen etter feil.",
    "Update to %1$s is available." : "Oppdatering til %1$s er tilgjengelig.",
    "Update for %1$s to version %2$s is available." : "Oppdatering for %1$s til versjon %2$s er tilgjengelig.",
    "Update for {app} to version %s is available." : "Oppdatering for {app} til versjon %s er tilgjengelig.",
    "Update notification" : "Oppdateringsvarsel",
    "The version you are running is not maintained anymore. Please make sure to update to a supported version as soon as possible." : "Versjonen du kjører vedlikeholdes ikke lengre. Vennligst sørg for å oppdatere til en støttet versjon så snart som mulig.",
    "Apps missing updates" : "Apper mangler oppdateringer",
    "Apps with available updates" : "Apper med tilgjengelige oppdateringer",
    "Open updater" : "Åpne oppdaterer",
    "Download now" : "Last ned nå",
    "The update check is not yet finished. Please refresh the page." : "Oppdateringssjekken er ikke ferdig, vennligst oppdater siden.",
    "Your version is up to date." : "Du har nyeste versjon.",
    "A non-default update server is in use to be checked for updates:" : "En ikke-forvalgt oppdateringsserver brukes for å se etter oppdateringer:",
    "Update channel:" : "Oppdateringskanal:",
    "You can always update to a newer version. But you can never downgrade to a more stable version." : "Du kan alltid oppdatere til en nyere versjon. Men du kan aldri nedgradere til en mer stabil versjon.",
    "Note that after a new release it can take some time before it shows up here. We roll out new versions spread out over time to our users and sometimes skip a version when issues are found." : "Vær oppmerksom på at etter en ny utgivelse kan det ta noe tid før den vises her. Vi ruller ut nye versjonen spredt utover tid til våre brukere, og av og til hoppes det over en versjon når problemer dukker opp.",
    "Notify members of the following groups about available updates:" : "Informer medlemmene i følgende grupper om tilgjengelig oppdateringer:",
    "Only notification for app updates are available." : "Kun varsler for app-oppdateringer er tilgjengelig.",
    "The selected update channel makes dedicated notifications for the server obsolete." : "Den valgte oppdateringskanalen gjør dedikerte varsler til denne serveren utdatert.",
    "The selected update channel does not support updates of the server." : "Den valgte oppdateringskanalen tilbyr ikke oppdateringer av serveren.",
    "A new version is available: <strong>{newVersionString}</strong>" : "En ny versjon er tilgjengelig: <strong>{newVersionString}</strong>",
    "Checked on {lastCheckedDate}" : "Sist sjekket {lastCheckedDate}",
    "Checking apps for compatible updates" : "Kontrollerer programmer for kompatible oppdateringer",
    "<strong>All</strong> apps have an update for this version available" : "<strong>Alle</strong> apper har en oppdatering tilgjengelig for denne versjonen",
    "_<strong>%n</strong> app has no update for this version available_::_<strong>%n</strong> apps have no update for this version available_" : ["<strong>%n</strong>  app har ingen oppdatering tilgjengelig for denne versjonen","<strong>%n</strong> apper har ingen oppdatering tilgjengelig for denne versjonen"],
    "You can always update to a newer version / experimental channel. But you can never downgrade to a more stable channel." : "Du kan alltid oppdatere til en nyere versjon / eksperimentell kanal. Men du kan aldri nedgradere til en mer stabil kanal.",
    "Could not start updater, please try the manual update" : "Kunne ikke starte oppdateringen, prøv å oppdatere manuelt"
},
"nplurals=2; plural=(n != 1);");
a id='n506' href='#n506'>506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
/**
* ownCloud
*
* @author Vincent Petry
* @copyright 2016 Vincent Petry <pvince81@owncloud.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* comment 3 of the License, or any later comment.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*
*/

describe('OCA.Comments.CommentsTabView tests', function() {
	var view, fileInfoModel;
	var fetchStub;
	var avatarStub;
	var testComments;
	var clock;

	/**
	 * Creates a dummy message with the given length
	 *
	 * @param {int} len length
	 * @return {string} message
	 */
	function createMessageWithLength(len) {
		var bigMessage = '';
		for (var i = 0; i < len; i++) {
			bigMessage += 'a';
		}
		return bigMessage;
	}

	beforeEach(function() {
		clock = sinon.useFakeTimers(Date.UTC(2016, 1, 3, 10, 5, 9));
		fetchStub = sinon.stub(OCA.Comments.CommentCollection.prototype, 'fetchNext');
		avatarStub = sinon.stub($.fn, 'avatar');
		view = new OCA.Comments.CommentsTabView();
		fileInfoModel = new OCA.Files.FileInfoModel({
			id: 5,
			name: 'One.txt',
			mimetype: 'text/plain',
			permissions: 31,
			path: '/subdir',
			size: 123456789,
			etag: 'abcdefg',
			mtime: Date.UTC(2016, 1, 0, 0, 0, 0)
		});
		view.render();
		var comment1 = new OCA.Comments.CommentModel({
			id: 1,
			actorType: 'users',
			actorId: 'user1',
			actorDisplayName: 'User One',
			objectType: 'files',
			objectId: 5,
			message: 'First',
			creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 5, 0)).toUTCString()
		});
		var comment2 = new OCA.Comments.CommentModel({
			id: 2,
			actorType: 'users',
			actorId: 'user2',
			actorDisplayName: 'User Two',
			objectType: 'files',
			objectId: 5,
			message: 'Second\nNewline',
			creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 0, 0)).toUTCString()
		});
		var comment3 = new OCA.Comments.CommentModel({
			id: 3,
			actorId: 'anotheruser',
			actorDisplayName: 'Another User',
			actorType: 'users',
			verb: 'comment',
			message: 'Hail to thee, @macbeth. Yours faithfully, @banquo',
			creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 5, 9)).toUTCString(),
			mentions: {
				0: {
					mentionDisplayName: "Thane of Cawdor",
					mentionId: "macbeth",
					mentionTye: "user"
				},
				1: {
					mentionDisplayName: "Lord Banquo",
					mentionId: "banquo",
					mentionTye: "user"
				}
			}
		});

		testComments = [comment1, comment2, comment3];
	});
	afterEach(function() {
		view.remove();
		view = undefined;
		fetchStub.restore();
		avatarStub.restore();
		clock.restore();
	});
	describe('rendering', function() {
		it('reloads matching comments when setting file info model', function() {
			view.setFileInfo(fileInfoModel);
			expect(fetchStub.calledOnce).toEqual(true);
		});

		it('renders loading icon while fetching comments', function() {
			view.setFileInfo(fileInfoModel);
			view.collection.trigger('request');

			expect(view.$el.find('.loading').length).toEqual(1);
			expect(view.$el.find('.comments li').length).toEqual(0);
		});

		it('renders comments', function() {
			view.setFileInfo(fileInfoModel);
			view.collection.set(testComments);

			var $comments = view.$el.find('.comments>li');
			expect($comments.length).toEqual(3);
			var $item = $comments.eq(0);
			expect($item.find('.author').text()).toEqual('User One');
			expect($item.find('.date').text()).toEqual('seconds ago');
			expect($item.find('.message').text()).toEqual('First');

			$item = $comments.eq(1);
			expect($item.find('.author').text()).toEqual('User Two');
			expect($item.find('.date').text()).toEqual('5 minutes ago');
			expect($item.find('.message').html()).toEqual('Second<br>Newline');
		});

		it('renders comments from deleted user differently', function() {
			testComments[0].set('actorType', 'deleted_users', {silent: true});
			view.collection.set(testComments);

			var $item = view.$el.find('.comment[data-id=1]');
			expect($item.find('.author').text()).toEqual('[Deleted user]');
			expect($item.find('.avatar').attr('data-username')).not.toBeDefined();
		});

		it('renders mentioned user id to avatar and displayname', function() {
			view.collection.set(testComments);

			var $comment = view.$el.find('.comment[data-id=3] .message');
			expect($comment.length).toEqual(1);
			expect($comment.find('.avatar[data-user=macbeth]').length).toEqual(1);
			expect($comment.find('strong:first').text()).toEqual('Thane of Cawdor');

			expect($comment.find('.avatar[data-user=banquo]').length).toEqual(1);
			expect($comment.find('.avatar-name-wrapper:last-child strong').text()).toEqual('Lord Banquo');
		});

	});
	describe('more comments', function() {
		var hasMoreResultsStub;

		beforeEach(function() {
			view.collection.set(testComments);
			hasMoreResultsStub = sinon.stub(OCA.Comments.CommentCollection.prototype, 'hasMoreResults');
		});
		afterEach(function() {
			hasMoreResultsStub.restore();
		});

		it('shows "More comments" button when more comments are available', function() {
			hasMoreResultsStub.returns(true);
			view.collection.trigger('sync');

			expect(view.$el.find('.showMore').hasClass('hidden')).toEqual(false);
		});
		it('does not show "More comments" button when more comments are available', function() {
			hasMoreResultsStub.returns(false);
			view.collection.trigger('sync');

			expect(view.$el.find('.showMore').hasClass('hidden')).toEqual(true);
		});
		it('fetches and appends the next page when clicking the "More" button', function() {
			hasMoreResultsStub.returns(true);

			expect(fetchStub.notCalled).toEqual(true);

			view.$el.find('.showMore').click();

			expect(fetchStub.calledOnce).toEqual(true);
		});
		it('appends comment to the list when added to collection', function() {
			var comment4 = new OCA.Comments.CommentModel({
				id: 4,
				actorType: 'users',
				actorId: 'user3',
				actorDisplayName: 'User Three',
				objectType: 'files',
				objectId: 5,
				message: 'Third',
				creationDateTime: new Date(Date.UTC(2016, 1, 3, 5, 0, 0)).toUTCString()
			});

			view.collection.add(comment4);

			expect(view.$el.find('.comments>li').length).toEqual(4);

			var $item = view.$el.find('.comments>li').eq(3);
			expect($item.find('.author').text()).toEqual('User Three');
			expect($item.find('.date').text()).toEqual('5 hours ago');
			expect($item.find('.message').html()).toEqual('Third');
		});
	});
	describe('posting comments', function() {
		var createStub;
		var currentUserStub;

		beforeEach(function() {
			view.collection.set(testComments);
			createStub = sinon.stub(OCA.Comments.CommentCollection.prototype, 'create');
			currentUserStub = sinon.stub(OC, 'getCurrentUser');
			currentUserStub.returns({
				uid: 'testuser',
				displayName: 'Test User'
			});

			// Required for the absolute selector used to find the new comment
			// after a successful creation in _onSubmitSuccess.
			$('#testArea').append(view.$el);
		});
		afterEach(function() {
			createStub.restore();
			currentUserStub.restore();
		});

		it('creates a new comment when clicking post button', function() {
			view.$el.find('.message').val('New message');
			view.$el.find('form').submit();

			expect(createStub.calledOnce).toEqual(true);
			expect(createStub.lastCall.args[0]).toEqual({
				actorId: 'testuser',
				actorDisplayName: 'Test User',
				actorType: 'users',
				verb: 'comment',
				message: 'New message',
				creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 5, 9)).toUTCString()
			});
		});
		it('creates a new comment with mentions when clicking post button', function() {
			view.$el.find('.message').val('New message @anotheruser');
			view.$el.find('form').submit();

			var createStubExpectedData = {
				actorId: 'testuser',
				actorDisplayName: 'Test User',
				actorType: 'users',
				verb: 'comment',
				message: 'New message @anotheruser',
				creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 5, 9)).toUTCString()
			};

			expect(createStub.calledOnce).toEqual(true);
			expect(createStub.lastCall.args[0]).toEqual(createStubExpectedData);

			var model = new OCA.Comments.CommentModel(_.extend({id: 4}, createStubExpectedData));
			var fetchStub = sinon.stub(model, 'fetch');
			// simulate the fact that create adds the model to the collection
			view.collection.add(model, {at: 0});
			createStub.yieldTo('success', model);

			expect(fetchStub.calledOnce).toEqual(true);

			// simulate the fact that fetch sets the attribute
			model.set('mentions', {
				0: {
					mentionDisplayName: "Another User",
					mentionId: "anotheruser",
					mentionTye: "user"
				}
			});
			fetchStub.yieldTo('success', model);

			// comment was added to the list
			var $comment = view.$el.find('.comment[data-id=4]');
			expect($comment.length).toEqual(1);
			var $message = $comment.find('.message');
			expect($message.html()).toContain('New message');
			expect($message.find('.avatar').length).toEqual(1);
			expect($message.find('.avatar[data-user=anotheruser]').length).toEqual(1);
			expect($message.find('.avatar[data-user=anotheruser] ~ strong').text()).toEqual('Another User');
		});
		it('does not create a comment if the field is empty', function() {
			view.$el.find('.message').val('   ');
			view.$el.find('form').submit();

			expect(createStub.notCalled).toEqual(true);
		});
		it('does not create a comment if the field length is too large', function() {
			var bigMessage = '';
			for (var i = 0; i < view._commentMaxLength * 2; i++) {
				bigMessage += 'a';
			}
			view.$el.find('.message').val(bigMessage);
			view.$el.find('form').submit();

			expect(createStub.notCalled).toEqual(true);
		});
		describe('limit indicator', function() {
			var tooltipStub;
			var $message;
			var $submitButton;

			beforeEach(function() {
				tooltipStub = sinon.stub($.fn, 'tooltip');
				$message = view.$el.find('.message');
				$submitButton = view.$el.find('.submit');
			});
			afterEach(function() { 
				tooltipStub.restore(); 
			});
			
			it('does not displays tooltip when limit is far away', function() {
				$message.val(createMessageWithLength(3));
				$message.trigger('change');

				expect(tooltipStub.calledWith('show')).toEqual(false);
				expect($submitButton.prop('disabled')).toEqual(false);
				expect($message.hasClass('error')).toEqual(false);
			});
			it('displays tooltip when limit is almost reached', function() {
				$message.val(createMessageWithLength(view._commentMaxLength - 2));
				$message.trigger('change');

				expect(tooltipStub.calledWith('show')).toEqual(true);
				expect($submitButton.prop('disabled')).toEqual(false);
				expect($message.hasClass('error')).toEqual(false);
			});
			it('displays tooltip and disabled button when limit is exceeded', function() {
				$message.val(createMessageWithLength(view._commentMaxLength + 2));
				$message.trigger('change');

				expect(tooltipStub.calledWith('show')).toEqual(true);
				expect($submitButton.prop('disabled')).toEqual(true);
				expect($message.hasClass('error')).toEqual(true);
			});
		});
	});
	describe('editing comments', function() {
		var saveStub;
		var fetchStub;
		var currentUserStub;

		beforeEach(function() {
			saveStub = sinon.stub(OCA.Comments.CommentModel.prototype, 'save');
			fetchStub = sinon.stub(OCA.Comments.CommentModel.prototype, 'fetch');
			currentUserStub = sinon.stub(OC, 'getCurrentUser');
			currentUserStub.returns({
				uid: 'testuser',
				displayName: 'Test User'
			});
			view.collection.add({
				id: 1,
				actorId: 'testuser',
				actorDisplayName: 'Test User',
				actorType: 'users',
				verb: 'comment',
				message: 'New message',
				creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 5, 9)).toUTCString()
			});
			view.collection.add({
				id: 2,
				actorId: 'anotheruser',
				actorDisplayName: 'Another User',
				actorType: 'users',
				verb: 'comment',
				message: 'New message from another user',
				creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 5, 9)).toUTCString(),
			});
			view.collection.add({
				id: 3,
				actorId: 'testuser',
				actorDisplayName: 'Test User',
				actorType: 'users',
				verb: 'comment',
				message: 'Hail to thee, @macbeth. Yours faithfully, @banquo',
				creationDateTime: new Date(Date.UTC(2016, 1, 3, 10, 5, 9)).toUTCString(),
				mentions: {
					0: {
						mentionDisplayName: "Thane of Cawdor",
						mentionId: "macbeth",
						mentionTye: "user"
					},
					1: {
						mentionDisplayName: "Lord Banquo",
						mentionId: "banquo",
						mentionTye: "user"
					}
				}
			});
		});
		afterEach(function() {
			saveStub.restore();
			fetchStub.restore();
			currentUserStub.restore();
		});

		it('shows edit link for owner comments', function() {
			var $comment = view.$el.find('.comment[data-id=1]');
			expect($comment.length).toEqual(1);
			expect($comment.find('.action.edit').length).toEqual(1);
		});

		it('does not show edit link for other user\'s comments', function() {
			var $comment = view.$el.find('.comment[data-id=2]');
			expect($comment.length).toEqual(1);
			expect($comment.find('.action.edit').length).toEqual(0);
		});

		it('shows edit form when clicking edit', function() {
			var $comment = view.$el.find('.comment[data-id=1]');
			$comment.find('.action.edit').click();

			expect($comment.hasClass('hidden')).toEqual(true);
			var $formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
			expect($formRow.length).toEqual(1);
		});

		it('saves message and updates comment item when clicking save', function() {
			var $comment = view.$el.find('.comment[data-id=1]');
			$comment.find('.action.edit').click();

			var $formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
			expect($formRow.length).toEqual(1);

			$formRow.find('textarea').val('modified message');
			$formRow.find('form').submit();

			expect(saveStub.calledOnce).toEqual(true);
			expect(saveStub.lastCall.args[0]).toEqual({
				message: 'modified message'
			});

			var model = view.collection.get(1);
			// simulate the fact that save sets the attribute
			model.set('message', 'modified\nmessage');
			saveStub.yieldTo('success', model);

			expect(fetchStub.calledOnce).toEqual(true);
			fetchStub.yieldTo('success', model);

			// original comment element is visible again
			expect($comment.hasClass('hidden')).toEqual(false);
			// and its message was updated
			expect($comment.find('.message').html()).toEqual('modified<br>message');

			// form row is gone
			$formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
			expect($formRow.length).toEqual(0);
		});

		it('saves message and updates comment item with mentions when clicking save', function() {
			var $comment = view.$el.find('.comment[data-id=3]');
			$comment.find('.action.edit').click();

			var $formRow = view.$el.find('.newCommentRow.comment[data-id=3]');
			expect($formRow.length).toEqual(1);

			$formRow.find('textarea').val('modified\nmessage @anotheruser');
			$formRow.find('form').submit();

			expect(saveStub.calledOnce).toEqual(true);
			expect(saveStub.lastCall.args[0]).toEqual({
				message: 'modified\nmessage @anotheruser'
			});

			var model = view.collection.get(3);
			// simulate the fact that save sets the attribute
			model.set('message', 'modified\nmessage @anotheruser');
			saveStub.yieldTo('success', model);

			expect(fetchStub.calledOnce).toEqual(true);

			// simulate the fact that fetch sets the attribute
			model.set('mentions', {
				0: {
					mentionDisplayName: "Another User",
					mentionId: "anotheruser",
					mentionTye: "user"
				}
			});
			fetchStub.yieldTo('success', model);

			// original comment element is visible again
			expect($comment.hasClass('hidden')).toEqual(false);
			// and its message was updated
			var $message = $comment.find('.message');
			expect($message.html()).toContain('modified<br>message');
			expect($message.find('.avatar').length).toEqual(1);
			expect($message.find('.avatar[data-user=anotheruser]').length).toEqual(1);
			expect($message.find('.avatar[data-user=anotheruser] ~ strong').text()).toEqual('Another User');

			// form row is gone
			$formRow = view.$el.find('.newCommentRow.comment[data-id=3]');
			expect($formRow.length).toEqual(0);
		});

		it('restores original comment when cancelling', function() {
			var $comment = view.$el.find('.comment[data-id=1]');
			$comment.find('.action.edit').click();

			var $formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
			expect($formRow.length).toEqual(1);

			$formRow.find('textarea').val('modified\nmessage');
			$formRow.find('.cancel').click();

			expect(saveStub.notCalled).toEqual(true);

			// original comment element is visible again
			expect($comment.hasClass('hidden')).toEqual(false);
			// and its message was not updated
			expect($comment.find('.message').html()).toEqual('New message');

			// form row is gone
			$formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
			expect($formRow.length).toEqual(0);
		});

		it('destroys model when clicking delete', function() {
			var destroyStub = sinon.stub(OCA.Comments.CommentModel.prototype, 'destroy');
			var $comment = view.$el.find('.comment[data-id=1]');
			$comment.find('.action.edit').click();

			var $formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
			expect($formRow.length).toEqual(1);

			$formRow.find('.delete').click();

			expect(destroyStub.calledOnce).toEqual(true);
			expect(destroyStub.thisValues[0].id).toEqual(1);

			destroyStub.yieldTo('success');

			// original comment element is gone
			$comment = view.$el.find('.comment[data-id=1]');
			expect($comment.length).toEqual(0);

			// form row is gone
			$formRow = view.$el.find('.newCommentRow.comment[data-id=1]');
			expect($formRow.length).toEqual(0);

			destroyStub.restore();
		});
		it('does not submit comment if the field is empty', function() {
			var $comment = view.$el.find('.comment[data-id=1]');
			$comment.find('.action.edit').click();
			$comment.find('.message').val('   ');
			$comment.find('form').submit();

			expect(saveStub.notCalled).toEqual(true);
		});
		it('does not submit comment if the field length is too large', function() {
			var $comment = view.$el.find('.comment[data-id=1]');
			$comment.find('.action.edit').click();
			$comment.find('.message').val(createMessageWithLength(view._commentMaxLength * 2));
			$comment.find('form').submit();

			expect(saveStub.notCalled).toEqual(true);
		});
	});
	describe('read marker', function() {
		var updateMarkerStub;

		beforeEach(function() {
			updateMarkerStub = sinon.stub(OCA.Comments.CommentCollection.prototype, 'updateReadMarker');
		});
		afterEach(function() { 
			updateMarkerStub.restore();
		});

		it('resets the read marker after REPORT', function() {
			testComments[0].set('isUnread', true, {silent: true});
			testComments[1].set('isUnread', true, {silent: true});
			view.collection.set(testComments);
			view.collection.trigger('sync', 'REPORT');

			expect(updateMarkerStub.calledOnce).toEqual(true);
			expect(updateMarkerStub.lastCall.args[0]).toBeFalsy();
		});
		it('does not reset the read marker if there was no unread comments', function() {
			view.collection.set(testComments);
			view.collection.trigger('sync', 'REPORT');

			expect(updateMarkerStub.notCalled).toEqual(true);
		});
		it('does not reset the read marker when posting comments', function() {
			testComments[0].set('isUnread', true, {silent: true});
			testComments[1].set('isUnread', true, {silent: true});
			view.collection.set(testComments);
			view.collection.trigger('sync', 'POST');

			expect(updateMarkerStub.notCalled).toEqual(true);
		});
	});
});