Skip to content

Commit

Permalink
Add SwiftUI snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Aug 11, 2020
1 parent 6f6db9d commit cc7007f
Show file tree
Hide file tree
Showing 19 changed files with 1,126 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Snapshots/SwiftUI/.swiftformat
@@ -0,0 +1,2 @@
--swiftversion 5.2
--indent 2
66 changes: 66 additions & 0 deletions Snapshots/SwiftUI/Bookworm/AddBookView.swift
@@ -0,0 +1,66 @@
//
// AddBookView.swift
// Bookworm
//
// Created by Nick Lockwood on 29/06/2020.
// Copyright © 2020 Nick Lockwood. All rights reserved.
//

import SwiftUI

struct AddBookView: View {
@Environment(\.managedObjectContext) var moc
@Environment(\.presentationMode) var presentationMode

@State private var title = ""
@State private var author = ""
@State private var rating = 3
@State private var genre = "Fantasy"
@State private var review = ""

let genres = ["Fantasy", "Horror", "Kids", "Mystery", "Poetry", "Romance", "Thriller"]

var body: some View {
NavigationView {
Form {
Section {
TextField("Name of book", text: $title)
TextField("Author's name", text: $author)

Picker("Genre", selection: $genre) {
ForEach(genres, id: \.self) {
Text($0)
}
}
}

Section {
RatingView(rating: $rating)
TextField("Write a review", text: $review)
}

Section {
Button("Save") {
let newBook = Book(context: self.moc)
newBook.title = self.title
newBook.author = self.author
newBook.rating = Int16(self.rating)
newBook.genre = self.genre
newBook.review = self.review
newBook.date = Date()

try? self.moc.save()
self.presentationMode.wrappedValue.dismiss()
}
}
}
.navigationBarTitle("Add book")
}
}
}

struct AddBookView_Previews: PreviewProvider {
static var previews: some View {
AddBookView()
}
}
66 changes: 66 additions & 0 deletions Snapshots/SwiftUI/Bookworm/ContentView.swift
@@ -0,0 +1,66 @@
//
// ContentView.swift
// Bookworm
//
// Created by Nick Lockwood on 29/06/2020.
// Copyright © 2020 Nick Lockwood. All rights reserved.
//

import SwiftUI

struct ContentView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: Book.entity(), sortDescriptors: [
NSSortDescriptor(keyPath: \Book.title, ascending: true),
NSSortDescriptor(keyPath: \Book.author, ascending: true),
]) var books: FetchedResults<Book>

@State private var showingAddScreen = false

var body: some View {
NavigationView {
List {
ForEach(books, id: \.self) { book in
NavigationLink(destination:
DetailView(book: book).environment(\.managedObjectContext, self.moc)
) {
EmojiRatingView(rating: book.rating)
.font(.largeTitle)

VStack(alignment: .leading) {
Text(book.title ?? "Unknown title")
.font(.headline)
.foregroundColor(book.rating < 2 ? .red : .primary)
Text(book.author ?? "Unknown author")
.foregroundColor(.secondary)
}
}
}
.onDelete(perform: deleteBooks(at:))
}
.navigationBarTitle("Bookworm")
.navigationBarItems(leading: EditButton(), trailing: Button(action: {
self.showingAddScreen.toggle()
}) {
Image(systemName: "plus")
})
.sheet(isPresented: $showingAddScreen) {
AddBookView().environment(\.managedObjectContext, self.moc)
}
}
}

func deleteBooks(at offsets: IndexSet) {
for offset in offsets {
let book = books[offset]
moc.delete(book)
try? moc.save()
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
94 changes: 94 additions & 0 deletions Snapshots/SwiftUI/Bookworm/DetailView.swift
@@ -0,0 +1,94 @@
//
// DetailView.swift
// Bookworm
//
// Created by Nick Lockwood on 30/06/2020.
// Copyright © 2020 Nick Lockwood. All rights reserved.
//

import CoreData
import SwiftUI

struct DetailView: View {
let book: Book

@Environment(\.managedObjectContext) var moc
@Environment(\.presentationMode) var presentationMode
@State private var showingDeleteAlert = false

var body: some View {
GeometryReader { geo in
VStack {
ZStack(alignment: .bottomTrailing) {
Image(self.book.genre ?? "Fantasy")
.frame(maxWidth: geo.size.width)

Text(self.book.genre?.uppercased() ?? "FANTASY")
.font(.caption)
.fontWeight(.black)
.padding(8)
.foregroundColor(.white)
.background(Color.black.opacity(0.75))
.clipShape(Capsule())
.offset(x: -5, y: -5)
}

Text(self.book.author ?? "Unknown author")
.font(.title)
.foregroundColor(.secondary)

Text(self.book.review ?? "No review")
.padding()

Text(self.formattedDate(for: self.book))
.padding()

RatingView(rating: .constant(Int(self.book.rating)))
.font(.largeTitle)

Spacer()
}
}
.navigationBarTitle(Text(book.title ?? "Unknown book"), displayMode: .inline)
.alert(isPresented: $showingDeleteAlert) {
Alert(title: Text("Delete book"), message: Text("Are you sure?"), primaryButton: .destructive(Text("Delete")) {
self.deleteBook()
}, secondaryButton: .cancel())
}
.navigationBarItems(trailing: Button(action: {
self.showingDeleteAlert = true
}) {
Image(systemName: "trash")
})
}

func formattedDate(for book: Book) -> String {
let formatter = DateFormatter()
formatter.dateStyle = .short
return "Date added: \(formatter.string(from: book.date ?? Date()))"
}

func deleteBook() {
moc.delete(book)

try? moc.save()
presentationMode.wrappedValue.dismiss()
}
}

struct DetailView_Previews: PreviewProvider {
static let moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)

static var previews: some View {
let book = Book(context: moc)
book.title = "Test book"
book.author = "Test author"
book.genre = "Fantasy"
book.rating = 4
book.review = "This was a great book; I really enjoyed it."

return NavigationView {
DetailView(book: book)
}
}
}
34 changes: 34 additions & 0 deletions Snapshots/SwiftUI/Bookworm/EmojiRatingView.swift
@@ -0,0 +1,34 @@
//
// EmojiRatingView.swift
// Bookworm
//
// Created by Nick Lockwood on 30/06/2020.
// Copyright © 2020 Nick Lockwood. All rights reserved.
//

import SwiftUI

struct EmojiRatingView: View {
let rating: Int16

var body: some View {
switch rating {
case 1:
return Text("😴")
case 2:
return Text("☹️")
case 3:
return Text("😐")
case 4:
return Text("😄")
default:
return Text("🤩")
}
}
}

struct EmojiRatingView_Previews: PreviewProvider {
static var previews: some View {
EmojiRatingView(rating: 3)
}
}
49 changes: 49 additions & 0 deletions Snapshots/SwiftUI/Bookworm/RatingView.swift
@@ -0,0 +1,49 @@
//
// RatingView.swift
// Bookworm
//
// Created by Nick Lockwood on 30/06/2020.
// Copyright © 2020 Nick Lockwood. All rights reserved.
//

import SwiftUI

struct RatingView: View {
@Binding var rating: Int

var label = ""

var maximumRating = 5

var offImage: Image?
var onImage = Image(systemName: "star.fill")

var offColor = Color.gray
var onColor = Color.yellow

var body: some View {
HStack {
if label.isEmpty == false {
Text(label)
}

ForEach(1 ..< maximumRating + 1) { number in
self.image(for: number)
.foregroundColor(number > self.rating ? self.offColor : self.onColor)
.onTapGesture {
self.rating = number
}
}
}
}

func image(for number: Int) -> Image {
number > rating ? offImage ?? onImage : onImage
}
}

struct RatingView_Previews: PreviewProvider {
static var previews: some View {
RatingView(rating: .constant(4))
}
}
37 changes: 37 additions & 0 deletions Snapshots/SwiftUI/CupcakeCorner/AddressView.swift
@@ -0,0 +1,37 @@
//
// AddressView.swift
// CupcakeCorner
//
// Created by Nick Lockwood on 28/06/2020.
// Copyright © 2020 Nick Lockwood. All rights reserved.
//

import SwiftUI

struct AddressView: View {
@ObservedObject var order = Order()

var body: some View {
Form {
Section {
TextField("Name", text: $order.data.name)
TextField("Street Address", text: $order.data.streetAddress)
TextField("City", text: $order.data.city)
TextField("Zip", text: $order.data.zip)
}

Section {
NavigationLink(destination: CheckoutView(order: order)) {
Text("Check out")
}
}.disabled(!order.data.hasValidAddress)
}
.navigationBarTitle("Delivery details", displayMode: .inline)
}
}

struct AddressView_Previews: PreviewProvider {
static var previews: some View {
AddressView(order: Order())
}
}

0 comments on commit cc7007f

Please sign in to comment.