Skip to content

Commit

Permalink
Add migration example
Browse files Browse the repository at this point in the history
  • Loading branch information
noahsmartin committed Sep 19, 2024
1 parent 0b0438a commit 6eff290
Show file tree
Hide file tree
Showing 82 changed files with 828 additions and 145 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Xcode select
run: sudo xcode-select -s '/Applications/Xcode_15.4.app/Contents/Developer'
- name: Build TestApp
run: xcodebuild build -scheme DemoApp -sdk iphonesimulator -destination 'generic/platform=iOS Simulator' -project DemoApp/DemoApp.xcodeproj
run: cd Examples && xcodebuild build -scheme DemoApp -sdk iphonesimulator -destination 'generic/platform=iOS Simulator' -project DemoApp/DemoApp.xcodeproj
- name: Build Snapshotting
run: xcodebuild build -scheme Snapshotting -sdk iphonesimulator -destination 'generic/platform=iOS Simulator'
- name: Build SnapshottingTests
Expand Down Expand Up @@ -43,7 +43,7 @@ jobs:
- name: Xcode select
run: sudo xcode-select -s '/Applications/Xcode_15.4.app/Contents/Developer'
- name: Build TestApp
run: xcodebuild build -scheme DemoApp -sdk xrsimulator -destination 'generic/platform=visionOS Simulator' -project DemoApp/DemoApp.xcodeproj
run: cd Examples && xcodebuild build -scheme DemoApp -sdk xrsimulator -destination 'generic/platform=visionOS Simulator' -project DemoApp/DemoApp.xcodeproj
- name: Build Snapshotting
run: xcodebuild build -scheme Snapshotting -sdk xrsimulator -destination 'generic/platform=visionOS Simulator'
- name: Build SnapshottingTests
Expand All @@ -57,7 +57,7 @@ jobs:
- name: Xcode select
run: sudo xcode-select -s '/Applications/Xcode_15.4.app/Contents/Developer'
- name: Build Test Watch App
run: xcodebuild build -scheme 'Demo Watch App' -sdk watchsimulator -destination 'platform=watchOS Simulator,name=Apple Watch Series 9 (41mm)' -project DemoApp/DemoApp.xcodeproj CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO
run: cd Examples && xcodebuild build -scheme 'Demo Watch App' -sdk watchsimulator -destination 'platform=watchOS Simulator,name=Apple Watch Series 9 (41mm)' -project DemoApp/DemoApp.xcodeproj CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO
- name: Build Snapshotting
run: xcodebuild build -scheme Snapshotting -sdk watchsimulator -destination 'generic/platform=watchOS Simulator'
- name: Build SnapshottingTests
Expand Down
82 changes: 0 additions & 82 deletions DemoApp/DemoApp.xcodeproj/xcshareddata/xcschemes/DemoApp.xcscheme

This file was deleted.

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
FAD52BF42A7B5EEA001F1832 /* RideShareButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RideShareButton.swift; sourceTree = "<group>"; };
FAD52BF52A7B5EEA001F1832 /* RideOptionsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RideOptionsView.swift; sourceTree = "<group>"; };
FADB112B2C67A17500985C48 /* DemoAppSnapshotTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoAppSnapshotTest.swift; sourceTree = "<group>"; };
FAF1BA622A54934200AEBE1B /* .. */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = ..; path = ../..; sourceTree = "<group>"; };
FAF1BA622A54934200AEBE1B /* .. */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = ..; path = ../../..; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions Examples/DemoApp/DemoApp/TestViews/ScrollingTextView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ScrollingTextView.swift
// DemoApp
//
// Created by Noah Martin on 8/24/24.
//

import Foundation
import SwiftUI

struct ScrollingTextView: NSViewRepresentable {
func makeNSView(context: Context) -> some NSView {
let textView = NSTextView()
textView.string = "test"
let scrollView = NSScrollView()
scrollView.documentView = textView
return scrollView
}

func updateNSView(_ nsView: NSViewType, context: Context) { }
}

struct ScrollingTextView_Previews: PreviewProvider {
static var previews: some View {
ScrollingTextView()
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions Examples/UnitTestMigration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Unit Test Migration

This example shows how to migrate from swift-snapshot-testing to preview providers. The example app contains a unit test target `UnitTestMigrationTests` with one [snapshot test](https://github.com/EmergeTools/SnapshotPreviews-iOS/blob/main/Examples/UnitTestMigration/UnitTestMigrationTests/ExampleSnapshotTest.swift). It has been migrated by copying/pasting the file into the main app target as [ExampleSnapshotTest_Migrated.swift](https://github.com/EmergeTools/SnapshotPreviews-iOS/blob/main/Examples/UnitTestMigration/UnitTestMigration/ExampleSnapshotTest_Migrated.swift). The test is modified by removing the XCTest/SnapshotTesting importants, changing the base class to `SnapshotTest` and adding the conformance to `PreviewProvider`.

## Migrating your own tests

1. Add the file [SnapshotTest.swift](https://github.com/EmergeTools/SnapshotPreviews-iOS/blob/main/Examples/UnitTestMigration/UnitTestMigration/SnapshotTest.swift) to your app, this does the heavy lifting of converting snapshot test function calls into previews.
2. Copy your test file from the unit test target to your application.
3. Remove imports of `XCTest` and `SnapshotTesting`.
4. Change the base class from `XCTestCase` to `SnapshotTest` and add a conformce to `PreviewProvider`.

Here’s a complete example of a migrated test:

```swift
import SwiftUI

// Add the conformance to `PreviewProvider` when extending `SnapshotTest`
// The `SnapshotTest` base class automatically handles turning calls to
// assertSnapshot into previews.
class ExampleSnapshotTest: SnapshotTest, PreviewProvider {

func testContentViewSnapshot() {
assertSnapshot(of: ContentView(), as: .image)
}
}
```
Loading

0 comments on commit 6eff290

Please sign in to comment.