♻️ Move generic error pages to custom classes
[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.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
15 import org.junit.Test
16
17 /**
18  * Unit test for [BookmarksPage].
19  */
20 class BookmarksPageTest: WebPageTest(::BookmarksPage) {
21
22         private val post1 = createLoadedPost(1000)
23         private val post2 = createLoadedPost(3000)
24         private val post3 = createLoadedPost(2000)
25
26         private fun createLoadedPost(time: Long) = mock<Post>().apply {
27                 whenever(isLoaded).thenReturn(true)
28                 whenever(this.time).thenReturn(time)
29         }
30
31         @Before
32         fun setupBookmarkedPostsAndPagination() {
33                 whenever(core.bookmarkedPosts).thenReturn(setOf(post1, post2, post3))
34                 core.preferences.newPostsPerPage = 5
35         }
36
37         @Test
38         fun `page returns correct path`() {
39                 assertThat(page.path, equalTo("bookmarks.html"))
40         }
41
42         @Test
43         @Suppress("UNCHECKED_CAST")
44         fun `page sets correct posts in template context`() {
45                 verifyNoRedirect {
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))
49                 }
50         }
51
52         @Test
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)
56                 verifyNoRedirect {
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))
60                 }
61         }
62
63         @Test
64         fun `bookmarks page can be created by dependency injection`() {
65                 assertThat(baseInjector.getInstance<BookmarksPage>(), notNullValue())
66         }
67
68         @Test
69         fun `page is annotated with correct menuname`() {
70             assertThat(page.menuName, equalTo("Bookmarks"))
71         }
72
73         @Test
74         fun `page is annotated with correct template path`() {
75             assertThat(page.templatePath, equalTo("/templates/bookmarks.html"))
76         }
77
78 }