Add test for DI constructability of EditAlbumAjaxPage
[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 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
14 import org.junit.Test
15
16 /**
17  * Unit test for [BookmarksPage].
18  */
19 class BookmarksPageTest: WebPageTest(::BookmarksPage) {
20
21         private val post1 = createLoadedPost(1000)
22         private val post2 = createLoadedPost(3000)
23         private val post3 = createLoadedPost(2000)
24
25         private fun createLoadedPost(time: Long) = mock<Post>().apply {
26                 whenever(isLoaded).thenReturn(true)
27                 whenever(this.time).thenReturn(time)
28         }
29
30         @Before
31         fun setupBookmarkedPostsAndPagination() {
32                 whenever(core.bookmarkedPosts).thenReturn(setOf(post1, post2, post3))
33                 core.preferences.postsPerPage = 5
34         }
35
36         @Test
37         fun `page returns correct path`() {
38                 assertThat(page.path, equalTo("bookmarks.html"))
39         }
40
41         @Test
42         @Suppress("UNCHECKED_CAST")
43         fun `page sets correct posts in template context`() {
44                 verifyNoRedirect {
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))
48                 }
49         }
50
51         @Test
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)
55                 verifyNoRedirect {
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))
59                 }
60         }
61
62         @Test
63         fun `bookmarks page can be created by dependency injection`() {
64                 assertThat(baseInjector.getInstance<BookmarksPage>(), notNullValue())
65         }
66
67 }