Add test for delete profile field ajax page
[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.mock
5 import net.pterodactylus.sone.test.whenever
6 import net.pterodactylus.sone.utils.Pagination
7 import org.hamcrest.MatcherAssert.assertThat
8 import org.hamcrest.Matchers.contains
9 import org.hamcrest.Matchers.equalTo
10 import org.junit.Before
11 import org.junit.Test
12
13 /**
14  * Unit test for [BookmarksPage].
15  */
16 class BookmarksPageTest: WebPageTest(::BookmarksPage) {
17
18         private val post1 = createLoadedPost(1000)
19         private val post2 = createLoadedPost(3000)
20         private val post3 = createLoadedPost(2000)
21
22         private fun createLoadedPost(time: Long) = mock<Post>().apply {
23                 whenever(isLoaded).thenReturn(true)
24                 whenever(this.time).thenReturn(time)
25         }
26
27         @Before
28         fun setupBookmarkedPostsAndPagination() {
29                 whenever(core.bookmarkedPosts).thenReturn(setOf(post1, post2, post3))
30                 core.preferences.postsPerPage = 5
31         }
32
33         @Test
34         fun `page returns correct path`() {
35                 assertThat(page.path, equalTo("bookmarks.html"))
36         }
37
38         @Test
39         @Suppress("UNCHECKED_CAST")
40         fun `page sets correct posts in template context`() {
41                 verifyNoRedirect {
42                         assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post3, post1))
43                         assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post3, post1))
44                         assertThat(templateContext["postsNotLoaded"], equalTo<Any>(false))
45                 }
46         }
47
48         @Test
49         @Suppress("UNCHECKED_CAST")
50         fun `page does not put unloaded posts in template context but sets a flag`() {
51                 whenever(post3.isLoaded).thenReturn(false)
52                 verifyNoRedirect {
53                         assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post1))
54                         assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post1))
55                         assertThat(templateContext["postsNotLoaded"], equalTo<Any>(true))
56                 }
57         }
58
59 }