Skip to content

Commit

Permalink
test: Add test for stop page changing
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaSimon committed Apr 10, 2024
1 parent 1573010 commit 3b507a9
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 14 deletions.
20 changes: 20 additions & 0 deletions iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
9A635D1F2B99103200A43C51 /* EmptyWhenModifierTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A635D1E2B99103200A43C51 /* EmptyWhenModifierTests.swift */; };
9A69D4902B99212400235125 /* NearbyFetcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A69D48F2B99212300235125 /* NearbyFetcherTests.swift */; };
9A6DDF912B976FDF004D141A /* EmptyWhenModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A6DDF902B976FDF004D141A /* EmptyWhenModifier.swift */; };
9A6FA0212BC7090B0067769C /* StopDetailsPageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A6FA0202BC7090B0067769C /* StopDetailsPageTests.swift */; };
9A6FA0232BC70D0B0067769C /* InspectionEmissary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A6FA0222BC70D0A0067769C /* InspectionEmissary.swift */; };
9A6FA0252BC714360067769C /* HomeMapViewTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A6FA0242BC714360067769C /* HomeMapViewTest.swift */; };
9A7B7CA92B98E41B0045214F /* NonNilModifierTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A7B7CA82B98E41B0045214F /* NonNilModifierTests.swift */; };
9A887D572B683103006F5B80 /* SearchResultView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A887D562B683103006F5B80 /* SearchResultView.swift */; };
9A887D592B698EF1006F5B80 /* SearchResultViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A887D582B698EF1006F5B80 /* SearchResultViewTests.swift */; };
Expand Down Expand Up @@ -198,6 +201,9 @@
9A635D1E2B99103200A43C51 /* EmptyWhenModifierTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyWhenModifierTests.swift; sourceTree = "<group>"; };
9A69D48F2B99212300235125 /* NearbyFetcherTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NearbyFetcherTests.swift; sourceTree = "<group>"; };
9A6DDF902B976FDF004D141A /* EmptyWhenModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyWhenModifier.swift; sourceTree = "<group>"; };
9A6FA0202BC7090B0067769C /* StopDetailsPageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopDetailsPageTests.swift; sourceTree = "<group>"; };
9A6FA0222BC70D0A0067769C /* InspectionEmissary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InspectionEmissary.swift; sourceTree = "<group>"; };
9A6FA0242BC714360067769C /* HomeMapViewTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeMapViewTest.swift; sourceTree = "<group>"; };
9A7B7CA82B98E41B0045214F /* NonNilModifierTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NonNilModifierTests.swift; sourceTree = "<group>"; };
9A887D562B683103006F5B80 /* SearchResultView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultView.swift; sourceTree = "<group>"; };
9A887D582B698EF1006F5B80 /* SearchResultViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultViewTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -299,6 +305,7 @@
6EED5EA92B3DF1BF0052A1B8 /* Views */,
6EED5E8E2B3DC6A00052A1B8 /* IosAppTests.swift */,
8C7FA86E2B5EEA34009B699D /* LocationDataManagerTests.swift */,
9A6FA0222BC70D0A0067769C /* InspectionEmissary.swift */,
);
path = iosAppTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -474,6 +481,7 @@
9A5B275D2BB242EF009A6FC6 /* Pages */ = {
isa = PBXGroup;
children = (
9A6FA01F2BC708D80067769C /* StopDetails */,
9A5B275E2BB24326009A6FC6 /* Map */,
);
path = Pages;
Expand All @@ -487,10 +495,19 @@
9A5B27632BB3621F009A6FC6 /* RouteLayerGeneratorTests.swift */,
9A5B27672BB36A23009A6FC6 /* StopLayerGeneratorTests.swift */,
9A5B27652BB3631F009A6FC6 /* MapTestDataHelper.swift */,
9A6FA0242BC714360067769C /* HomeMapViewTest.swift */,
);
path = Map;
sourceTree = "<group>";
};
9A6FA01F2BC708D80067769C /* StopDetails */ = {
isa = PBXGroup;
children = (
9A6FA0202BC7090B0067769C /* StopDetailsPageTests.swift */,
);
path = StopDetails;
sourceTree = "<group>";
};
9A9E05F22B6D6D9F0086B437 /* Fetchers */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -817,14 +834,17 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
9A6FA0252BC714360067769C /* HomeMapViewTest.swift in Sources */,
9AD1D1FE2BA4D5C600182060 /* ViewportProviderTest.swift in Sources */,
9A887D592B698EF1006F5B80 /* SearchResultViewTests.swift in Sources */,
9A5B27602BB31178009A6FC6 /* StopSourceGeneratorTests.swift in Sources */,
9A6FA0232BC70D0B0067769C /* InspectionEmissary.swift in Sources */,
6EE745842B965B9C0052227E /* SocketTests.swift in Sources */,
9A5B27682BB36A23009A6FC6 /* StopLayerGeneratorTests.swift in Sources */,
8CEA10272BA4C83D001C6EB9 /* AlertsFetcherTests.swift in Sources */,
6EF50F482B9889D600833070 /* MockSocket.swift in Sources */,
9A5B27662BB3631F009A6FC6 /* MapTestDataHelper.swift in Sources */,
9A6FA0212BC7090B0067769C /* StopDetailsPageTests.swift in Sources */,
9ADB84A02BAD1B84006581CE /* DebouncerTests.swift in Sources */,
6EF50F512B988BF600833070 /* PredictionsFetcherTests.swift in Sources */,
9A5B27622BB32621009A6FC6 /* RouteSourceGeneratorTests.swift in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions iosApp/iosApp/Pages/Map/HomeMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct HomeMapView: View {
@State private var now = Date.now
@State private var currentStopAlerts: [String: AlertAssociatedStop] = [:]

let inspection = Inspection<Self>()
let timer = Timer.publish(every: 300, on: .main, in: .common).autoconnect()
let log = Logger()

Expand Down Expand Up @@ -98,6 +99,7 @@ struct HomeMapView: View {
.onChange(of: currentStopAlerts) { nextStopAlerts in
handleStopAlertChange(alertsByStop: nextStopAlerts)
}
.onReceive(inspection.notice) { inspection.visit(self, $0) }
.overlay(alignment: .topTrailing) {
if !viewportProvider.viewport.isFollowing, locationDataManager.currentLocation != nil {
RecenterButton { Task { viewportProvider.follow() } }
Expand Down
18 changes: 6 additions & 12 deletions iosApp/iosApp/Pages/StopDetails/StopDetailsPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct StopDetailsPage: View {
var filter: StopDetailsFilter?
@State var now = Date.now

let inspection = Inspection<Self>()
let timer = Timer.publish(every: 5, on: .main, in: .common).autoconnect()

init(
Expand Down Expand Up @@ -61,18 +62,11 @@ struct StopDetailsPage: View {
}
}
.navigationTitle(stop.name)
.onAppear {
changeStop(stop)
}
.onChange(of: stop) { nextStop in
changeStop(nextStop)
}
.onReceive(timer) { input in
now = input
}
.onDisappear {
leavePredictions()
}
.onAppear { changeStop(stop) }
.onChange(of: stop) { nextStop in changeStop(nextStop) }
.onReceive(inspection.notice) { inspection.visit(self, $0) }
.onReceive(timer) { input in now = input }
.onDisappear { leavePredictions() }
}

func changeStop(_ stop: Stop) {
Expand Down
16 changes: 16 additions & 0 deletions iosApp/iosAppTests/InspectionEmissary.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// InspectionEmissary.swift
// iosAppTests
//
// This is used with ViewInspector as recommended in their docs.
// See https://github.com/nalexn/ViewInspector/blob/0.9.11/guide.md#approach-2
//
// Created by Simon, Emma on 4/10/24.
// Copyright © 2024 MBTA. All rights reserved.
//

import Foundation
@testable import iosApp
import ViewInspector

extension Inspection: InspectionEmissary {}
43 changes: 43 additions & 0 deletions iosApp/iosAppTests/Pages/Map/HomeMapViewTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// HomeMapViewTest.swift
// iosAppTests
//
// Created by Simon, Emma on 4/10/24.
// Copyright © 2024 MBTA. All rights reserved.
//

@testable import iosApp
import shared
import SwiftPhoenixClient
import SwiftUI
import ViewInspector
import XCTest
@_spi(Experimental) import MapboxMaps

final class HomeMapViewTest: XCTestCase {
override func setUp() {
executionTimeAllowance = 60
}

func testAppears() throws {
let viewportProvider: ViewportProvider = .init(viewport: .followPuck(zoom: 1))

Check notice on line 23 in iosApp/iosAppTests/Pages/Map/HomeMapViewTest.swift

View check run for this annotation

Xcode Cloud / mbtaapp | PR Branch Workflow | Test - iOS

iosApp/iosAppTests/Pages/Map/HomeMapViewTest.swift#L23

Immutable value 'viewportProvider' was never used; consider replacing with '_' or removing it
let navStack: Binding<[SheetNavigationStackEntry]> = .init(wrappedValue: [])
let sheetHeight: Binding<CGFloat> = .constant(100)

var sut = HomeMapView(

Check notice on line 27 in iosApp/iosAppTests/Pages/Map/HomeMapViewTest.swift

View check run for this annotation

Xcode Cloud / mbtaapp | PR Branch Workflow | Test - iOS

iosApp/iosAppTests/Pages/Map/HomeMapViewTest.swift#L27

Variable 'sut' was never mutated; consider changing to 'let' constant
alertsFetcher: .init(socket: MockSocket()),
globalFetcher: .init(backend: IdleBackend()),
nearbyFetcher: .init(backend: IdleBackend()),
railRouteShapeFetcher: .init(backend: IdleBackend()),
viewportProvider: ViewportProvider(),
navigationStack: navStack,
sheetHeight: sheetHeight
)

let exp = sut.inspection.inspect { view in
XCTAssertNotNil(view)
}
ViewHosting.host(view: sut)
wait(for: [exp], timeout: 2)
}
}
47 changes: 47 additions & 0 deletions iosApp/iosAppTests/Pages/StopDetails/StopDetailsPageTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// StopDetailsPageTests.swift
// iosAppTests
//
// Created by Simon, Emma on 4/5/24.
// Copyright © 2024 MBTA. All rights reserved.
//

@testable import iosApp
import shared
import SwiftPhoenixClient
import SwiftUI
import ViewInspector
import XCTest
@_spi(Experimental) import MapboxMaps

final class StopDetailsPageTests: XCTestCase {
override func setUp() {
executionTimeAllowance = 60
}

func testStopChange() throws {
let objects = ObjectCollectionBuilder()
let route = objects.route()
let stop = objects.stop { _ in }
let nextStop = objects.stop { $0.id = "next" }
let routePattern = objects.routePattern(route: route) { _ in }

let viewportProvider: ViewportProvider = .init(viewport: .followPuck(zoom: 1))

var sut = StopDetailsPage(
backend: IdleBackend(),
socket: MockSocket(),
globalFetcher: .init(backend: IdleBackend()),
viewportProvider: viewportProvider,
stop: stop, filter: .init(routeId: route.id, directionId: routePattern.directionId)
)

let exp = sut.inspection.inspect { _ in
XCTAssertEqual(viewportProvider.viewport.camera?.center, stop.coordinate)
sut.stop = nextStop
XCTAssertEqual(viewportProvider.viewport.camera?.center, nextStop.coordinate)
}
ViewHosting.host(view: sut)
wait(for: [exp], timeout: 2)
}
}
2 changes: 0 additions & 2 deletions iosApp/iosAppTests/Views/NearbyTransitViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import ViewInspector
import XCTest
@_spi(Experimental) import MapboxMaps

extension Inspection: InspectionEmissary {}

final class NearbyTransitViewTests: XCTestCase {
struct NotUnderTestError: Error {}

Expand Down

0 comments on commit 3b507a9

Please sign in to comment.