Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mltbnz committed Mar 1, 2024
1 parent 7afc9da commit dd84d04
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions CriticalMapsKit/Tests/ChatFeatureTests/ChatFeatureCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ApiClient
import ChatFeature
import ComposableArchitecture
import L10n
import Helpers
import SharedModels
import UserDefaultsClient
import XCTest
Expand All @@ -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<ChatFeature.State, ChatFeature.Action, ChatFeature.State, ChatFeature.Action, ()> {
func defaultTestStore(
with state: ContentState<[ChatMessage]> = .results([])
) -> TestStore<ChatFeature.State, ChatFeature.Action, ChatFeature.State, ChatFeature.Action, ()> {
let testStore = TestStore(
initialState: ChatFeature.State(
chatMessages: .results([]),
chatMessages: state,
chatInputState: .init(
isEditing: true,
message: "Hello World!"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit dd84d04

Please sign in to comment.