Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jflan-dd committed Nov 21, 2022
0 parents commit 09964ba
Show file tree
Hide file tree
Showing 43 changed files with 1,989 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Package.resolved
Binary file added .readme_assets/add_package.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .readme_assets/xcode_previews.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
556 changes: 556 additions & 0 deletions Examples/PreviewSnapshotsTestApp.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
78 changes: 78 additions & 0 deletions Examples/PreviewSnapshotsTestApp/ObservableObjectView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2022 DoorDash, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

import PreviewSnapshots
import SwiftUI

final class ObservableObjectViewModel: ObservableObject {
@Published var input = ""
@Published var errorMessage: String?

func validate() {
if input.count < 5 {
errorMessage = "Message Too Short"
} else {
errorMessage = nil
}
}
}

struct ObservableObjectView: View {
@ObservedObject var viewModel: ObservableObjectViewModel

var body: some View {
VStack(spacing: 8) {
TextField("Input", text: $viewModel.input)

if let errorMessage = viewModel.errorMessage {
Text(errorMessage)
.font(.caption)
.foregroundColor(.red)
}

Button("Submit") {
viewModel.validate()
}
}
.padding(8)
}
}

struct ObservableObjectView_Previews: PreviewProvider {
static var previews: some View {
snapshots.previews.previewLayout(.sizeThatFits)
}

static var snapshots: PreviewSnapshots<ObservableObjectViewModel> {
let empty = ObservableObjectViewModel()

let tooShort = ObservableObjectViewModel()
tooShort.input = "Hi"
tooShort.validate()

let valid = ObservableObjectViewModel()
valid.input = "Hello World"
valid.validate()

return PreviewSnapshots(
configurations: [
.init(name: "Empty", state: empty),
.init(name: "Too Short", state: tooShort),
.init(name: "Valid", state: valid),
],
configure: {
ObservableObjectView(viewModel: $0)
}
)
}
}
38 changes: 38 additions & 0 deletions Examples/PreviewSnapshotsTestApp/PreviewSnapshotsTestApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2022 DoorDash, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

import SwiftUI

@main
struct PreviewSnapshotsTestApp: App {
var body: some Scene {
WindowGroup {
NavigationView {
List {
NavigationLink("Simple View") {
SimpleView(message: "Hello World!")
}

NavigationLink("PreviewState View") {
PreviewStateView(title: "Hello", subtitle: "Word", message: "Hello World")
}

NavigationLink("ObservableObject View") {
ObservableObjectView(viewModel: .init())
}
}
.navigationTitle("Test Views")
}
}
}
}
68 changes: 68 additions & 0 deletions Examples/PreviewSnapshotsTestApp/PreviewStateView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2022 DoorDash, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

import PreviewSnapshots
import SwiftUI

struct PreviewStateView: View {
let title: String
let subtitle: String
let message: String

var body: some View {
VStack(spacing: 24) {
VStack(spacing: 8) {
Text(title)
.font(.title)

Text(subtitle)
.font(.subheadline)
}

Text(message)
.font(.body)
}
.multilineTextAlignment(.center)
.padding(8)
}
}

struct PreviewStateView_Previews: PreviewProvider {
static var previews: some View {
snapshots.previews.previewLayout(.sizeThatFits)
}

static var snapshots: PreviewSnapshots<PreviewState> {
PreviewSnapshots(
states: [
.init(name: "Short", title: "Hello", subtitle: "PreviewSnapshots", message: "This is a test"),
.init(
name: "Long",
title: "Hello PreviewSnapshots",
subtitle: "Welcome to PreviewSnapshots",
message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
)
],
configure: {
PreviewStateView(title: $0.title, subtitle: $0.subtitle, message: $0.message)
}
)
}

struct PreviewState: NamedPreviewState {
let name: String
let title: String
let subtitle: String
let message: String
}
}
48 changes: 48 additions & 0 deletions Examples/PreviewSnapshotsTestApp/SimpleView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2022 DoorDash, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

import PreviewSnapshots
import SwiftUI

struct SimpleView: View {
let message: String

var body: some View {
VStack(spacing: 8) {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)

Text(message)
}
.padding(8)
}
}

struct SimpleView_Previews: PreviewProvider {
static var previews: some View {
snapshots.previews.previewLayout(.sizeThatFits)
}

static var snapshots: PreviewSnapshots<String> {
PreviewSnapshots(
configurations: [
.init(name: "Short Message", state: "Hello World"),
.init(name: "Long Message", state: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."),
],
configure: {
SimpleView(message: $0)
}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2022 DoorDash, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

import PreviewSnapshotsTesting
import SnapshotTesting
import SwiftUI
import XCTest

@testable import PreviewSnapshotsTestApp

final class PreviewSnapshotsTestAppTests: XCTestCase {
func test_simpleViewSnapshots() {
SimpleView_Previews.snapshots.assertSnapshots(as: .testStrategy())
}

func test_previewStateViewSnapshots() {
PreviewStateView_Previews.snapshots.assertSnapshots(as: .testStrategy())
}

func test_observableObjectSnapshots() {
ObservableObjectView_Previews.snapshots.assertSnapshots(as: .testStrategy())
}

func test_previewStateLightAndDark() {
PreviewStateView_Previews.snapshots
.assertSnapshots(as: [
"Light": .testStrategy(userInterfaceStyle: .light),
"Dark": .testStrategy(userInterfaceStyle: .dark),
])
}
}

extension Snapshotting where Value: SwiftUI.View, Format == UIImage {
/// Shared image test strategy
static func testStrategy(userInterfaceStyle: UIUserInterfaceStyle = .light) -> Self {
let traits = UITraitCollection(traitsFrom: [
UITraitCollection(displayScale: 1),
UITraitCollection(userInterfaceStyle: userInterfaceStyle),
])

return .image(
layout: .fixed(width: 400, height: 400),
traits: traits
)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 09964ba

Please sign in to comment.