Skip to content

Commit

Permalink
Add unit tests for autosave object handling in handleFetchedPostList
Browse files Browse the repository at this point in the history
  • Loading branch information
malinajirka committed Aug 27, 2019
1 parent 3b03a7c commit d4ceb54
Showing 1 changed file with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.wordpress.android.fluxc.post
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.argThat
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions
import com.nhaarman.mockitokotlin2.whenever
Expand Down Expand Up @@ -214,6 +215,85 @@ class PostStoreTest {
verifyNoMoreInteractions(dispatcher)
}

@Test
fun `handleFetchedPostList emits FetchPostAction when autosave object changed in remote`() {
// Arrange
val postInLocalDb = createPostModel()
whenever(postSqlUtils.getPostsByRemoteIds(any(), any())).thenReturn(listOf(postInLocalDb))

val remotePostListItem = createRemotePostListItem(postInLocalDb, autoSaveModified = "modified in remote")
val action = createFetchedPostListAction(postListItems = listOf(remotePostListItem))

// Act
store.onAction(action)

// Assert
verify(dispatcher).dispatch(argThat {
(this.type == PostAction.FETCH_POST)
})
verify(dispatcher).dispatch(argThat {
(this.type == ListAction.FETCHED_LIST_ITEMS)
})
verifyNoMoreInteractions(dispatcher)
}

/**
* We can't fetch the post from the remote as we'd override the local changes. The plan is to introduce improved
* conflict resolution on the UI and handle even the scenario for cases when the only thing that has changed is
* the autosave object. The current (temporary) solution simply ignores the fact that the auto-save object was
* updated in the remote.
*/
@Test
fun `handleFetchedPostList doesn't emit UpdatePostAction when changed locally and autosave changed in remote`() {
// Arrange
val postInLocalDb = createPostModel(isLocallyChanged = true)
whenever(postSqlUtils.getPostsByRemoteIds(any(), any())).thenReturn(listOf(postInLocalDb))

val remotePostListItem = createRemotePostListItem(postInLocalDb, autoSaveModified = "modified in remote")
val action = createFetchedPostListAction(postListItems = listOf(remotePostListItem))

// Act
store.onAction(action)

// Assert
verify(dispatcher).dispatch(argThat {
(this.type == ListAction.FETCHED_LIST_ITEMS)
})
verifyNoMoreInteractions(dispatcher)
}

/**
* This is handled as if the only thing that has changed was the post status - we invoke UpdatePostAction and
* the UI needs to take care of conflict resolution. The fact that the autosave object has changed is being
* currently ignored.
*/
@Test
fun `handleFetchedPostList emits UpdatePostAction when changed locally and status + autosave changed in remote`() {
// Arrange
val postInLocalDb = createPostModel(isLocallyChanged = true)
whenever(postSqlUtils.getPostsByRemoteIds(any(), any())).thenReturn(listOf(postInLocalDb))

val remotePostListItem = createRemotePostListItem(
postInLocalDb,
status = PostStatus.TRASHED.toString(),
autoSaveModified = "modified in remote"
)
val action = createFetchedPostListAction(postListItems = listOf(remotePostListItem))

// Act
store.onAction(action)

// Assert
verify(dispatcher).dispatch(argThat {
(this.type == PostAction.UPDATE_POST)
})
verify(dispatcher).dispatch(argThat {
(this.type == ListAction.FETCHED_LIST_ITEMS)
})
verifyNoMoreInteractions(dispatcher)
}


private fun createFetchedPostListAction(
postListItems: List<PostListItem> = listOf(),
listDescriptor: PostListDescriptor = mockedListDescriptor,
Expand Down

0 comments on commit d4ceb54

Please sign in to comment.