Skip to content
Snippets Groups Projects
MessengerGetNotificationReportsTests.swift 2.07 KiB
Newer Older
import CustomDump
import XCTest
import XCTestDynamicOverlay
import XXClient
@testable import XXMessengerClient

Dariusz Rybicki's avatar
Dariusz Rybicki committed
final class MessengerGetNotificationReportsTests: XCTestCase {
  func testGetReport() throws {
    let serviceList = MessageServiceList.stub()
    let notificationCSV = "notification-csv"
    let notificationReports = [NotificationReport].stub()

    struct GetNotificationsReportParams: Equatable {
      var notificationCSV: String
      var serviceList: MessageServiceList
    }
    var didGetNotificationsReport: [GetNotificationsReportParams] = []

    var env: MessengerEnvironment = .unimplemented
    env.serviceList.get = {
      serviceList
    }
    env.getNotificationsReport.run = { notificationCSV, serviceList in
      didGetNotificationsReport.append(.init(
        notificationCSV: notificationCSV,
        serviceList: serviceList
      ))
      return notificationReports
Dariusz Rybicki's avatar
Dariusz Rybicki committed
    let getReports: MessengerGetNotificationReports = .live(env)
Dariusz Rybicki's avatar
Dariusz Rybicki committed
    let reports = try getReports(notificationCSV: notificationCSV)

    XCTAssertNoDifference(didGetNotificationsReport, [
      .init(
        notificationCSV: notificationCSV,
        serviceList: serviceList
      )
    ])
    XCTAssertNoDifference(reports, notificationReports)
  }

  func testGetReportWhenServiceListMissing() {
    var env: MessengerEnvironment = .unimplemented
    env.e2e.get = { .unimplemented }
    env.serviceList.get = { nil }
Dariusz Rybicki's avatar
Dariusz Rybicki committed
    let getReports: MessengerGetNotificationReports = .live(env)
Dariusz Rybicki's avatar
Dariusz Rybicki committed
    XCTAssertThrowsError(try getReports(notificationCSV: "")) { error in
      XCTAssertNoDifference(
Dariusz Rybicki's avatar
Dariusz Rybicki committed
        error as? MessengerGetNotificationReports.Error,
private extension Array where Element == NotificationReport {
  static func stub() -> [NotificationReport] {
    [.stub(), .stub(), .stub()]
  }
}

private extension NotificationReport {
  static func stub() -> NotificationReport {
    NotificationReport(
      forMe: .random(),
      type: ReportType.allCases.randomElement()!,
      source: "source-\(Int.random(in: 100...999))".data(using: .utf8)!
    )
  }
}