Skip to content

Commit

Permalink
chore: revert unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ohassine committed Dec 11, 2024
1 parent faaa29c commit 246d351
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
package com.wire.android.ui.home.messagecomposer

import android.content.Context
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import com.wire.android.config.CoroutineTestExtension
import com.wire.android.config.SnapshotExtension
import com.wire.android.framework.TestConversation
Expand Down Expand Up @@ -65,7 +65,7 @@ class MessageComposerStateHolderTest {
private lateinit var messageCompositionHolder: State<MessageCompositionHolder>
private lateinit var additionalOptionStateHolder: AdditionalOptionStateHolder
private lateinit var state: MessageComposerStateHolder
private lateinit var messageTextFieldValue: MutableState<TextFieldValue>
private lateinit var messageTextState: TextFieldState

@BeforeEach
fun before() {
Expand All @@ -74,16 +74,16 @@ class MessageComposerStateHolderTest {
every { focusRequester.captureFocus() } returns true
messageComposerViewState = mutableStateOf(MessageComposerViewState())
messageComposition = mutableStateOf(MessageComposition(TestConversation.ID))
messageTextFieldValue = mutableStateOf(TextFieldValue())
messageTextState = TextFieldState()
messageCompositionInputStateHolder = MessageCompositionInputStateHolder(
messageTextFieldValue = messageTextFieldValue,
messageTextState = messageTextState,
keyboardController = null,
focusRequester = focusRequester
)
messageCompositionHolder = mutableStateOf(
MessageCompositionHolder(
messageComposition = messageComposition,
messageTextFieldValue = messageTextFieldValue,
messageTextState = messageTextState,
onClearDraft = {},
onSaveDraft = {},
onSearchMentionQueryChanged = {},
Expand Down Expand Up @@ -134,13 +134,9 @@ class MessageComposerStateHolderTest {
editMessageText = "edit_message_text",
mentions = listOf()
)

state.messageCompositionHolder.value.messageTextFieldValue.value =
messageTextFieldValue.value.copy(
text = messageTextFieldValue.value.text + "some text",
selection = TextRange(messageTextFieldValue.value.text.length + "some text".length)
)

state.messageCompositionHolder.value.messageTextState.edit {
append("some text")
}
assertInstanceOf(InputType.Editing::class.java, messageCompositionInputStateHolder.inputType).also {
assertEquals(true, it.isEditButtonEnabled)
}
Expand All @@ -154,23 +150,21 @@ class MessageComposerStateHolderTest {
state.toReply(mockMessageWithText)

// then
assertEquals(String.EMPTY, messageCompositionHolder.value.messageTextFieldValue.value.text)
assertEquals(String.EMPTY, messageCompositionHolder.value.messageTextState.text.toString())
assertInstanceOf(InputType.Composing::class.java, messageCompositionInputStateHolder.inputType)
}

@Test
fun `given some message was being composed, when setting toReply, then input continues with the current text`() = runTest {
// given
val currentText = "Potato"
messageTextFieldValue.value = messageTextFieldValue.value.copy(
text = currentText,
selection = TextRange(currentText.length)
)
messageCompositionHolder.value.messageTextState.setTextAndPlaceCursorAtEnd(currentText)

// when
state.toReply(mockMessageWithText)

// then
assertEquals(currentText, messageCompositionHolder.value.messageTextFieldValue.value.text)
assertEquals(currentText, messageCompositionHolder.value.messageTextState.text.toString())
}

@Test
Expand All @@ -197,7 +191,7 @@ class MessageComposerStateHolderTest {
// then
assertEquals(
String.EMPTY,
messageCompositionHolder.value.messageTextFieldValue.value.text
messageCompositionHolder.value.messageTextState.text.toString()
)
assertEquals(
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
package com.wire.android.ui.home.messagecomposer.state

import android.content.Context
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import com.wire.android.config.SnapshotExtension
import com.wire.android.framework.TestConversation
import com.wire.android.ui.home.messagecomposer.model.MessageComposition
Expand All @@ -47,7 +47,7 @@ class MessageCompositionHolderTest {
private lateinit var state: MessageCompositionHolder

private lateinit var messageComposition: MutableState<MessageComposition>
private lateinit var messageTextFieldValue: MutableState<TextFieldValue>
private lateinit var messageTextState: TextFieldState
private val dispatcher = StandardTestDispatcher()

@BeforeEach
Expand All @@ -56,10 +56,10 @@ class MessageCompositionHolderTest {
Dispatchers.setMain(dispatcher)

messageComposition = mutableStateOf(MessageComposition(TestConversation.ID))
messageTextFieldValue = mutableStateOf(TextFieldValue())
messageTextState = TextFieldState()
state = MessageCompositionHolder(
messageComposition = messageComposition,
messageTextFieldValue = messageTextFieldValue,
messageTextState = messageTextState,
onClearDraft = {},
onSaveDraft = {},
onSearchMentionQueryChanged = {},
Expand All @@ -82,7 +82,7 @@ class MessageCompositionHolderTest {
// then
assertEquals(
"# ",
state.messageTextFieldValue.value.text
state.messageTextState.text.toString()
)
}

Expand All @@ -95,7 +95,7 @@ class MessageCompositionHolderTest {
// then
assertEquals(
"****",
state.messageTextFieldValue.value.text
state.messageTextState.text.toString()
)
}

Expand All @@ -108,60 +108,70 @@ class MessageCompositionHolderTest {
// then
assertEquals(
"__",
state.messageTextFieldValue.value.text
state.messageTextState.text.toString()
)
}

@Test
fun `given non empty text, when adding header markdown on selection, then # is added to the text`() = runTest {
// given
val newText = "header"
state.messageTextFieldValue.value = messageTextFieldValue.value.copy(
text = newText,
selection = TextRange(0, 6)
)
state.messageTextState.edit {
replace(0, length, "header")
selection = TextRange(
start = 0,
end = 6
)
}

// when
state.addOrRemoveMessageMarkdown(markdown = RichTextMarkdown.Header)

// then
assertEquals(
"# header",
state.messageTextFieldValue.value.text
state.messageTextState.text.toString()
)
}

@Test
fun `given non empty text, when adding bold markdown on selection, then 2x star char is added to the text`() = runTest {
// given
state.messageTextFieldValue.value = messageTextFieldValue.value.copy(
text = "bold", // Replace the entire text with "bold"
selection = TextRange(0, 4)
)
state.messageTextState.edit {
replace(0, length, "bold")
selection = TextRange(
start = 0,
end = 4
)
}

// when
state.addOrRemoveMessageMarkdown(markdown = RichTextMarkdown.Bold)

// then
assertEquals(
"**bold**",
state.messageTextFieldValue.value.text
state.messageTextState.text.toString()
)
}

@Test
fun `given non empty text, when adding italic markdown on selection, then 2x _ is added to the text`() = runTest {
// given
state.messageTextFieldValue.value = messageTextFieldValue.value.copy(
text = "italic", // Replace the entire text with "bold"
selection = TextRange(0, 6)
)
state.messageTextState.edit {
replace(0, length, "italic")
selection = TextRange(
start = 0,
end = 6
)
}

// when
state.addOrRemoveMessageMarkdown(markdown = RichTextMarkdown.Italic)

// then
assertEquals(
"_italic_",
state.messageTextFieldValue.value.text
state.messageTextState.text.toString()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@

package com.wire.android.ui.home.messagecomposer.state

import androidx.compose.runtime.mutableStateOf
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.platform.SoftwareKeyboardController
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import com.wire.android.config.CoroutineTestExtension
import com.wire.android.config.SnapshotExtension
Expand Down Expand Up @@ -234,14 +233,14 @@ class MessageCompositionInputStateHolderTest {

class Arrangement {

private val textFieldValue = mutableStateOf(TextFieldValue())
private val textFieldState = TextFieldState()

val softwareKeyboardController = mockk<SoftwareKeyboardController>()

private val focusRequester = mockk<FocusRequester>()

private val state by lazy {
MessageCompositionInputStateHolder(textFieldValue, softwareKeyboardController, focusRequester)
MessageCompositionInputStateHolder(textFieldState, softwareKeyboardController, focusRequester)
}

init {
Expand All @@ -252,10 +251,7 @@ class MessageCompositionInputStateHolderTest {
}

fun withText(text: String) = apply {
textFieldValue.value = textFieldValue.value.copy(
text = text,
selection = TextRange(text.length)
)
textFieldState.setTextAndPlaceCursorAtEnd(text)
}

fun arrange() = state to this
Expand Down

0 comments on commit 246d351

Please sign in to comment.