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 net.pterodactylus.sone.web.page.*
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.contains
12 import org.hamcrest.Matchers.equalTo
13 import org.hamcrest.Matchers.notNullValue
14 import org.junit.Before
18 * Unit test for [BookmarksPage].
20 class BookmarksPageTest: WebPageTest(::BookmarksPage) {
22 private val post1 = createLoadedPost(1000)
23 private val post2 = createLoadedPost(3000)
24 private val post3 = createLoadedPost(2000)
26 private fun createLoadedPost(time: Long) = mock<Post>().apply {
27 whenever(isLoaded).thenReturn(true)
28 whenever(this.time).thenReturn(time)
32 fun setupBookmarkedPostsAndPagination() {
33 whenever(core.bookmarkedPosts).thenReturn(setOf(post1, post2, post3))
34 core.preferences.newPostsPerPage = 5
38 fun `page returns correct path`() {
39 assertThat(page.path, equalTo("bookmarks.html"))
43 @Suppress("UNCHECKED_CAST")
44 fun `page sets correct posts in template context`() {
46 assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post3, post1))
47 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post3, post1))
48 assertThat(templateContext["postsNotLoaded"], equalTo<Any>(false))
53 @Suppress("UNCHECKED_CAST")
54 fun `page does not put unloaded posts in template context but sets a flag`() {
55 whenever(post3.isLoaded).thenReturn(false)
57 assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post1))
58 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post1))
59 assertThat(templateContext["postsNotLoaded"], equalTo<Any>(true))
64 fun `bookmarks page can be created by dependency injection`() {
65 assertThat(baseInjector.getInstance<BookmarksPage>(), notNullValue())
69 fun `page is annotated with correct menuname`() {
70 assertThat(page.menuName, equalTo("Bookmarks"))
74 fun `page is annotated with correct template path`() {
75 assertThat(page.templatePath, equalTo("/templates/bookmarks.html"))