Skip to content

Commit

Permalink
Return elements for ContentState.loading to prevent blinking (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
mltbnz committed Mar 5, 2024
1 parent f2955dc commit 1b8b6f0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CriticalMapsKit/Sources/Helpers/ContentState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public enum ContentState<T: Hashable>: Equatable {

public var elements: T? {
switch self {
case let .results(results):
case let .results(results), let .loading(results):
return results
default:
return nil
Expand Down
35 changes: 33 additions & 2 deletions CriticalMapsKit/Tests/ChatFeatureTests/ChatFeatureCoreTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ApiClient
import ChatFeature
import ComposableArchitecture
import Helpers
import L10n
import SharedModels
import UserDefaultsClient
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 1b8b6f0

Please sign in to comment.