aboutsummaryrefslogtreecommitdiffstats
path: root/apps/comments/tests/js/commentscollectionSpec.js
blob: 0dc68cc167c6e82d0cb6fc6fe909b0114dfbacde (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (c) 2016
 *
 * This file is licensed under the Affero General Public License comment 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */
describe('OCA.Comments.CommentCollection', function() {
	var CommentCollection = OCA.Comments.CommentCollection;
	var collection, syncStub;
	var comment1, comment2, comment3;

	beforeEach(function() {
		syncStub = sinon.stub(CommentCollection.prototype, 'sync');
		collection = new CommentCollection();
		collection.setObjectId(5);

		comment1 = {
			id: 1,
			actorType: 'users',
			actorId: 'user1',
			actorDisplayName: 'User One',
			objectType: 'files',
			objectId: 5,
			message: 'First',
			creationDateTime: Date.UTC(2016, 1, 3, 10, 5, 0)
		};
		comment2 = {
			id: 2,
			actorType: 'users',
			actorId: 'user2',
			actorDisplayName: 'User Two',
			objectType: 'files',
			objectId: 5,
			message: 'Second\nNewline',
			creationDateTime: Date.UTC(2016, 1, 3, 10, 0, 0)
		};
		comment3 = {
			id: 3,
			actorType: 'users',
			actorId: 'user3',
			actorDisplayName: 'User Three',
			objectType: 'files',
			objectId: 5,
			message: 'Third',
			creationDateTime: Date.UTC(2016, 1, 3, 5, 0, 0)
		};
	});
	afterEach(function() { 
		syncStub.restore(); 
	});

	it('fetches the next page', function() {
		collection._limit = 2;
		collection.fetchNext();

		expect(syncStub.calledOnce).toEqual(true);
		expect(syncStub.lastCall.args[0]).toEqual('REPORT');
		var options = syncStub.lastCall.args[2];
		expect(options.remove).toEqual(false);

        var parser = new DOMParser();
        var doc = parser.parseFromString(options.data, "application/xml");
		expect(doc.getElementsByTagNameNS('http://owncloud.org/ns', 'limit')[0].textContent).toEqual('3');
		expect(doc.getElementsByTagNameNS('http://owncloud.org/ns', 'offset')[0].textContent).toEqual('0');

		syncStub.yieldTo('success', [comment1, comment2, comment3]);

		expect(collection.length).toEqual(2);
		expect(collection.hasMoreResults()).toEqual(true);

		collection.fetchNext();

		expect(syncStub.calledTwice).toEqual(true);
		options = syncStub.lastCall.args[2];
        doc = parser.parseFromString(options.data, "application/xml");
		expect(doc.getElementsByTagNameNS('http://owncloud.org/ns', 'limit')[0].textContent).toEqual('3');
		expect(doc.getElementsByTagNameNS('http://owncloud.org/ns', 'offset')[0].textContent).toEqual('2');

		syncStub.yieldTo('success', [comment3]);

		expect(collection.length).toEqual(3);
		expect(collection.hasMoreResults()).toEqual(false);

		collection.fetchNext();

		// no further requests
		expect(syncStub.calledTwice).toEqual(true);
	});
	it('resets page counted when calling reset', function() {
		collection.fetchNext();

		syncStub.yieldTo('success', [comment1]);

		expect(collection.hasMoreResults()).toEqual(false);

		collection.reset();

		expect(collection.hasMoreResults()).toEqual(true);
	});
});