Skip to content

Commit

Permalink
Add test for parsing session update
Browse files Browse the repository at this point in the history
to validate PR #76
  • Loading branch information
ligi committed Jul 17, 2022
1 parent 1486aa2 commit 2ff8396
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class MoshiPayloadAdapter(moshi: Moshi) : Session.PayloadAdapter {
/**
* Convert FROM request bytes
*/
private fun ByteArray.toMethodCall(): Session.MethodCall =
fun ByteArray.toMethodCall(): Session.MethodCall =
String(this).let { json ->
mapAdapter.fromJson(json)?.let {
try {
Expand Down
39 changes: 39 additions & 0 deletions lib/src/test/java/org/walletconnect/TheWCSession.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.walletconnect

import com.squareup.moshi.Moshi
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.walletconnect.impls.MoshiPayloadAdapter


class TheWCSession {

@Test
fun canParseSessionUpdate() {
val adapter = MoshiPayloadAdapter(Moshi.Builder().build())
with(adapter) {
val json = """{
|"id":42,
|"result":{},
|"method":"wc_sessionUpdate",
|"params":[{
| "approved":true,
| "chainId":420,
| "accounts":["0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045","0xb8c2c29ee19d8307cb7255e1cd9cbde883a267d5"]
|}]
|}""".trimMargin()
val tested = json.toByteArray().toMethodCall()
assertThat(tested).isInstanceOf(Session.MethodCall.SessionUpdate::class.java)
assertThat(tested.id()).isEqualTo(42)
val testedAndTyped = tested as Session.MethodCall.SessionUpdate

assertThat(testedAndTyped.params.approved).isTrue
assertThat(testedAndTyped.params.accounts).isEqualTo(
listOf("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", "0xb8c2c29ee19d8307cb7255e1cd9cbde883a267d5")
)
assertThat(testedAndTyped.params.chainId).isEqualTo(420)
}

}
}

0 comments on commit 2ff8396

Please sign in to comment.