From fd22099c698592cb631377007c08a362112cb5a2 Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Wed, 13 Nov 2024 11:31:47 -0300 Subject: [PATCH] =?UTF-8?q?Enhance=20contributor=20mock=E2=80=99s=20state?= =?UTF-8?q?=20change=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add an option for it to emit a state change when you subscribe to its state. Will use in an upcoming commit. --- .../DefaultRoomLifecycleManagerTests.swift | 6 ++++-- .../MockRoomLifecycleContributorChannel.swift | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift b/Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift index 2840837f..05bc5837 100644 --- a/Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift +++ b/Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift @@ -73,14 +73,16 @@ struct DefaultRoomLifecycleManagerTests { initialState: ARTRealtimeChannelState = .initialized, feature: RoomFeature = .messages, // Arbitrarily chosen, its value only matters in test cases where we check which error is thrown attachBehavior: MockRoomLifecycleContributorChannel.AttachOrDetachBehavior? = nil, - detachBehavior: MockRoomLifecycleContributorChannel.AttachOrDetachBehavior? = nil + detachBehavior: MockRoomLifecycleContributorChannel.AttachOrDetachBehavior? = nil, + subscribeToStateBehavior: MockRoomLifecycleContributorChannel.SubscribeToStateBehavior? = nil ) -> MockRoomLifecycleContributor { .init( feature: feature, channel: .init( initialState: initialState, attachBehavior: attachBehavior, - detachBehavior: detachBehavior + detachBehavior: detachBehavior, + subscribeToStateBehavior: subscribeToStateBehavior ) ) } diff --git a/Tests/AblyChatTests/Mocks/MockRoomLifecycleContributorChannel.swift b/Tests/AblyChatTests/Mocks/MockRoomLifecycleContributorChannel.swift index 28f6723e..b363f875 100644 --- a/Tests/AblyChatTests/Mocks/MockRoomLifecycleContributorChannel.swift +++ b/Tests/AblyChatTests/Mocks/MockRoomLifecycleContributorChannel.swift @@ -4,6 +4,7 @@ final actor MockRoomLifecycleContributorChannel: RoomLifecycleContributorChannel { private let attachBehavior: AttachOrDetachBehavior? private let detachBehavior: AttachOrDetachBehavior? + private let subscribeToStateBehavior: SubscribeToStateBehavior var state: ARTRealtimeChannelState var errorReason: ARTErrorInfo? @@ -16,11 +17,13 @@ final actor MockRoomLifecycleContributorChannel: RoomLifecycleContributorChannel init( initialState: ARTRealtimeChannelState, attachBehavior: AttachOrDetachBehavior?, - detachBehavior: AttachOrDetachBehavior? + detachBehavior: AttachOrDetachBehavior?, + subscribeToStateBehavior: SubscribeToStateBehavior? ) { state = initialState self.attachBehavior = attachBehavior self.detachBehavior = detachBehavior + self.subscribeToStateBehavior = subscribeToStateBehavior ?? .justAddSubscription } enum AttachOrDetachResult { @@ -52,6 +55,11 @@ final actor MockRoomLifecycleContributorChannel: RoomLifecycleContributorChannel } } + enum SubscribeToStateBehavior { + case justAddSubscription + case addSubscriptionAndEmitStateChange(ARTChannelStateChange) + } + func attach() async throws(ARTErrorInfo) { attachCallCount += 1 @@ -100,6 +108,14 @@ final actor MockRoomLifecycleContributorChannel: RoomLifecycleContributorChannel func subscribeToState() -> Subscription { let subscription = Subscription(bufferingPolicy: .unbounded) subscriptions.append(subscription) + + switch subscribeToStateBehavior { + case .justAddSubscription: + break + case let .addSubscriptionAndEmitStateChange(stateChange): + emitStateChange(stateChange) + } + return subscription }