Skip to content

Add failing test cases for onDismiss bugs #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Examples/CaseStudiesTests/Internal/AssertEventually.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import CustomDump
import XCTest

@MainActor
func assertEventually(
_ expression: @autoclosure @escaping @MainActor () -> Bool,
timeout: TimeInterval = 1,
file: StaticString = #file,
line: UInt = #line
) async {
await _assertEventually(
expression(),
condition: { $0 },
assert: XCTAssertTrue,
timeout: timeout,
file: file,
line: line
)
}

@MainActor
func assertEventuallyEqual<T: Equatable>(
_ expression1: @autoclosure @escaping @MainActor () -> T,
Expand Down
67 changes: 66 additions & 1 deletion Examples/CaseStudiesTests/PresentationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,71 @@ final class PresentationTests: XCTestCase {
)
await assertEventuallyNotNil(vc.presentedChild)
}

@MainActor func testOnDismissCalledForInteractiveDismissal() async throws {
let vc = BasicViewController()
try await setUp(controller: vc)

await assertEventuallyNil(vc.presentedViewController)

withUITransaction(\.uiKit.disablesAnimations, true) {
vc.model.isPresented = true
}
await assertEventuallyNotNil(vc.presentedViewController)
await assertEventuallyEqual(vc.isPresenting, true)

vc.presentedViewController!.dismiss(animated: false)
await assertEventuallyNil(vc.presentedViewController)
await assertEventuallyEqual(vc.isPresenting, false)
}

@Observable
fileprivate final class Destinations: Identifiable {
@CasePathable
enum Destination {
case presentedA
case presentedB
}
var destination: Destination?
}
@MainActor func testOnDismissNotCalledForUnrelatedDismissal() async throws {
class A: ViewController {}
class B: ViewController {}
class VC: ViewController {
@UIBindable var model = Destinations()
var onDismissA: (() -> Void)?
var onDismissB: (() -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
present(isPresented: UIBinding($model.destination.presentedA)) { [weak self] in
self?.onDismissA?()
} content: {
A()
}
present(isPresented: UIBinding($model.destination.presentedB)) { [weak self] in
self?.onDismissB?()
} content: {
B()
}
}
}
let vc = VC()
vc.onDismissB = { XCTFail() }
try await setUp(controller: vc)

await assertEventuallyNil(vc.presentedViewController)

withUITransaction(\.uiKit.disablesAnimations, true) {
vc.model.destination = .presentedA
}
await assertEventually(vc.presentedViewController is A)

withUITransaction(\.uiKit.disablesAnimations, true) {
vc.model.destination = .presentedB
}
await assertEventually(vc.presentedViewController is B)
vc.onDismissB = nil
}
}

@Observable
Expand Down Expand Up @@ -492,7 +557,7 @@ private class ViewController: UIViewController {

private class BasicViewController: UIViewController {
@UIBindable var model: Model
var isPresenting = false
private(set) var isPresenting = false
init(model: Model = Model()) {
self.model = model
super.init(nibName: nil, bundle: nil)
Expand Down
Loading