diff --git a/CriticalMapsKit/Sources/Helpers/ContentState.swift b/CriticalMapsKit/Sources/Helpers/ContentState.swift index 36f4fd13..0ff79d75 100644 --- a/CriticalMapsKit/Sources/Helpers/ContentState.swift +++ b/CriticalMapsKit/Sources/Helpers/ContentState.swift @@ -10,7 +10,7 @@ public enum ContentState: Equatable { public var elements: T? { switch self { - case let .results(results): + case let .results(results), let .loading(results): return results default: return nil diff --git a/CriticalMapsKit/Tests/ChatFeatureTests/ChatFeatureCoreTests.swift b/CriticalMapsKit/Tests/ChatFeatureTests/ChatFeatureCoreTests.swift index b5186ad5..bb00c794 100644 --- a/CriticalMapsKit/Tests/ChatFeatureTests/ChatFeatureCoreTests.swift +++ b/CriticalMapsKit/Tests/ChatFeatureTests/ChatFeatureCoreTests.swift @@ -1,6 +1,7 @@ import ApiClient import ChatFeature import ComposableArchitecture +import Helpers import L10n import SharedModels import UserDefaultsClient @@ -11,10 +12,12 @@ final class ChatFeatureCore: XCTestCase { let uuid = { UUID(uuidString: "00000000-0000-0000-0000-000000000000")! } let date = { Date(timeIntervalSinceReferenceDate: 0) } - func defaultTestStore() -> TestStore { + func defaultTestStore( + with state: ContentState<[ChatMessage]> = .results([]) + ) -> TestStore { let testStore = TestStore( initialState: ChatFeature.State( - chatMessages: .results([]), + chatMessages: state, chatInputState: .init( isEditing: true, message: "Hello World!" @@ -49,6 +52,34 @@ final class ChatFeatureCore: XCTestCase { } } + func test_storeWithItems_shouldTriggerNetworkCall_withSuccessResponse_andHaveElements() async { + let testStore = defaultTestStore( + with: .loading([ + ChatMessage( + identifier: "ID88878", + device: "Device", + message: "Hello World!", + timestamp: 1889.1 + ) + ]) + ) + testStore.dependencies.apiService.postChatMessage = { _ in return ApiResponse(status: "ok") } + testStore.dependencies.apiService.getChatMessages = { mockResponse } + + _ = await testStore.send(.chatInput(.onCommit)) { state in + state.chatInputState.isSending = true + } + await testStore.receive(.chatInputResponse(.success(.init(status: "ok")))) { state in + state.chatInputState.isSending = false + state.chatInputState.message = "" + } + await testStore.receive(.fetchChatMessages) + XCTAssertFalse(testStore.state.chatMessages.elements!.isEmpty) + await testStore.receive(.fetchChatMessagesResponse(.success(mockResponse))) { + $0.chatMessages = .results(mockResponse) + } + } + func test_chatInputAction_onCommit_shouldTriggerNetworkCallWithFailureResponse() async { let error = NSError(domain: "", code: 1) let testStore = defaultTestStore()