1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.test.mock
5 import net.pterodactylus.sone.test.whenever
6 import net.pterodactylus.sone.utils.Pagination
7 import org.hamcrest.MatcherAssert.assertThat
8 import org.hamcrest.Matchers.contains
9 import org.hamcrest.Matchers.equalTo
10 import org.junit.Before
14 * Unit test for [BookmarksPage].
16 class BookmarksPageTest: WebPageTest() {
18 private val page = BookmarksPage(template, webInterface)
19 private val post1 = createLoadedPost(1000)
20 private val post2 = createLoadedPost(3000)
21 private val post3 = createLoadedPost(2000)
23 private fun createLoadedPost(time: Long) = mock<Post>().apply {
24 whenever(isLoaded).thenReturn(true)
25 whenever(this.time).thenReturn(time)
29 fun setupBookmarkedPostsAndPagination() {
30 whenever(core.bookmarkedPosts).thenReturn(setOf(post1, post2, post3))
31 core.preferences.postsPerPage = 5
35 fun `page returns correct path`() {
36 assertThat(page.path, equalTo("bookmarks.html"))
40 @Suppress("UNCHECKED_CAST")
41 fun `page sets correct posts in template context`() {
42 page.processTemplate(freenetRequest, templateContext)
43 assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post3, post1))
44 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post3, post1))
45 assertThat(templateContext["postsNotLoaded"], equalTo<Any>(false))
49 @Suppress("UNCHECKED_CAST")
50 fun `page does not put unloaded posts in template context but sets a flag`() {
51 whenever(post3.isLoaded).thenReturn(false)
52 page.processTemplate(freenetRequest, templateContext)
53 assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post1))
54 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post1))
55 assertThat(templateContext["postsNotLoaded"], equalTo<Any>(true))