summaryrefslogtreecommitdiffstats
path: root/js/tests/controllers/feedcontrollerSpec.coffee
blob: df3917ea80ae9574a30ebc8b7ccacc052544b189 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
###

ownCloud - News

@author Bernhard Posselt
@copyright 2012 Bernhard Posselt nukeawhale@gmail.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
version 3 of the License, or any later version.

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 '_FeedController', ->


	beforeEach module 'News'


	beforeEach inject (@_FeedController, @ActiveFeed, @ShowAll, @FeedType,
		               @StarredCount, @FeedModel, @FolderModel, @ItemModel) =>
		@scope = {}
		@persistence = {
			getItems: ->
		}
		@controller = new @_FeedController(@scope, @FolderModel, @FeedModel,
			                               @ActiveFeed, @ShowAll, @FeedType,
			                               @StarredCount, @persistence,
			                               @ItemModel)


	it 'should make folders available', =>
		@FolderModel.getAll = jasmine.createSpy('FolderModel')
		new @_FeedController(@scope, @FolderModel, @FeedModel, @_ActiveFeed)

		expect(@FolderModel.getAll).toHaveBeenCalled()


	it 'should make feeds availabe', =>
		@FeedModel.getAll = jasmine.createSpy('FeedModel')
		new @_FeedController(@scope, @FolderModel, @FeedModel, @_ActiveFeed)

		expect(@FeedModel.getAll).toHaveBeenCalled()


	it 'should make feedtype available', =>
		expect(@scope.feedType).toBe(@FeedType)


	it 'should check the active feed', =>
		@ActiveFeed.getType = =>
			return @FeedType.Feed
		@ActiveFeed.getId = =>
			return 5

		expect(@scope.isFeedActive(@FeedType.Feed, 5)).toBeTruthy()


	it 'should provide ShowAll', =>
		expect(@scope.isShowAll()).toBeFalsy()
		
		@ShowAll.setShowAll(true)
		expect(@scope.isShowAll()).toBeTruthy()


	it 'should handle show all correctly', =>
		@persistence.userSettingsReadHide = jasmine.createSpy('hide')
		@persistence.userSettingsReadShow = jasmine.createSpy('show')

		@scope.setShowAll(true)
		expect(@ShowAll.getShowAll()).toBeTruthy()
		expect(@persistence.userSettingsReadShow).toHaveBeenCalled()
		expect(@persistence.userSettingsReadHide).not.toHaveBeenCalled()


	it 'should handle hide all correctly', =>
		@persistence.userSettingsReadHide = jasmine.createSpy('hide')
		@persistence.userSettingsReadShow = jasmine.createSpy('show')

		@scope.setShowAll(false)
		expect(@ShowAll.getShowAll()).toBeFalsy()
		expect(@persistence.userSettingsReadShow).not.toHaveBeenCalled()
		expect(@persistence.userSettingsReadHide).toHaveBeenCalled()


	it 'should get the correct count for starred items', =>
		@StarredCount.setStarredCount(133)
		count = @scope.getUnreadCount(@FeedType.Starred, 0)

		expect(count).toBe(133)


	it 'should set the count to 999+ if the count is over 999', =>
		@StarredCount.setStarredCount(1000)
		count = @scope.getUnreadCount(@FeedType.Starred, 0)

		expect(count).toBe('999+')


	it 'should get the correct unread count for feeds', =>
		@FeedModel.add({id: 3, unreadCount:134})
		count = @scope.getUnreadCount(@FeedType.Feed, 3)

		expect(count).toBe(134)


	it 'should get the correct unread count for subscribtions', =>
		@FeedModel.add({id: 3, unreadCount:134})
		@FeedModel.add({id: 5, unreadCount:2})
		count = @scope.getUnreadCount(@FeedType.Subscriptions, 0)

		expect(count).toBe(136)


	it 'should get the correct unread count for folders', =>
		@FeedModel.add({id: 3, unreadCount:134, folderId: 3})
		@FeedModel.add({id: 5, unreadCount:2, folderId: 2})
		@FeedModel.add({id: 1, unreadCount:12, folderId: 5})
		@FeedModel.add({id: 2, unreadCount:35, folderId: 3})
		count = @scope.getUnreadCount(@FeedType.Folder, 3)

		expect(count).toBe(169)


	it 'should reset the item cache when a different feed is being loaded', =>
		@ItemModel.clear = jasmine.createSpy('clear')
		@ActiveFeed.handle({id: 3, type: 3})
		@scope.loadFeed(3, 3)

		expect(@ItemModel.clear).not.toHaveBeenCalled()
		
		@scope.loadFeed(3, 4)
		expect(@ItemModel.clear).toHaveBeenCalled()


	it 'should send a get latest items query when feed did not change', =>
		@ItemModel.add({id: 1, lastModified: 5})
		@ItemModel.add({id: 2, lastModified: 1})
		@ItemModel.add({id: 4, lastModified: 323})
		@ItemModel.add({id: 6, lastModified: 44})
		@persistence.getItems = jasmine.createSpy('latest')
		@ActiveFeed.handle({id: 3, type: 3})
		@scope.loadFeed(3, 3)

		expect(@persistence.getItems).toHaveBeenCalledWith(3, 3, 0, null, 323)


	it 'should send a get all items query when feed changed', =>
		@persistence.getItems = jasmine.createSpy('latest')
		@ActiveFeed.handle({id: 3, type: 3})
		@scope.loadFeed(4, 3)

		expect(@persistence.getItems).toHaveBeenCalledWith(4, 3, 0)


	it 'should set active feed to new feed if changed', =>
		@ActiveFeed.handle({id: 3, type: 3})
		@scope.loadFeed(4, 3)

		expect(@ActiveFeed.getId()).toBe(3)
		expect(@ActiveFeed.getType()).toBe(4)


	it 'should return true when calling isShown and ShowAll is set to true', =>
		@ShowAll.setShowAll(true)
		expect(@scope.isShown(3, 4)).toBeTruthy()

		@ShowAll.setShowAll(false)
		expect(@scope.isShown(3, 4)).toBeFalsy()


	it 'should return true if ShowAll is false but unreadcount is not 0', =>
		@ShowAll.setShowAll(false)
		@FeedModel.add({id: 4, unreadCount: 0})
		expect(@scope.isShown(@FeedType.Feed, 4)).toBeFalsy()

		@FeedModel.add({id: 4, unreadCount: 12})
		expect(@scope.isShown(@FeedType.Feed, 4)).toBeTruthy()


	it 'should return all feeds of a folder', =>
		@FeedModel.add({id: 3, unreadCount:134, folderId: 3})
		@FeedModel.add({id: 5, unreadCount:2, folderId: 2})
		@FeedModel.add({id: 1, unreadCount:12, folderId: 5})
		@FeedModel.add({id: 2, unreadCount:35, folderId: 3})

		result = @scope.getFeedsOfFolder(3)

		expect(result).toContain(@FeedModel.getById(3))
		expect(result).toContain(@FeedModel.getById(2))
		expect(result).not.toContain(@FeedModel.getById(1))
		expect(result).not.toContain(@FeedModel.getById(5))


	it 'should return true when folder has feeds', =>
		@FeedModel.add({id: 5, unreadCount:2, folderId: 2})
		expect(@scope.hasFeeds(3)).toBeFalsy()

		@FeedModel.add({id: 2, unreadCount:35, folderId: 3})
		expect(@scope.hasFeeds(3)).toBeTruthy()


	it 'should delete feeds', =>
		@FeedModel.removeById = jasmine.createSpy('remove')
		@persistence.deleteFeed = jasmine.createSpy('deletequery')
		@scope.delete(@FeedType.Feed, 3)

		expect(@FeedModel.removeById).toHaveBeenCalledWith(3)
		expect(@persistence.deleteFeed).toHaveBeenCalledWith(3)


	it 'should delete folders', =>
		@FolderModel.removeById = jasmine.createSpy('remove')
		@persistence.deleteFolder = jasmine.createSpy('deletequery')
		@scope.delete(@FeedType.Folder, 3)

		expect(@FolderModel.removeById).toHaveBeenCalledWith(3)
		expect(@persistence.deleteFolder).toHaveBeenCalledWith(3)


	it 'should mark feed as read', =>
		@persistence.setFeedRead = jasmine.createSpy('setFeedRead')
		@FeedModel.add({