package net.pterodactylus.sone.web.pages import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever import net.pterodactylus.sone.utils.Pagination import net.pterodactylus.sone.web.pages.WebPageTest import net.pterodactylus.sone.web.pages.BookmarksPage import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.contains import org.hamcrest.Matchers.equalTo import org.junit.Before import org.junit.Test /** * Unit test for [BookmarksPage]. */ class BookmarksPageTest: WebPageTest() { private val page = BookmarksPage(template, webInterface) private val post1 = createLoadedPost(1000) private val post2 = createLoadedPost(3000) private val post3 = createLoadedPost(2000) private fun createLoadedPost(time: Long) = mock().apply { whenever(isLoaded).thenReturn(true) whenever(this.time).thenReturn(time) } @Before fun setupBookmarkedPostsAndPagination() { whenever(core.bookmarkedPosts).thenReturn(setOf(post1, post2, post3)) core.preferences.postsPerPage = 5 } @Test fun `page returns correct path`() { assertThat(page.path, equalTo("bookmarks.html")) } @Test @Suppress("UNCHECKED_CAST") fun `page sets correct posts in template context`() { page.processTemplate(freenetRequest, templateContext) assertThat(templateContext["posts"] as Collection, contains(post2, post3, post1)) assertThat((templateContext["pagination"] as Pagination).items, contains(post2, post3, post1)) assertThat(templateContext["postsNotLoaded"], equalTo(false)) } @Test @Suppress("UNCHECKED_CAST") fun `page does not put unloaded posts in template context but sets a flag`() { whenever(post3.isLoaded).thenReturn(false) page.processTemplate(freenetRequest, templateContext) assertThat(templateContext["posts"] as Collection, contains(post2, post1)) assertThat((templateContext["pagination"] as Pagination).items, contains(post2, post1)) assertThat(templateContext["postsNotLoaded"], equalTo(true)) } }