Skip to content

Commit

Permalink
Merge pull request #8 from hotwired/new-message
Browse files Browse the repository at this point in the history
Create new message instance from existing message
  • Loading branch information
jayohms authored Mar 30, 2023
2 parents 0bdb9bb + 829c6bb commit 54966ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
17 changes: 16 additions & 1 deletion strada/src/main/kotlin/dev/hotwire/strada/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@ data class Message(
* For a "page" component, this might be `{"title": "Page Title"}`
*/
val jsonData: String
)
) {
/**
* Convenience method for creating a new message from an existing message,
* replacing its `event` and/or `jsonData`. The new message can be sent
* back to the over the bridge to notify the web of a change/action.
*/
fun replacing(
event: String = this.event,
jsonData: String = this.jsonData
) = Message(
id = this.id,
component = this.component,
event = event,
jsonData = jsonData
)
}
26 changes: 26 additions & 0 deletions strada/src/test/kotlin/dev/hotwire/strada/MessageTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.hotwire.strada

import org.junit.Assert.assertEquals
import org.junit.Test

class MessageTest {
@Test
fun replacing() {
val message = Message(
id = "1",
component = "page",
event = "connect",
jsonData = """{"title":"Page-title","subtitle":"Page-subtitle"}"""
)

val newMessage = message.replacing(
event = "disconnect",
jsonData = "{}"
)

assertEquals("1", newMessage.id)
assertEquals("page", newMessage.component)
assertEquals("disconnect", newMessage.event)
assertEquals("{}", newMessage.jsonData)
}
}

0 comments on commit 54966ca

Please sign in to comment.