-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bluetooth/Usb automatic reconnection #516
Changes from all commits
7f8ed62
3b010b5
05ff404
c94b21f
9e82916
5684d82
d72fba5
5aa434b
e6e9738
d734481
351e56f
3455132
dd941e0
faa72e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.stripeterminalreactnative.listener | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.stripe.stripeterminal.external.callable.Cancelable | ||
import com.stripe.stripeterminal.external.callable.ReaderReconnectionListener | ||
import com.stripe.stripeterminal.external.models.Reader | ||
import com.stripe.stripeterminal.external.models.TerminalException.TerminalErrorCode | ||
import com.stripeterminalreactnative.ReactExtensions.sendEvent | ||
import com.stripeterminalreactnative.ReactNativeConstants.* | ||
import com.stripeterminalreactnative.mapFromReader | ||
import com.stripeterminalreactnative.nativeMapOf | ||
|
||
class RNReaderReconnectionListener( | ||
private val context: ReactApplicationContext, | ||
private val onReaderReconnectStarted: (cancelable: Cancelable?) -> Unit, | ||
) : ReaderReconnectionListener { | ||
|
||
override fun onReaderReconnectFailed(reader: Reader) { | ||
context.sendEvent(READER_RECONNECT_FAIL.listenerName) { | ||
putMap("error", nativeMapOf { | ||
putString("code", TerminalErrorCode.UNEXPECTED_SDK_ERROR.toString()) | ||
putString("message", "Reader reconnect fail") | ||
}) | ||
} | ||
} | ||
|
||
override fun onReaderReconnectStarted(reader: Reader, cancelReconnect: Cancelable) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to deal with the cc @chr-stripe |
||
onReaderReconnectStarted(cancelReconnect) | ||
context.sendEvent(START_READER_RECONNECT.listenerName) { | ||
putMap("reader", mapFromReader(reader)) | ||
} | ||
} | ||
|
||
override fun onReaderReconnectSucceeded(reader: Reader) { | ||
context.sendEvent(READER_RECONNECT_SUCCEED.listenerName) { | ||
putMap("reader", mapFromReader(reader)) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.stripeterminalreactnative.listener | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.stripe.stripeterminal.external.callable.Cancelable | ||
import com.stripe.stripeterminal.external.models.Reader | ||
import com.stripeterminalreactnative.* | ||
import com.stripeterminalreactnative.ReactExtensions.sendEvent | ||
import com.stripeterminalreactnative.ReactNativeConstants.READER_RECONNECT_FAIL | ||
import com.stripeterminalreactnative.ReactNativeConstants.START_READER_RECONNECT | ||
import com.stripeterminalreactnative.ReactNativeConstants.READER_RECONNECT_SUCCEED | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import org.junit.ClassRule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
import kotlin.test.assertTrue | ||
|
||
@RunWith(JUnit4::class) | ||
class RNReaderReconnectionListenerTest { | ||
|
||
companion object { | ||
@ClassRule | ||
@JvmField | ||
val typeReplacer = ReactNativeTypeReplacementRule() | ||
} | ||
|
||
private val context = mockk<ReactApplicationContext>() | ||
|
||
@Test | ||
fun `should send onReaderReconnectFailed event`() { | ||
val mockOnReaderReconnectStarted = mockk<(Cancelable?) -> Unit>(relaxed = true) | ||
val reader = mockk<Reader>(relaxed = true) | ||
val listener = RNReaderReconnectionListener(context,mockOnReaderReconnectStarted) | ||
listener.onReaderReconnectFailed(reader) | ||
|
||
verify(exactly = 1) { | ||
context.sendEvent(READER_RECONNECT_FAIL.listenerName, any()) | ||
} | ||
|
||
assertTrue(typeReplacer.sendEventSlot.captured.hasValue("error")) | ||
} | ||
|
||
@Test | ||
fun `should send onReaderReconnectStarted event`() { | ||
val mockOnReaderReconnectStarted = mockk<(Cancelable?) -> Unit>(relaxed = true) | ||
val mockCancelable = mockk<Cancelable>() | ||
val reader = mockk<Reader>(relaxed = true) | ||
val listener = RNReaderReconnectionListener(context,mockOnReaderReconnectStarted) | ||
listener.onReaderReconnectStarted(reader,mockCancelable) | ||
|
||
verify(exactly = 1) { mockOnReaderReconnectStarted.invoke(mockCancelable) } | ||
verify(exactly = 1) { context.sendEvent(START_READER_RECONNECT.listenerName, any()) } | ||
|
||
assertTrue(typeReplacer.sendEventSlot.captured.hasValue("reader")) | ||
} | ||
|
||
@Test | ||
fun `should send onReaderReconnectSucceeded event`() { | ||
val mockOnReaderReconnectStarted = mockk<(Cancelable?) -> Unit>(relaxed = true) | ||
val reader = mockk<Reader>(relaxed = true) | ||
val listener = RNReaderReconnectionListener(context,mockOnReaderReconnectStarted) | ||
listener.onReaderReconnectSucceeded(reader) | ||
|
||
verify(exactly = 1) { context.sendEvent(READER_RECONNECT_SUCCEED.listenerName, any()) } | ||
|
||
assertTrue(typeReplacer.sendEventSlot.captured.hasValue("reader")) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add tests for this? example: https://github.com/stripe/stripe-terminal-react-native/blob/1870117c/android/src/test/java/com/stripeterminalreactnative/listener/RNBluetoothReaderListenerTest.kt#L1