Skip to content

Commit 82ae5a3

Browse files
committed
Add test and further fix for member events where the user id isn’t a
string
1 parent bc4f6af commit 82ae5a3

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Source/PusherPresenceChannel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public class PresencePusherChannel: PusherChannel {
9898
if let userId = memberJSON["user_id"] as? String {
9999
id = userId
100100
} else {
101-
id = String(memberJSON["user_id"])
101+
id = String(memberJSON["user_id"]!)
102102
}
103103

104104
if let index = self.members.indexOf({ $0.userId == id }) {

Tests/PresenceChannelTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,26 @@ class PusherPresenceChannelSpec: QuickSpec {
155155
expect((stubber.calls.last?.args?.first as? PresenceChannelMember)?.userId).to(equal("100"))
156156
}
157157

158+
it("calls the onMemberRemoved function, if provided, and the userId of the member when they were addded was not a string") {
159+
let options = PusherClientOptions(
160+
authMethod: .Internal(secret: "secret")
161+
)
162+
pusher = Pusher(key: "key", options: options)
163+
socket.delegate = pusher.connection
164+
pusher.connection.socket = socket
165+
pusher.connection.userDataFetcher = { () -> PusherUserData in
166+
return PusherUserData(userId: "123")
167+
}
168+
pusher.connect()
169+
let memberRemovedFunction = { (member: PresenceChannelMember) -> Void in stubber.stub("onMemberRemoved", args: [member], functionToCall: nil) }
170+
pusher.subscribe("presence-channel", onMemberAdded: nil, onMemberRemoved: memberRemovedFunction) as? PresencePusherChannel
171+
pusher.connection.handleEvent("pusher_internal:member_added", jsonObject: ["event": "pusher_internal:member_added", "channel": "presence-channel", "data": "{\"user_id\":100}"])
172+
pusher.connection.handleEvent("pusher_internal:member_removed", jsonObject: ["event": "pusher_internal:member_removed", "channel": "presence-channel", "data": "{\"user_id\":100}"])
173+
174+
expect(stubber.calls.last?.name).to(equal("onMemberRemoved"))
175+
expect((stubber.calls.last?.args?.first as? PresenceChannelMember)?.userId).to(equal("100"))
176+
}
177+
158178
}
159179
}
160180
}

0 commit comments

Comments
 (0)