Skip to content

Instantly share code, notes, and snippets.

@dheerajn
Created September 19, 2021 18:51
Show Gist options
  • Save dheerajn/551e4183cd55495f6bb42b0e752afb8f to your computer and use it in GitHub Desktop.
Save dheerajn/551e4183cd55495f6bb42b0e752afb8f to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// SwiftUI-UITesting-Medium
//
// Created by Dheeru Neelam on 9/16/21.
//
import SwiftUI
struct ContentView: View {
@State private var navigateToView1 = false
@State private var navigateToView2 = false
var body: some View {
NavigationView {
ZStack {
Color.gray.opacity(0.5)
VStack {
getTitleText()
getButton()
setupNavigationLinks()
}
}
.navigationBarTitle("UI Testing")
}
}
// MARK: - Views
private func getTitleText() -> some View {
Text(getTitleText())
.padding()
}
private func getButton() -> some View {
Button {
handleButtonAction()
} label: {
Text("Navigate to New View")
}
.accessibilityIdentifier("customButton")
}
private func setupNavigationLinks() -> some View {
VStack {
NavigationLink("", isActive: $navigateToView1) {
View1()
}
NavigationLink("", isActive: $navigateToView2) {
View2()
}
}
}
// MARK: - Helper methods
private func getTitleText() -> String {
if TestDetector.isRunningUITests(), TestDetector.shouldOverrideTitleText() {
return "Text Overridden for UI Tests"
}
return "Normal Text"
}
private func handleButtonAction() {
if TestDetector.isRunningUITests(), TestDetector.shouldShowView2() {
navigateToView2 = true
return
}
if didMeetCondition() {
navigateToView1 = true
} else {
navigateToView2 = true
}
}
private func didMeetCondition() -> Bool {
return true
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment