1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.test.getInstance
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import net.pterodactylus.sone.utils.Pagination
8 import net.pterodactylus.sone.web.baseInjector
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.contains
11 import org.hamcrest.Matchers.equalTo
12 import org.hamcrest.Matchers.notNullValue
13 import org.junit.Before
17 * Unit test for [BookmarksPage].
19 class BookmarksPageTest: WebPageTest(::BookmarksPage) {
21 private val post1 = createLoadedPost(1000)
22 private val post2 = createLoadedPost(3000)
23 private val post3 = createLoadedPost(2000)
25 private fun createLoadedPost(time: Long) = mock<Post>().apply {
26 whenever(isLoaded).thenReturn(true)
27 whenever(this.time).thenReturn(time)
31 fun setupBookmarkedPostsAndPagination() {
32 whenever(core.bookmarkedPosts).thenReturn(setOf(post1, post2, post3))
33 core.preferences.postsPerPage = 5
37 fun `page returns correct path`() {
38 assertThat(page.path, equalTo("bookmarks.html"))
42 @Suppress("UNCHECKED_CAST")
43 fun `page sets correct posts in template context`() {
45 assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post3, post1))
46 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post3, post1))
47 assertThat(templateContext["postsNotLoaded"], equalTo<Any>(false))
52 @Suppress("UNCHECKED_CAST")
53 fun `page does not put unloaded posts in template context but sets a flag`() {
54 whenever(post3.isLoaded).thenReturn(false)
56 assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post1))
57 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post1))
58 assertThat(templateContext["postsNotLoaded"], equalTo<Any>(true))
63 fun `bookmarks page can be created by dependency injection`() {
64 assertThat(baseInjector.getInstance<BookmarksPage>(), notNullValue())