Add and improve tests for view Sone page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 4 Jun 2017 23:11:35 +0000 (01:11 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 4 Jun 2017 23:11:35 +0000 (01:11 +0200)
src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt

index d13575d..5b3c959 100644 (file)
@@ -4,8 +4,10 @@ import net.pterodactylus.sone.data.Post
 import net.pterodactylus.sone.data.PostReply
 import net.pterodactylus.sone.data.Profile
 import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.test.isOnPage
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.Pagination
 import net.pterodactylus.sone.utils.asOptional
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.contains
@@ -28,6 +30,7 @@ class ViewSonePageTest: WebPageTest() {
        private val post2 = createPost("post2", "Second Post.", 2000, currentSone)
        private val foreignPost1 = createPost("foreign-post1", "First Foreign Post.", 1000, mock<Sone>())
        private val foreignPost2 = createPost("foreign-post2", "Second Foreign Post.", 2000, mock<Sone>())
+       private val foreignPost3 = createPost("foreign-post3", "Third Foreign Post.", 3000, mock<Sone>())
        private val directed1 = createPost("post3", "First directed.", 1500, mock<Sone>(), recipient = currentSone)
        private val directed2 = createPost("post4", "Second directed.", 2500, mock<Sone>(), recipient = currentSone)
 
@@ -96,6 +99,7 @@ class ViewSonePageTest: WebPageTest() {
                addHttpRequestParameter("sone", "sone-id")
                verifyNoRedirect {
                        assertThat(templateContext["posts"] as Iterable<Post>, contains(directed2, post2))
+                       assertThat(templateContext["postPagination"] as Pagination<Post>, isOnPage(0).hasPages(2))
                }
        }
 
@@ -107,6 +111,7 @@ class ViewSonePageTest: WebPageTest() {
                addHttpRequestParameter("postPage", "1")
                verifyNoRedirect {
                        assertThat(templateContext["posts"] as Iterable<Post>, contains(directed1, post1))
+                       assertThat(templateContext["postPagination"] as Pagination<Post>, isOnPage(1).hasPages(2))
                }
        }
 
@@ -127,16 +132,47 @@ class ViewSonePageTest: WebPageTest() {
                val reply6 = createReply("Sixth Reply", 2100, directed1)
                val reply7 = createReply("Seventh Reply", 2200, null)
                val reply8 = createReply("Eigth Reply", 2300, foreignPost1)
-               whenever(currentSone.replies).thenReturn(setOf(reply1, reply2, reply3, reply4, reply5, reply6, reply7, reply8))
+               val reply9 = createReply("Ninth Reply", 2050, foreignPost3)
+               whenever(currentSone.replies).thenReturn(setOf(reply1, reply2, reply3, reply4, reply5, reply6, reply7, reply8, reply9))
                whenever(core.getReplies("post1")).thenReturn(listOf(reply3, reply5))
                whenever(core.getReplies("post2")).thenReturn(listOf(reply4))
-               whenever(core.getReplies("foreign-post1")).thenReturn(listOf(reply1))
+               whenever(core.getReplies("foreign-post1")).thenReturn(listOf(reply8, reply1))
                whenever(core.getReplies("foreign-post2")).thenReturn(listOf(reply2))
                whenever(core.getReplies("post3")).thenReturn(listOf(reply6))
+               whenever(core.getReplies("foreign-post3")).thenReturn(listOf(reply9))
                addSone("sone-id", currentSone)
                addHttpRequestParameter("sone", "sone-id")
                verifyNoRedirect {
                        assertThat(templateContext["repliedPosts"] as Iterable<Post>, contains(foreignPost2, foreignPost1))
+                       assertThat(templateContext["repliedPostPagination"] as Pagination<Post>, isOnPage(0).hasPages(2))
+               }
+       }
+
+       @Test
+       @Suppress("UNCHECKED_CAST")
+       fun `second page of replies is shown correctly`() {
+               val reply1 = createReply("First Reply", 1500, foreignPost1)
+               val reply2 = createReply("Second Reply", 2500, foreignPost2)
+               val reply3 = createReply("Third Reply", 1750, post1)
+               val reply4 = createReply("Fourth Reply", 2250, post2)
+               val reply5 = createReply("Fifth Reply", 1600, post1)
+               val reply6 = createReply("Sixth Reply", 2100, directed1)
+               val reply7 = createReply("Seventh Reply", 2200, null)
+               val reply8 = createReply("Eigth Reply", 2300, foreignPost1)
+               val reply9 = createReply("Ninth Reply", 2050, foreignPost3)
+               whenever(currentSone.replies).thenReturn(setOf(reply1, reply2, reply3, reply4, reply5, reply6, reply7, reply8, reply9))
+               whenever(core.getReplies("post1")).thenReturn(listOf(reply3, reply5))
+               whenever(core.getReplies("post2")).thenReturn(listOf(reply4))
+               whenever(core.getReplies("foreign-post1")).thenReturn(listOf(reply8, reply1))
+               whenever(core.getReplies("foreign-post2")).thenReturn(listOf(reply2))
+               whenever(core.getReplies("post3")).thenReturn(listOf(reply6))
+               whenever(core.getReplies("foreign-post3")).thenReturn(listOf(reply9))
+               addSone("sone-id", currentSone)
+               addHttpRequestParameter("sone", "sone-id")
+               addHttpRequestParameter("repliedPostPage", "1")
+               verifyNoRedirect {
+                       assertThat(templateContext["repliedPosts"] as Iterable<Post>, contains(foreignPost3))
+                       assertThat(templateContext["repliedPostPagination"] as Pagination<Post>, isOnPage(1).hasPages(2))
                }
        }