Skip to content

Commit f38a5f3

Browse files
committed
Add tests and make small function call change
1 parent a9df484 commit f38a5f3

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

Sources/PusherConnection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public typealias PusherEventJSON = [String: AnyObject]
182182
*/
183183
internal func unsubscribeAll() {
184184
for (_, channel) in channels.channels {
185-
unsubscribe(channel.name)
185+
unsubscribe(channelName: channel.name)
186186
}
187187
}
188188

Tests/Mocks.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ open class MockWebSocket: WebSocket {
6060
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"test-channel\",\"data\":\"{}\"}")
6161
}
6262
)
63+
} else if string == "{\"data\":{\"channel\":\"test-channel2\"},\"event\":\"pusher:subscribe\"}" || string == "{\"event\":\"pusher:subscribe\",\"data\":{\"channel\":\"test-channel2\"}}" {
64+
let _ = stubber.stub(
65+
functionName: "writeString",
66+
args: [string],
67+
functionToCall: {
68+
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"test-channel2\",\"data\":\"{}\"}")
69+
}
70+
)
6371
} else if stringContainsElements(string, elements: ["testkey123:6aae8814fabd5285245422096705abbed64ea59614648814ffb0bf2dc5d19168", "private-channel", "pusher:subscribe"]) {
6472
let _ = stubber.stub(
6573
functionName: "writeString",
@@ -144,6 +152,12 @@ open class MockWebSocket: WebSocket {
144152
args: [string],
145153
functionToCall: nil
146154
)
155+
} else if stringContainsElements(string, elements: ["test-channel2", "pusher:unsubscribe"]) {
156+
let _ = stubber.stub(
157+
functionName: "writeString",
158+
args: [string],
159+
functionToCall: nil
160+
)
147161
} else if stringContainsElements(string, elements: ["testkey123:736f0b19c2e56f985f3e6faa38db5b69d39305bc8519952c8f9f5595d69fcb3d", "presence-test", "user_id", "123", "pusher:subscribe", "user_info", "twitter", "hamchapman"]) {
148162
let _ = stubber.stub(
149163
functionName: "writeString",

Tests/PusherTopLevelAPITests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,24 @@ class PusherTopLevelApiTests: XCTestCase {
232232
XCTAssertTrue(parsedEqualsExpected)
233233
}
234234

235+
func testUnsubscribingFromAllChannelsRemovesTheChannels() {
236+
pusher.connect()
237+
let _ = pusher.subscribe("test-channel")
238+
let _ = pusher.subscribe("test-channel2")
239+
XCTAssertEqual(pusher.connection.channels.channels.count, 2, "should have 2 channels")
240+
XCTAssertEqual(socket.stubber.calls.last?.name, "writeString", "write function should have been called")
241+
pusher.unsubscribeAll()
242+
243+
XCTAssertEqual(socket.stubber.calls.last?.name, "writeString", "write function should have been called")
244+
245+
let parsedSubscribeArgs = convertStringToDictionary(socket.stubber.calls.last?.args!.first as! String)
246+
let expectedDict = ["data": ["channel": "test-channel2"], "event": "pusher:unsubscribe"] as [String: Any]
247+
let parsedEqualsExpected = NSDictionary(dictionary: parsedSubscribeArgs!).isEqual(to: NSDictionary(dictionary: expectedDict) as [NSObject: AnyObject])
248+
249+
XCTAssertTrue(parsedEqualsExpected)
250+
XCTAssertEqual(pusher.connection.channels.channels.count, 0, "should have no channels")
251+
}
252+
235253
/* global channel interactions */
236254

237255
func testBindingToEventsGloballyAddsACallbackToTheGlobalChannel() {

0 commit comments

Comments
 (0)