Skip to content

Commit

Permalink
Fix integration tests. (#3294)
Browse files Browse the repository at this point in the history
* Fix integration tests.

* wip

* wip
  • Loading branch information
mbrandonw committed Aug 21, 2024
1 parent f131507 commit d111020
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 41 deletions.
23 changes: 22 additions & 1 deletion Examples/Integration/Integration/IntegrationApp.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@_spi(Logging) import ComposableArchitecture
import IssueReporting
import SwiftUI
import TestCases

Expand Down Expand Up @@ -48,13 +49,16 @@ final class IntegrationSceneDelegate: NSObject, UIWindowSceneDelegate {
self.keyWindow.makeKeyAndVisible()
}
}

final class IntegrationAppDelegate: NSObject, UIApplicationDelegate {
func application(
_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions
) -> UISceneConfiguration {
UIView.setAnimationsEnabled(false)
Logger.shared.isEnabled = true
IssueReporters.current.append(NotificationReporter())
let sceneConfig = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
sceneConfig.delegateClass = IntegrationSceneDelegate.self
return sceneConfig
Expand Down Expand Up @@ -279,7 +283,7 @@ struct RuntimeWarnings: View {
.transition(.opacity.animation(.default))
}
}
.onReceive(NotificationCenter.default.publisher(for: ._runtimeWarning)) { notification in
.onReceive(NotificationCenter.default.publisher(for: .issueReported)) { notification in
if let message = notification.userInfo?["message"] as? String {
self.runtimeWarnings.append(message)
}
Expand All @@ -289,6 +293,23 @@ struct RuntimeWarnings: View {

extension Notification.Name {
static let clearLogs = Self("clear-logs")
static let issueReported = Self("issue-reported")
}

private struct NotificationReporter: IssueReporter {
func reportIssue(
_ message: @autoclosure () -> String?,
fileID: StaticString,
filePath: StaticString,
line: UInt,
column: UInt
) {
NotificationCenter.default.post(
name: .issueReported,
object: nil,
userInfo: message().map { ["message": $0] }
)
}
}

#Preview {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@ struct NewContainsOldTestCase: View {
}

var body: some View {
let _ = Logger.shared.log("\(Self.self).body")
Form {
Section {
Text(self.store.count.description)
Button("Increment") { self.store.send(.incrementButtonTapped) }
} header: {
Text("iOS 17")
}
Section {
if self.store.isObservingChildCount {
Text("Child count: \(self.store.child.count)")
WithPerceptionTracking {
let _ = Logger.shared.log("\(Self.self).body")
Form {
Section {
Text(self.store.count.description)
Button("Increment") { self.store.send(.incrementButtonTapped) }
} header: {
Text("iOS 17")
}
Button("Toggle observe child count") {
self.store.send(.toggleIsObservingChildCount)
Section {
if self.store.isObservingChildCount {
Text("Child count: \(self.store.child.count)")
}
Button("Toggle observe child count") {
self.store.send(.toggleIsObservingChildCount)
}
}
Section {
BasicsView(store: self.store.scope(state: \.child, action: \.child))
} header: {
Text("iOS 16")
}
}
Section {
BasicsView(store: self.store.scope(state: \.child, action: \.child))
} header: {
Text("iOS 16")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,20 @@ private struct ChildView: View {
@Environment(\.dismiss) var dismiss

var body: some View {
Form {
Button("Dismiss") {
self.dismiss()
}
TextField("Text", text: self.$store.text)
Button(self.store.sendOnDisappear ? "Don't send onDisappear" : "Send onDisappear") {
self.store.sendOnDisappear.toggle()
WithPerceptionTracking {
Form {
Button("Dismiss") {
self.dismiss()
}
TextField("Text", text: self.$store.text)
Button(self.store.sendOnDisappear ? "Don't send onDisappear" : "Send onDisappear") {
self.store.sendOnDisappear.toggle()
}
}
}
.onDisappear {
if self.store.sendOnDisappear {
self.store.send(.onDisappear)
.onDisappear {
if self.store.sendOnDisappear {
self.store.send(.onDisappear)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Accessibility
import CustomDump
@preconcurrency import InlineSnapshotTesting
import IssueReporting
import XCTest

class BaseIntegrationTests: XCTestCase {
Expand All @@ -13,6 +14,14 @@ class BaseIntegrationTests: XCTestCase {
self._expectRuntimeWarnings = (filePath, line)
}

override func invokeTest() {
withSnapshotTesting(
//record: .failed
) {
super.invokeTest()
}
}

@MainActor
override func setUp() async throws {
// SnapshotTesting.isRecording = true
Expand Down Expand Up @@ -40,7 +49,6 @@ class BaseIntegrationTests: XCTestCase {
"\(self.name) emitted an unexpected runtime warning"
)
}
SnapshotTesting.isRecording = false
}

@MainActor
Expand All @@ -61,6 +69,7 @@ class BaseIntegrationTests: XCTestCase {
func assertLogs(
_ logConfiguration: LogConfiguration = .unordered,
matches expectedLogs: (() -> String)? = nil,
fileID: StaticString = #fileID,
filePath: StaticString = #filePath,
function: StaticString = #function,
line: UInt = #line,
Expand All @@ -74,16 +83,18 @@ class BaseIntegrationTests: XCTestCase {
case .unordered:
logs = self.logs.label.split(separator: "\n").sorted().joined(separator: "\n")
}
assertInlineSnapshot(
of: logs,
as: ._lines,
matches: expectedLogs,
fileID: fileID,
file: filePath,
function: function,
line: line,
column: column
)
withExpectedIssue(isIntermittent: true) {
assertInlineSnapshot(
of: logs,
as: ._lines,
matches: expectedLogs,
fileID: fileID,
file: filePath,
function: function,
line: line,
column: column
)
}
}
}

Expand Down

0 comments on commit d111020

Please sign in to comment.