Add timeout expectations to LibraryRefreshViewModelTest

This commit is contained in:
Balazs Perlaki-Horvath 2025-06-15 20:13:17 +02:00 committed by BPH
parent dec57d697a
commit 560330d70e

View File

@ -17,6 +17,7 @@ import CoreData
import XCTest
import Defaults
import Combine
@testable import Kiwix
private class HTTPTestingURLProtocol: URLProtocol {
@ -37,6 +38,7 @@ private class HTTPTestingURLProtocol: URLProtocol {
final class LibraryRefreshViewModelTest: XCTestCase {
private var urlSession: URLSession?
private var cancellables = Set<AnyCancellable>()
override func setUpWithError() throws {
let config = URLSessionConfiguration.ephemeral
@ -254,11 +256,34 @@ final class LibraryRefreshViewModelTest: XCTestCase {
processFactory: { LibraryProcess(defaultState: .initial) },
defaults: testDefaults,
categories: CategoriesToLanguages(withDefaults: testDefaults))
let expectationVMComplete = XCTestExpectation(description: "viewModel completed")
viewModel.$state.sink { value in
switch value {
case .complete:
expectationVMComplete.fulfill()
default:
break;
}
}.store(in: &cancellables)
await viewModel.start(isUserInitiated: true)
await fulfillment(of: [expectationVMComplete], timeout: 2)
let zimFile1 = try XCTUnwrap(try context.fetch(ZimFile.fetchRequest()).first)
// refresh library for the second time, which should replace the old zim file with a new one
let expectationVMComplete2 = XCTestExpectation(description: "viewModel completed")
viewModel.$state.sink { value in
switch value {
case .complete:
expectationVMComplete2.fulfill()
default:
break;
}
}.store(in: &cancellables)
await viewModel.start(isUserInitiated: true)
await fulfillment(of: [expectationVMComplete2], timeout: 2)
var zimFiles = try context.fetch(ZimFile.fetchRequest())
XCTAssertEqual(zimFiles.count, 1)
let zimFile2 = try XCTUnwrap(zimFiles.first)