Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarvis Lin committed May 30, 2024
1 parent b71b440 commit e60a6ab
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ class CommentDetailViewModel @Inject constructor(
}
}

/**
* Dispatch a moderation action to the server, it does not include [CommentStatus.DELETED] status
*/
fun dispatchModerationAction(site: SiteModel, comment: CommentModel, status: CommentStatus) {
commentsStoreAdapter.dispatch(
if (status == CommentStatus.DELETED) {
CommentActionBuilder.newDeleteCommentAction(CommentStore.RemoteCommentPayload(site, comment))
} else {
CommentActionBuilder.newPushCommentAction(CommentStore.RemoteCommentPayload(site, comment))
}
CommentActionBuilder.newPushCommentAction(CommentStore.RemoteCommentPayload(site, comment))
)

comment.apply { this.status = status.toString() }
Expand Down
5 changes: 2 additions & 3 deletions WordPress/src/main/res/layout/comment_approved.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/margin_extra_large"
android:layout_marginTop="@dimen/margin_large"
android:layout_marginTop="@dimen/margin_extra_small_large"
app:cardCornerRadius="20dp"
app:cardElevation="0dp"
app:strokeColor="@color/divider"
Expand All @@ -56,7 +56,6 @@
android:id="@+id/text_reply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:layout_marginStart="@dimen/margin_medium"
android:textColor="@color/menu_more"
android:lines="1"
Expand All @@ -80,7 +79,7 @@
android:textSize="@dimen/text_sz_medium"
android:drawablePadding="@dimen/margin_small"
android:layout_marginBottom="@dimen/margin_extra_medium_large"
tools:text="Like Edna’s comment"
tools:text="Like comment"
android:layout_height="wrap_content"
app:drawableStartCompat="@drawable/star_empty" />
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
@file:Suppress("DEPRECATION")

package org.wordpress.android.ui.comments

import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Before
import org.junit.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.isA
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.wordpress.android.BaseUnitTest
import org.wordpress.android.fluxc.model.CommentModel
import org.wordpress.android.fluxc.model.CommentStatus
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.comments.CommentsMapper
import org.wordpress.android.fluxc.persistence.comments.CommentsDao
import org.wordpress.android.fluxc.store.CommentsStore
import org.wordpress.android.models.Note
import org.wordpress.android.ui.comments.unified.CommentsStoreAdapter
import org.wordpress.android.ui.notifications.NotificationEvents
import org.wordpress.android.util.EventBusWrapper

@ExperimentalCoroutinesApi
class CommentDetailViewModelTest : BaseUnitTest() {
private val commentsStore: CommentsStore = mock()
private val commentsStoreAdapter: CommentsStoreAdapter = mock()
private val eventBusWrapper: EventBusWrapper = mock()
private val commentsMapper: CommentsMapper = mock()
private lateinit var viewModel: CommentDetailViewModel

@Before
fun setup() {
viewModel = CommentDetailViewModel(
testDispatcher(),
commentsStore,
commentsStoreAdapter,
eventBusWrapper,
commentsMapper
)
}

@Test
fun `when like a comment from comment list, then update comment`() {
// Given
val comment: CommentModel = mock()
val site: SiteModel = mock()

whenever(comment.iLike).thenReturn(false)

// When
viewModel.likeComment(comment, site)

// Then
verify(comment).setILike(true)
verify(commentsStoreAdapter).dispatch(any())
assert(viewModel.updatedComment.value == comment)
}

@Test
fun `when like a comment from a notification, then update comment and notification`() {
// Given
val comment: CommentModel = mock()
val site: SiteModel = mock()
val note: Note = mock()

whenever(comment.iLike).thenReturn(false)

// When
viewModel.likeComment(comment, site, note)

// Then
verify(comment).setILike(true)
verify(commentsStoreAdapter).dispatch(any())
verify(eventBusWrapper).postSticky(isA<NotificationEvents.OnNoteCommentLikeChanged>())
assert(viewModel.updatedComment.value == comment)
}

@Test
fun `when dispatch moderation action, then update comment`() {
// Given
val comment: CommentModel = mock()
val site: SiteModel = mock()

// When
viewModel.dispatchModerationAction(site, comment, CommentStatus.APPROVED)

// Then
verify(commentsStoreAdapter).dispatch(any())
verify(comment).setStatus(CommentStatus.APPROVED.toString())
assert(viewModel.updatedComment.value == comment)
}

@Test
fun `when comment fetched, then update comment`() = test {
// Given
val commentId = 123L
val site: SiteModel = mock()
val comment:CommentModel = mock()
val commentEntity: CommentsDao.CommentEntity = mock()
val result: CommentsStore.CommentsActionPayload<CommentsStore.CommentsData.CommentsActionData> = mock()

whenever(result.data).thenReturn(mock())
whenever(result.data?.comments).thenReturn(listOf(commentEntity))
whenever(commentsStore.fetchComment(any(), any(), eq(null))).thenReturn(result)
whenever(commentsMapper.commentEntityToLegacyModel(commentEntity)).thenReturn(comment)

// When
viewModel.fetchComment(site, commentId)

// Then
verify(commentsStore).fetchComment(site, commentId, null)
assert(viewModel.updatedComment.value == comment)
}
}

0 comments on commit e60a6ab

Please sign in to comment.