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(::BookmarksPage) {
18 private val post1 = createLoadedPost(1000)
19 private val post2 = createLoadedPost(3000)
20 private val post3 = createLoadedPost(2000)
22 private fun createLoadedPost(time: Long) = mock<Post>().apply {
23 whenever(isLoaded).thenReturn(true)
24 whenever(this.time).thenReturn(time)
28 fun setupBookmarkedPostsAndPagination() {
29 whenever(core.bookmarkedPosts).thenReturn(setOf(post1, post2, post3))
30 core.preferences.postsPerPage = 5
34 fun `page returns correct path`() {
35 assertThat(page.path, equalTo("bookmarks.html"))
39 @Suppress("UNCHECKED_CAST")
40 fun `page sets correct posts in template context`() {
42 assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post3, post1))
43 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post3, post1))
44 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)
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))