GetDocumentsTests.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import FirebaseCombineSwift
  16. import FirebaseFirestoreTestingSupport
  17. import Combine
  18. import XCTest
  19. class GetDocumentsTests: XCTestCase {
  20. class MockQuery: QueryFake {
  21. var mockGetDocuments: () throws -> QuerySnapshot = {
  22. fatalError("You need to implement \(#function) in your mock.")
  23. }
  24. var verifySource: ((_ source: FirestoreSource) -> Void)?
  25. override func getDocuments(source: FirestoreSource,
  26. completion: @escaping (QuerySnapshot?, Error?) -> Void) {
  27. do {
  28. verifySource?(source)
  29. let snapshot = try mockGetDocuments()
  30. completion(snapshot, nil)
  31. } catch {
  32. completion(nil, error)
  33. }
  34. }
  35. }
  36. override class func setUp() {
  37. FirebaseApp.configureForTests()
  38. }
  39. override class func tearDown() {
  40. FirebaseApp.app()?.delete { success in
  41. if success {
  42. print("Shut down app successfully.")
  43. } else {
  44. print("💥 There was a problem when shutting down the app..")
  45. }
  46. }
  47. }
  48. func testGetDocumentsFailure() {
  49. // given
  50. var cancellables = Set<AnyCancellable>()
  51. let getDocumentsWasCalledExpectation = expectation(description: "getDocuments was called")
  52. let getDocumentsFailureExpectation = expectation(description: "getDocuments failed")
  53. let query = MockQuery()
  54. let source: FirestoreSource = .server
  55. query.mockGetDocuments = {
  56. getDocumentsWasCalledExpectation.fulfill()
  57. throw NSError(domain: FirestoreErrorDomain,
  58. code: FirestoreErrorCode.unknown.rawValue,
  59. userInfo: [NSLocalizedDescriptionKey: "Dummy Error"])
  60. }
  61. query.verifySource = {
  62. XCTAssertTrue(source == $0, "💥 Something went wrong: source changed")
  63. }
  64. // when
  65. query.getDocuments(source: source)
  66. .sink { completion in
  67. switch completion {
  68. case .finished:
  69. print("Finished")
  70. case let .failure(error as NSError):
  71. XCTAssertEqual(error.code, FirestoreErrorCode.unknown.rawValue)
  72. getDocumentsFailureExpectation.fulfill()
  73. }
  74. } receiveValue: { _ in
  75. XCTFail("💥 Something went wrong")
  76. }
  77. .store(in: &cancellables)
  78. // then
  79. wait(
  80. for: [getDocumentsWasCalledExpectation, getDocumentsFailureExpectation],
  81. timeout: expectationTimeout
  82. )
  83. }
  84. }