136db9e9d9e8fe429a43085bf0f75b7e73f8baae
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / BookmarksPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.test.*
5 import net.pterodactylus.sone.utils.*
6 import net.pterodactylus.sone.web.*
7 import net.pterodactylus.sone.web.page.*
8 import org.hamcrest.MatcherAssert.*
9 import org.hamcrest.Matchers.*
10 import org.junit.*
11
12 /**
13  * Unit test for [BookmarksPage].
14  */
15 class BookmarksPageTest: WebPageTest(::BookmarksPage) {
16
17         private val post1 = createLoadedPost(1000)
18         private val post2 = createLoadedPost(3000)
19         private val post3 = createLoadedPost(2000)
20
21         private fun createLoadedPost(time: Long) = mock<Post>().apply {
22                 whenever(isLoaded).thenReturn(true)
23                 whenever(this.time).thenReturn(time)
24         }
25
26         @Before
27         fun setupBookmarkedPostsAndPagination() {
28                 whenever(core.bookmarkedPosts).thenReturn(setOf(post1, post2, post3))
29                 core.preferences.newPostsPerPage = 5
30         }
31
32         @Test
33         fun `page returns correct path`() {
34                 assertThat(page.path, equalTo("bookmarks.html"))
35         }
36
37         @Test
38         @Suppress("UNCHECKED_CAST")
39         fun `page sets correct posts in template context`() {
40                 verifyNoRedirect {
41                         assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post3, post1))
42                         assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post3, post1))
43                         assertThat(templateContext["postsNotLoaded"], equalTo<Any>(false))
44                 }
45         }
46
47         @Test
48         @Suppress("UNCHECKED_CAST")
49         fun `page does not put unloaded posts in template context but sets a flag`() {
50                 whenever(post3.isLoaded).thenReturn(false)
51                 verifyNoRedirect {
52                         assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post1))
53                         assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post1))
54                         assertThat(templateContext["postsNotLoaded"], equalTo<Any>(true))
55                 }
56         }
57
58         @Test
59         fun `bookmarks page can be created by dependency injection`() {
60                 assertThat(baseInjector.getInstance<BookmarksPage>(), notNullValue())
61         }
62
63         @Test
64         fun `page is annotated with correct menuname`() {
65             assertThat(page.menuName, equalTo("Bookmarks"))
66         }
67
68         @Test
69         fun `page is annotated with correct template path`() {
70             assertThat(page.templatePath, equalTo("/templates/bookmarks.html"))
71         }
72
73 }