|
| 1 | +package org.phoenixframework |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import org.junit.Before |
| 5 | +import org.junit.Test |
| 6 | +import org.mockito.Mockito |
| 7 | +import org.mockito.MockitoAnnotations |
| 8 | +import org.mockito.Spy |
| 9 | + |
| 10 | +class PhxChannelTest { |
| 11 | + |
| 12 | + private val defaultRef = "1" |
| 13 | + |
| 14 | + @Spy |
| 15 | + var socket: PhxSocket = PhxSocket("http://localhost:4000/socket/websocket") |
| 16 | + lateinit var channel: PhxChannel |
| 17 | + |
| 18 | + @Before |
| 19 | + fun setUp() { |
| 20 | + MockitoAnnotations.initMocks(this) |
| 21 | + Mockito.doReturn(defaultRef).`when`(socket).makeRef() |
| 22 | + |
| 23 | + socket.timeout = 1234 |
| 24 | + channel = PhxChannel("topic", hashMapOf("one" to "two"), socket) |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + //------------------------------------------------------------------------------ |
| 29 | + // Constructor |
| 30 | + //------------------------------------------------------------------------------ |
| 31 | + @Test |
| 32 | + fun `constructor sets defaults`() { |
| 33 | + assertThat(channel.isClosed).isTrue() |
| 34 | + assertThat(channel.topic).isEqualTo("topic") |
| 35 | + assertThat(channel.params["one"]).isEqualTo("two") |
| 36 | + assertThat(channel.socket).isEqualTo(socket) |
| 37 | + assertThat(channel.timeout).isEqualTo(1234) |
| 38 | + assertThat(channel.joinedOnce).isFalse() |
| 39 | + assertThat(channel.pushBuffer).isEmpty() |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun `constructor sets up joinPush with params`() { |
| 44 | + val joinPush = channel.joinPush |
| 45 | + |
| 46 | + assertThat(joinPush.channel).isEqualTo(channel) |
| 47 | + assertThat(joinPush.payload["one"]).isEqualTo("two") |
| 48 | + assertThat(joinPush.event).isEqualTo(PhxChannel.PhxEvent.JOIN.value) |
| 49 | + assertThat(joinPush.timeout).isEqualTo(1234) |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + //------------------------------------------------------------------------------ |
| 54 | + // Join |
| 55 | + //------------------------------------------------------------------------------ |
| 56 | + @Test |
| 57 | + fun `it sets the state to joining`() { |
| 58 | + channel.join() |
| 59 | + assertThat(channel.isJoining).isTrue() |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun `it updates the join parameters`() { |
| 64 | + channel.join(hashMapOf("one" to "three")) |
| 65 | + |
| 66 | + val joinPush = channel.joinPush |
| 67 | + assertThat(joinPush.payload["one"]).isEqualTo("three") |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `it sets joinedOnce to true`() { |
| 72 | + assertThat(channel.joinedOnce).isFalse() |
| 73 | + |
| 74 | + channel.join() |
| 75 | + assertThat(channel.joinedOnce).isTrue() |
| 76 | + } |
| 77 | + |
| 78 | + @Test(expected = IllegalStateException::class) |
| 79 | + fun `it throws if attempting to join multiple times`() { |
| 80 | + channel.join() |
| 81 | + channel.join() |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + //------------------------------------------------------------------------------ |
| 87 | + // .off() |
| 88 | + //------------------------------------------------------------------------------ |
| 89 | + @Test |
| 90 | + fun `it removes all callbacks for events`() { |
| 91 | + Mockito.doReturn(defaultRef).`when`(socket).makeRef() |
| 92 | + |
| 93 | + var aCalled = false |
| 94 | + var bCalled = false |
| 95 | + var cCalled = false |
| 96 | + |
| 97 | + channel.on("event") { aCalled = true } |
| 98 | + channel.on("event") { bCalled = true } |
| 99 | + channel.on("other") { cCalled = true } |
| 100 | + |
| 101 | + channel.off("event") |
| 102 | + |
| 103 | + channel.trigger(PhxMessage(event = "event", ref = defaultRef)) |
| 104 | + channel.trigger(PhxMessage(event = "other", ref = defaultRef)) |
| 105 | + |
| 106 | + assertThat(aCalled).isFalse() |
| 107 | + assertThat(bCalled).isFalse() |
| 108 | + assertThat(cCalled).isTrue() |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + fun `it removes callbacks by its ref`() { |
| 113 | + var aCalled = false |
| 114 | + var bCalled = false |
| 115 | + |
| 116 | + val aRef = channel.on("event") { aCalled = true } |
| 117 | + channel.on("event") { bCalled = true } |
| 118 | + |
| 119 | + |
| 120 | + channel.off("event", aRef) |
| 121 | + |
| 122 | + channel.trigger(PhxMessage(event = "event", ref = defaultRef)) |
| 123 | + |
| 124 | + assertThat(aCalled).isFalse() |
| 125 | + assertThat(bCalled).isTrue() |
| 126 | + } |
| 127 | +} |
| 128 | + |
0 commit comments