Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boringcactus committed Apr 10, 2024
1 parent e40b3b6 commit 66582bc
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 0 deletions.
24 changes: 24 additions & 0 deletions iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
8C7FA8712B5F2EF2009B699D /* NearbyTransitViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C7FA8702B5F2EF2009B699D /* NearbyTransitViewTests.swift */; };
8C7FA8732B5F36D6009B699D /* Backend.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C7FA8722B5F36D6009B699D /* Backend.swift */; };
8C84D33E2B5AEE0200192C0A /* NearbyTransitView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C84D33D2B5AEE0200192C0A /* NearbyTransitView.swift */; };
8CB823D62BC5E85C002C87E0 /* SheetNavigationStackEntryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CB823D52BC5E85C002C87E0 /* SheetNavigationStackEntryTests.swift */; };
8CB823D92BC5EDD2002C87E0 /* StopDetailsRouteViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CB823D82BC5EDD2002C87E0 /* StopDetailsRouteViewTests.swift */; };
8CB823DB2BC5F053002C87E0 /* StopDetailsRoutesViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CB823DA2BC5F053002C87E0 /* StopDetailsRoutesViewTests.swift */; };
8CB823DD2BC5F432002C87E0 /* StopDetailsFilteredRouteViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CB823DC2BC5F432002C87E0 /* StopDetailsFilteredRouteViewTests.swift */; };
8CC1BB402B59D1F6005386FE /* LocationDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CC1BB3F2B59D1F6005386FE /* LocationDataManager.swift */; };
8CD1F8CD2B7164C100F419D4 /* PredictionsFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CD1F8CC2B7164C100F419D4 /* PredictionsFetcher.swift */; };
8CE0140E2BBDB7C300918FAE /* RoutePillSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CE0140D2BBDB7C300918FAE /* RoutePillSection.swift */; };
Expand Down Expand Up @@ -163,6 +167,10 @@
8C7FA8702B5F2EF2009B699D /* NearbyTransitViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NearbyTransitViewTests.swift; sourceTree = "<group>"; };
8C7FA8722B5F36D6009B699D /* Backend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Backend.swift; sourceTree = "<group>"; };
8C84D33D2B5AEE0200192C0A /* NearbyTransitView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NearbyTransitView.swift; sourceTree = "<group>"; };
8CB823D52BC5E85C002C87E0 /* SheetNavigationStackEntryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SheetNavigationStackEntryTests.swift; sourceTree = "<group>"; };
8CB823D82BC5EDD2002C87E0 /* StopDetailsRouteViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopDetailsRouteViewTests.swift; sourceTree = "<group>"; };
8CB823DA2BC5F053002C87E0 /* StopDetailsRoutesViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopDetailsRoutesViewTests.swift; sourceTree = "<group>"; };
8CB823DC2BC5F432002C87E0 /* StopDetailsFilteredRouteViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopDetailsFilteredRouteViewTests.swift; sourceTree = "<group>"; };
8CC1BB3F2B59D1F6005386FE /* LocationDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationDataManager.swift; sourceTree = "<group>"; };
8CD1F8CC2B7164C100F419D4 /* PredictionsFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PredictionsFetcher.swift; sourceTree = "<group>"; };
8CE0140D2BBDB7C300918FAE /* RoutePillSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoutePillSection.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -416,6 +424,16 @@
path = StopDetails;
sourceTree = "<group>";
};
8CB823D72BC5EDBF002C87E0 /* StopDetails */ = {
isa = PBXGroup;
children = (
8CB823D82BC5EDD2002C87E0 /* StopDetailsRouteViewTests.swift */,
8CB823DA2BC5F053002C87E0 /* StopDetailsRoutesViewTests.swift */,
8CB823DC2BC5F432002C87E0 /* StopDetailsFilteredRouteViewTests.swift */,
);
path = StopDetails;
sourceTree = "<group>";
};
9A2005C02B97B56100F562E1 /* Pages */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -477,6 +495,7 @@
9A5B275D2BB242EF009A6FC6 /* Pages */ = {
isa = PBXGroup;
children = (
8CB823D72BC5EDBF002C87E0 /* StopDetails */,
9A5B275E2BB24326009A6FC6 /* Map */,
);
path = Pages;
Expand Down Expand Up @@ -535,6 +554,7 @@
isa = PBXGroup;
children = (
9ADB849F2BAD1B84006581CE /* DebouncerTests.swift */,
8CB823D52BC5E85C002C87E0 /* SheetNavigationStackEntryTests.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -823,7 +843,9 @@
9AD1D1FE2BA4D5C600182060 /* ViewportProviderTest.swift in Sources */,
9A887D592B698EF1006F5B80 /* SearchResultViewTests.swift in Sources */,
9A5B27602BB31178009A6FC6 /* StopSourceGeneratorTests.swift in Sources */,
8CB823D62BC5E85C002C87E0 /* SheetNavigationStackEntryTests.swift in Sources */,
6EE745842B965B9C0052227E /* SocketTests.swift in Sources */,
8CB823DD2BC5F432002C87E0 /* StopDetailsFilteredRouteViewTests.swift in Sources */,
9A5B27682BB36A23009A6FC6 /* StopLayerGeneratorTests.swift in Sources */,
8CEA10272BA4C83D001C6EB9 /* AlertsFetcherTests.swift in Sources */,
6EF50F482B9889D600833070 /* MockSocket.swift in Sources */,
Expand All @@ -841,7 +863,9 @@
8C7FA86F2B5EEA34009B699D /* LocationDataManagerTests.swift in Sources */,
6EED5E8F2B3DC6A00052A1B8 /* IosAppTests.swift in Sources */,
8C7FA8712B5F2EF2009B699D /* NearbyTransitViewTests.swift in Sources */,
8CB823DB2BC5F053002C87E0 /* StopDetailsRoutesViewTests.swift in Sources */,
6E4EACFC2B7A82AC0011AB8B /* MockLocationFetcher.swift in Sources */,
8CB823D92BC5EDD2002C87E0 /* StopDetailsRouteViewTests.swift in Sources */,
6EE745882B965C2B0052227E /* MessageTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// StopDetailsFilteredRouteViewTests.swift
// iosAppTests
//
// Created by Horn, Melody on 2024-04-09.
// Copyright © 2024 MBTA. All rights reserved.
//

@testable import iosApp
import shared
import SwiftUI
import ViewInspector
import XCTest

final class StopDetailsFilteredRouteViewTests: XCTestCase {
private func testData() -> (patternsByStop: PatternsByStop, routeId: String, now: Instant) {
let objects = ObjectCollectionBuilder()
let route = objects.route()
let stop = objects.stop { _ in }

let patternNorth = objects.routePattern(route: route) { pattern in
pattern.directionId = 0
pattern.representativeTrip { $0.headsign = "North" }
}
let patternSouth = objects.routePattern(route: route) { pattern in
pattern.directionId = 1
pattern.representativeTrip { $0.headsign = "South" }
}

let now = Date.now

let tripNorth = objects.trip(routePattern: patternNorth)
let predictionNorth = objects.prediction {
$0.trip = tripNorth
$0.departureTime = now.toKotlinInstant()
}
let tripSouth = objects.trip(routePattern: patternSouth)
let predictionSouth = objects.prediction {
$0.trip = tripSouth
$0.departureTime = now.toKotlinInstant()
}

let patternsByStop = PatternsByStop(route: route, stop: stop, patternsByHeadsign: [
.init(route: route, headsign: "North", patterns: [patternNorth], upcomingTrips: [objects.upcomingTrip(prediction: predictionNorth)], alertsHere: nil),
.init(route: route, headsign: "South", patterns: [patternSouth], upcomingTrips: [objects.upcomingTrip(prediction: predictionSouth)], alertsHere: nil),
])

return (patternsByStop: patternsByStop, routeId: route.id, now: now.toKotlinInstant())
}

func testAppliesFilter() throws {
let (patternsByStop: patternsByStop, routeId: routeId, now: now) = testData()

let sut = StopDetailsFilteredRouteView(
patternsByStop: patternsByStop,
now: now,
filter: .constant(.init(routeId: routeId, directionId: 0))
)

XCTAssertNotNil(try sut.inspect().find(text: "North"))
XCTAssertNil(try? sut.inspect().find(text: "South"))
}

func testClearsFilter() throws {
let (patternsByStop: patternsByStop, routeId: routeId, now: now) = testData()

let filter = Binding<StopDetailsFilter?>(wrappedValue: .init(routeId: routeId, directionId: 1))

let sut = StopDetailsFilteredRouteView(patternsByStop: patternsByStop, now: now, filter: filter)

try sut.inspect().find(button: "Clear Filter").tap()

XCTAssertNil(filter.wrappedValue)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// StopDetailsRouteViewTests.swift
// iosAppTests
//
// Created by Horn, Melody on 2024-04-09.
// Copyright © 2024 MBTA. All rights reserved.
//

@testable import iosApp
import shared
import SwiftUI
import ViewInspector
import XCTest

final class StopDetailsRouteViewTests: XCTestCase {
func testSetFilter() throws {
let objects = ObjectCollectionBuilder()
let route = objects.route()
let stop = objects.stop { _ in }

let now = Date.now.toKotlinInstant()

let filter = Binding<StopDetailsFilter?>(wrappedValue: nil)

let northPattern = objects.routePattern(route: route) { $0.directionId = 0 }
let southPattern = objects.routePattern(route: route) { $0.directionId = 1 }
let patternsByHeadsignNorth = PatternsByHeadsign(route: route, headsign: "North", patterns: [northPattern], upcomingTrips: nil, alertsHere: nil)
let patternsByHeadsignSouth = PatternsByHeadsign(route: route, headsign: "South", patterns: [southPattern], upcomingTrips: nil, alertsHere: nil)
let patternsByStop = PatternsByStop(route: route, stop: stop, patternsByHeadsign: [patternsByHeadsignNorth, patternsByHeadsignSouth])
let sut = StopDetailsRouteView(patternsByStop: patternsByStop, now: now, filter: filter)

XCTAssertNil(filter.wrappedValue)
try sut.inspect().find(button: "North").tap()
XCTAssertEqual(filter.wrappedValue, .init(routeId: route.id, directionId: 0))
try sut.inspect().find(button: "South").tap()
XCTAssertEqual(filter.wrappedValue, .init(routeId: route.id, directionId: 1))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// StopDetailsRoutesViewTests.swift
// iosAppTests
//
// Created by Horn, Melody on 2024-04-09.
// Copyright © 2024 MBTA. All rights reserved.
//

@testable import iosApp
import shared
import SwiftUI
import ViewInspector
import XCTest

final class StopDetailsRoutesViewTests: XCTestCase {
private func testData() -> (departures: StopDetailsDepartures, routeId: String) {
let objects = ObjectCollectionBuilder()
let route = objects.route()
let stop = objects.stop { _ in }
let wrongRoute = objects.route()

let departures = StopDetailsDepartures(routes: [
PatternsByStop(route: route, stop: stop, patternsByHeadsign: []),
PatternsByStop(route: wrongRoute, stop: stop, patternsByHeadsign: []),
])

return (departures: departures, routeId: route.id)
}

func testShowsListWithoutFilter() throws {
let (departures: departures, routeId: _) = testData()

let sut = StopDetailsRoutesView(departures: departures, now: Date.now.toKotlinInstant(), filter: .constant(nil))

XCTAssertNotNil(try sut.inspect().list())
XCTAssertNil(try? sut.inspect().find(StopDetailsFilteredRouteView.self))
}

func testShowsFilteredWithFilter() throws {
let (departures: departures, routeId: routeId) = testData()

let filter = StopDetailsFilter(routeId: routeId, directionId: 0)
let sut = StopDetailsRoutesView(departures: departures, now: Date.now.toKotlinInstant(), filter: .constant(filter))

let actualView = try sut.inspect().find(StopDetailsFilteredRouteView.self).actualView()
XCTAssertEqual(actualView.filter, filter)
XCTAssertEqual(actualView.patternsByStop, departures.routes[0])
XCTAssertNil(try? sut.inspect().list())
}
}
54 changes: 54 additions & 0 deletions iosApp/iosAppTests/Utils/SheetNavigationStackEntryTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// SheetNavigationStackEntryTests.swift
// iosAppTests
//
// Created by Horn, Melody on 2024-04-09.
// Copyright © 2024 MBTA. All rights reserved.
//

@testable import iosApp
import shared
import XCTest

final class SheetNavigationStackEntryTests: XCTestCase {
func testLastFilterEmpty() throws {
var stack: [SheetNavigationStackEntry] = []

XCTAssertNil(stack.lastStopDetailsFilter)

stack.lastStopDetailsFilter = .init(routeId: "A", directionId: 1)

XCTAssertEqual(stack, [])
}

func testLastFilterShallow() throws {
let stop = ObjectCollectionBuilder.Single.shared.stop { _ in }
var stack: [SheetNavigationStackEntry] = [.stopDetails(stop, nil)]

XCTAssertNil(stack.lastStopDetailsFilter)

stack.lastStopDetailsFilter = .init(routeId: "A", directionId: 1)

XCTAssertEqual(stack, [.stopDetails(stop, .init(routeId: "A", directionId: 1))])
XCTAssertEqual(stack.lastStopDetailsFilter, .init(routeId: "A", directionId: 1))

stack.lastStopDetailsFilter = nil

XCTAssertEqual(stack, [.stopDetails(stop, nil)])
XCTAssertEqual(stack.lastStopDetailsFilter, nil)
}

func testLastFilterDeep() throws {
let stop = ObjectCollectionBuilder.Single.shared.stop { _ in }
let otherStop = ObjectCollectionBuilder.Single.shared.stop { _ in }
let previousEntry = SheetNavigationStackEntry.stopDetails(otherStop, .init(routeId: "A", directionId: 1))
var stack: [SheetNavigationStackEntry] = [previousEntry, .stopDetails(stop, .init(routeId: "B", directionId: 0))]

XCTAssertEqual(stack.lastStopDetailsFilter, .init(routeId: "B", directionId: 0))

stack.lastStopDetailsFilter = nil

XCTAssertEqual(stack, [previousEntry, .stopDetails(stop, nil)])
XCTAssertEqual(stack.lastStopDetailsFilter, nil)
}
}
Loading

0 comments on commit 66582bc

Please sign in to comment.