🎨 Use new elements container in AJAX pages
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / NewPageTest.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 import java.util.Arrays.*
12
13 /**
14  * Unit test for [NewPage].
15  */
16 class NewPageTest : WebPageTest() {
17
18         private val newElements = mock<NewElements>()
19         override val page: SoneTemplatePage
20                 get() = NewPage(webInterface, loaders, templateRenderer, newElements)
21
22         @Before
23         fun setupNumberOfPostsPerPage() {
24                 webInterface.core.preferences.newPostsPerPage = 5
25         }
26
27         @Test
28         fun `page returns correct path`() {
29                 assertThat(page.path, equalTo("new.html"))
30         }
31
32         @Test
33         fun `page does not require login`() {
34                 assertThat(page.requiresLogin(), equalTo(false))
35         }
36
37         @Test
38         fun `page returns correct title`() {
39                 addTranslation("Page.New.Title", "new page title")
40                 assertThat(page.getPageTitle(soneRequest), equalTo("new page title"))
41         }
42
43         @Test
44         fun `posts are not duplicated when they come from both new posts and new replies notifications`() {
45                 val extraPost = mock<Post>().withTime(2000)
46                 val posts = asList(mock<Post>().withTime(1000), mock<Post>().withTime(3000))
47                 val postReplies = asList(mock<PostReply>(), mock())
48                 whenever(postReplies[0].post).thenReturn(posts[0].asOptional())
49                 whenever(postReplies[1].post).thenReturn(extraPost.asOptional())
50                 whenever(newElements.newPosts).thenReturn(posts)
51                 whenever(newElements.newReplies).thenReturn(postReplies)
52
53                 verifyNoRedirect {
54                         val renderedPosts = templateContext.get<List<Post>>("posts", List::class.java)
55                         assertThat(renderedPosts, containsInAnyOrder(posts[1], extraPost, posts[0]))
56                 }
57         }
58
59         private fun Post.withTime(time: Long) = apply { whenever(this.time).thenReturn(time) }
60
61         @Test
62         @Suppress("UNCHECKED_CAST")
63         fun `posts are paginated properly`() {
64                 webInterface.core.preferences.newPostsPerPage = 2
65                 val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
66                 whenever(newElements.newPosts).thenReturn(posts)
67                 verifyNoRedirect {
68                         assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[1], posts[0]))
69                 }
70         }
71
72         @Test
73         @Suppress("UNCHECKED_CAST")
74         fun `posts are paginated properly on second page`() {
75                 webInterface.core.preferences.newPostsPerPage = 2
76                 addHttpRequestParameter("page", "1")
77                 val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
78                 whenever(newElements.newPosts).thenReturn(posts)
79                 verifyNoRedirect {
80                         assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[2]))
81                 }
82         }
83
84         @Test
85         fun `page can be created by dependency injection`() {
86                 assertThat(baseInjector.getInstance<NewPage>(), notNullValue())
87         }
88
89         @Test
90         fun `page is annotated with the correct menuname`() {
91                 assertThat(page.menuName, equalTo("New"))
92         }
93
94         @Test
95         fun `page is annotated with correct template path`() {
96                 assertThat(page.templatePath, equalTo("/templates/new.html"))
97         }
98
99 }