GetDocumentsTests.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Combine
  15. import FirebaseCombineSwift
  16. import FirebaseFirestoreTestingSupport
  17. import Foundation
  18. import XCTest
  19. class GetDocumentsTests: XCTestCase {
  20. let expectationTimeout: TimeInterval = 2
  21. class MockQuery: QueryFake {
  22. var mockGetDocuments: () throws -> QuerySnapshot = {
  23. fatalError("You need to implement \(#function) in your mock.")
  24. }
  25. var verifySource: ((_ source: FirestoreSource) -> Void)?
  26. override func getDocuments(source: FirestoreSource,
  27. completion: @escaping (QuerySnapshot?, Error?) -> Void) {
  28. do {
  29. verifySource?(source)
  30. let snapshot = try mockGetDocuments()
  31. completion(snapshot, nil)
  32. } catch {
  33. completion(nil, error)
  34. }
  35. }
  36. }
  37. override class func setUp() {
  38. FirebaseApp.configureForTests()
  39. }
  40. override class func tearDown() {
  41. FirebaseApp.app()?.delete { success in
  42. if success {
  43. print("Shut down app successfully.")
  44. } else {
  45. print("💥 There was a problem when shutting down the app..")
  46. }
  47. }
  48. }
  49. func testGetDocumentsFailure() {
  50. // given
  51. var cancellables = Set<AnyCancellable>()
  52. let getDocumentsWasCalledExpectation = expectation(description: "getDocuments was called")
  53. let getDocumentsFailureExpectation = expectation(description: "getDocuments failed")
  54. let query = MockQuery()
  55. let source: FirestoreSource = .server
  56. query.mockGetDocuments = {
  57. getDocumentsWasCalledExpectation.fulfill()
  58. throw NSError(domain: FirestoreErrorDomain,
  59. code: FirestoreErrorCode.unknown.rawValue,
  60. userInfo: [NSLocalizedDescriptionKey: "Dummy Error"])
  61. }
  62. query.verifySource = {
  63. XCTAssertTrue(source == $0, "💥 Something went wrong: source changed")
  64. }
  65. // when
  66. query.getDocuments(source: source)
  67. .sink { completion in
  68. switch completion {
  69. case .finished:
  70. print("Finished")
  71. case let .failure(error as NSError):
  72. XCTAssertEqual(error.code, FirestoreErrorCode.unknown.rawValue)
  73. getDocumentsFailureExpectation.fulfill()
  74. }
  75. } receiveValue: { _ in
  76. XCTFail("💥 Something went wrong")
  77. }
  78. .store(in: &cancellables)
  79. // then
  80. wait(
  81. for: [getDocumentsWasCalledExpectation, getDocumentsFailureExpectation],
  82. timeout: expectationTimeout
  83. )
  84. }
  85. }