Skip to content

Return elements for ContentState.loading to prevent blinking #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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