Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Oct 9, 2024
1 parent faccb01 commit 5fbd1d3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 25 deletions.
79 changes: 57 additions & 22 deletions Tests/RealtimeTests/RealtimeChannelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,58 @@ import XCTest
import XCTestDynamicOverlay

final class RealtimeChannelTests: XCTestCase {
var sut: RealtimeChannelV2!
let sut = RealtimeChannelV2(
topic: "topic",
config: RealtimeChannelConfig(
broadcast: BroadcastJoinConfig(),
presence: PresenceJoinConfig(),
isPrivate: false
),
socket: .mock,
logger: nil
)

func testOnPostgresChange() {
sut = RealtimeChannelV2(
topic: "topic",
config: RealtimeChannelConfig(
broadcast: BroadcastJoinConfig(),
presence: PresenceJoinConfig(),
isPrivate: false
),
socket: .mock,
logger: nil
)
var subscriptions = Set<RealtimeChannelV2.Subscription>()
sut.onPostgresChange(AnyAction.self) { _ in }.store(in: &subscriptions)
sut.onPostgresChange(InsertAction.self) { _ in }.store(in: &subscriptions)
sut.onPostgresChange(UpdateAction.self) { _ in }.store(in: &subscriptions)
sut.onPostgresChange(DeleteAction.self) { _ in }.store(in: &subscriptions)
func testAttachCallbacks() {
var subscriptions = Set<RealtimeSubscription>()

sut.onPostgresChange(
AnyAction.self,
schema: "public",
table: "users",
filter: "id=eq.1"
) { _ in }.store(in: &subscriptions)
sut.onPostgresChange(
InsertAction.self,
schema: "private"
) { _ in }.store(in: &subscriptions)
sut.onPostgresChange(
UpdateAction.self,
table: "messages"
) { _ in }.store(in: &subscriptions)
sut.onPostgresChange(
DeleteAction.self
) { _ in }.store(in: &subscriptions)

sut.onBroadcast(event: "test") { _ in }.store(in: &subscriptions)
sut.onBroadcast(event: "cursor-pos") { _ in }.store(in: &subscriptions)

sut.onPresenceChange { _ in }.store(in: &subscriptions)

assertInlineSnapshot(of: sut.callbackManager.callbacks, as: .dump) {
"""
4 elements
7 elements
▿ RealtimeCallback
▿ postgres: PostgresCallback
- callback: (Function)
▿ filter: PostgresJoinConfig
▿ event: Optional<PostgresChangeEvent>
- some: PostgresChangeEvent.all
- filter: Optional<String>.none
▿ filter: Optional<String>
- some: "id=eq.1"
- id: 0
- schema: "public"
- table: Optional<String>.none
▿ table: Optional<String>
- some: "users"
- id: 1
▿ RealtimeCallback
▿ postgres: PostgresCallback
Expand All @@ -52,7 +72,7 @@ final class RealtimeChannelTests: XCTestCase {
- some: PostgresChangeEvent.insert
- filter: Optional<String>.none
- id: 0
- schema: "public"
- schema: "private"
- table: Optional<String>.none
- id: 2
▿ RealtimeCallback
Expand All @@ -64,7 +84,8 @@ final class RealtimeChannelTests: XCTestCase {
- filter: Optional<String>.none
- id: 0
- schema: "public"
- table: Optional<String>.none
▿ table: Optional<String>
- some: "messages"
- id: 3
▿ RealtimeCallback
▿ postgres: PostgresCallback
Expand All @@ -77,6 +98,20 @@ final class RealtimeChannelTests: XCTestCase {
- schema: "public"
- table: Optional<String>.none
- id: 4
▿ RealtimeCallback
▿ broadcast: BroadcastCallback
- callback: (Function)
- event: "test"
- id: 5
▿ RealtimeCallback
▿ broadcast: BroadcastCallback
- callback: (Function)
- event: "cursor-pos"
- id: 6
▿ RealtimeCallback
▿ presence: PresenceCallback
- callback: (Function)
- id: 7
"""
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/RealtimeTests/RealtimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class RealtimeTests: XCTestCase {
}
.store(in: &subscriptions)

let socketStatuses = LockIsolated([RealtimeClientV2.Status]())
let socketStatuses = LockIsolated([RealtimeClientStatus]())

sut.onStatusChange { status in
socketStatuses.withValue { $0.append(status) }
Expand All @@ -80,7 +80,7 @@ final class RealtimeTests: XCTestCase {
let heartbeatTask = sut.mutableState.heartbeatTask
XCTAssertNotNil(heartbeatTask)

let channelStatuses = LockIsolated([RealtimeChannelV2.Status]())
let channelStatuses = LockIsolated([RealtimeChannelStatus]())
channel.onStatusChange { status in
channelStatuses.withValue {
$0.append(status)
Expand Down Expand Up @@ -205,7 +205,7 @@ final class RealtimeTests: XCTestCase {
return nil
}

let statuses = LockIsolated<[RealtimeClientV2.Status]>([])
let statuses = LockIsolated<[RealtimeClientStatus]>([])

Task {
for await status in sut.statusChange {
Expand Down

0 comments on commit 5fbd1d3

Please sign in to comment.