mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-21 17:12:45 +02:00
Improve tests
This commit is contained in:
@@ -52,17 +52,17 @@ enum MockPollHistoryScreenState: MockScreenState, CaseIterable {
|
||||
case .past:
|
||||
pollHistoryMode = .past
|
||||
case .contentLoading:
|
||||
pollService.nextBatchPublishers.append(loadingPolls)
|
||||
pollService.nextBatchPublishers.append(MockPollPublisher.loadingPolls)
|
||||
case .empty:
|
||||
pollHistoryMode = .active
|
||||
pollService.nextBatchPublishers = [noPolls]
|
||||
pollService.nextBatchPublishers = [MockPollPublisher.emptyPolls]
|
||||
case .emptyLoading:
|
||||
pollService.nextBatchPublishers = [noPolls, loadingPolls]
|
||||
pollService.nextBatchPublishers = [MockPollPublisher.emptyPolls, MockPollPublisher.loadingPolls]
|
||||
case .emptyNoMoreContent:
|
||||
pollService.hasNextBatch = false
|
||||
pollService.nextBatchPublishers = [noPolls]
|
||||
pollService.nextBatchPublishers = [MockPollPublisher.emptyPolls]
|
||||
case .loading:
|
||||
pollService.nextBatchPublishers = [loadingPolls]
|
||||
pollService.nextBatchPublishers = [MockPollPublisher.loadingPolls]
|
||||
}
|
||||
|
||||
let viewModel = PollHistoryViewModel(mode: pollHistoryMode, pollService: pollService)
|
||||
@@ -83,12 +83,16 @@ enum MockPollHistoryScreenState: MockScreenState, CaseIterable {
|
||||
}
|
||||
}
|
||||
|
||||
private extension MockPollHistoryScreenState {
|
||||
var noPolls: AnyPublisher<TimelinePollDetails, Error> {
|
||||
enum MockPollPublisher {
|
||||
static var emptyPolls: AnyPublisher<TimelinePollDetails, Error> {
|
||||
Empty<TimelinePollDetails, Error>(completeImmediately: true).eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
var loadingPolls: AnyPublisher<TimelinePollDetails, Error> {
|
||||
static var loadingPolls: AnyPublisher<TimelinePollDetails, Error> {
|
||||
Empty<TimelinePollDetails, Error>(completeImmediately: false).eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
static var failure: AnyPublisher<TimelinePollDetails, Error> {
|
||||
Fail(error: NSError(domain: "fake", code: 1)).eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ final class PollHistoryViewModelTests: XCTestCase {
|
||||
|
||||
func testLoadingStateIsTrueWhileLoading() {
|
||||
XCTAssertFalse(viewModel.state.isLoading)
|
||||
pollHistoryService.nextBatchPublishers = [loadingPublisher, emptyPublisher]
|
||||
pollHistoryService.nextBatchPublishers = [MockPollPublisher.loadingPolls, MockPollPublisher.emptyPolls]
|
||||
viewModel.process(viewAction: .viewAppeared)
|
||||
XCTAssertTrue(viewModel.state.isLoading)
|
||||
viewModel.process(viewAction: .viewAppeared)
|
||||
@@ -83,12 +83,40 @@ final class PollHistoryViewModelTests: XCTestCase {
|
||||
}
|
||||
|
||||
func testLivePollsAreHandled() throws {
|
||||
pollHistoryService.nextBatchPublishers = [emptyPublisher]
|
||||
pollHistoryService.nextBatchPublishers = [MockPollPublisher.emptyPolls]
|
||||
pollHistoryService.livePollsPublisher = Just(mockPoll).eraseToAnyPublisher()
|
||||
viewModel.process(viewAction: .viewAppeared)
|
||||
XCTAssertEqual(viewModel.state.polls?.count, 1)
|
||||
XCTAssertEqual(viewModel.state.polls?.first?.id, "id")
|
||||
}
|
||||
|
||||
func testLivePollsDontChangeLoadingState() throws {
|
||||
let livePolls = PassthroughSubject<TimelinePollDetails, Never>()
|
||||
pollHistoryService.nextBatchPublishers = [MockPollPublisher.loadingPolls]
|
||||
pollHistoryService.livePollsPublisher = livePolls.eraseToAnyPublisher()
|
||||
viewModel.process(viewAction: .viewAppeared)
|
||||
XCTAssertTrue(viewModel.state.isLoading)
|
||||
XCTAssertNil(viewModel.state.polls)
|
||||
livePolls.send(mockPoll)
|
||||
XCTAssertTrue(viewModel.state.isLoading)
|
||||
XCTAssertNotNil(viewModel.state.polls)
|
||||
XCTAssertEqual(viewModel.state.polls?.count, 1)
|
||||
}
|
||||
|
||||
func testAfterFailureCompletionIsCalled() throws {
|
||||
let expectation = expectation(description: #function)
|
||||
|
||||
pollHistoryService.nextBatchPublishers = [MockPollPublisher.failure]
|
||||
viewModel.completion = { event in
|
||||
XCTAssertEqual(event, .genericError)
|
||||
expectation.fulfill()
|
||||
}
|
||||
viewModel.process(viewAction: .viewAppeared)
|
||||
XCTAssertFalse(viewModel.state.isLoading)
|
||||
XCTAssertNotNil(viewModel.state.polls)
|
||||
|
||||
wait(for: [expectation], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
private extension PollHistoryViewModelTests {
|
||||
@@ -98,14 +126,6 @@ private extension PollHistoryViewModelTests {
|
||||
}
|
||||
}
|
||||
|
||||
var loadingPublisher: AnyPublisher<TimelinePollDetails, Error> {
|
||||
Empty(completeImmediately: false, outputType: TimelinePollDetails.self, failureType: Error.self).eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
var emptyPublisher: AnyPublisher<TimelinePollDetails, Error> {
|
||||
Empty(completeImmediately: true, outputType: TimelinePollDetails.self, failureType: Error.self).eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
var mockPoll: TimelinePollDetails {
|
||||
.init(id: "id",
|
||||
question: "Do you like polls?",
|
||||
|
||||
Reference in New Issue
Block a user