Skip to content

Commit

Permalink
Merge pull request #28 from hotwired/component-testing
Browse files Browse the repository at this point in the history
Open up the component's internal methods for testing
  • Loading branch information
jayohms authored Aug 22, 2023
2 parents 427024f + 77a8428 commit ed745c2
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions strada/src/main/kotlin/dev/hotwire/strada/BridgeComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,6 @@ abstract class BridgeComponent<in D : BridgeDestination>(
) {
private val receivedMessages = hashMapOf<String, Message>()

internal fun didReceive(message: Message) {
receivedMessages[message.event] = message
onReceive(message)
}

internal fun didStart() {
onStart()
}

internal fun didStop() {
onStop()
}

/**
* Returns the last received message for a given `event`, if available.
*/
Expand All @@ -32,6 +19,38 @@ abstract class BridgeComponent<in D : BridgeDestination>(
*/
abstract fun onReceive(message: Message)

/**
* This passes a received message to onReceive(message), caching it
* for use with replyTo(event) and receivedMessageFor(event).
*
* NOTE: This should not be called directly from within a component,
* but is available to use for testing.
*/
fun didReceive(message: Message) {
receivedMessages[message.event] = message
onReceive(message)
}

/**
* This passes the start lifecycle event to onStart().
*
* NOTE: This should not be called directly from within a component,
* but is available to use for testing.
*/
fun didStart() {
onStart()
}

/**
* This passes the stop lifecycle event to onStop().
*
* NOTE: This should not be called directly from within a component,
* but is available to use for testing.
*/
fun didStop() {
onStop()
}

/**
* Called when the component's destination starts (and is active)
* based on its lifecycle events. You can use this as an opportunity
Expand Down

0 comments on commit ed745c2

Please sign in to comment.