Skip to content
/ Prefire Public
forked from BarredEwe/Prefire

πŸ”₯ A library based on SwiftUI Preview, for easy generation: Playbook view, Snapshot and Accessibility tests

License

Notifications You must be signed in to change notification settings

markst/Prefire

Β 
Β 

Repository files navigation

Prefire

A library for easily generating automatic Playbook (Demo) view and Tests using SwiftUI Preview

Works with: UI-components, screens and flows

Release Platform Swift5 Swift Package Manager Swift Package Manager

Prefire

Playbook

Do you like SwiftUI Preview and use it? Then you must try πŸ”₯Prefire!

You can try πŸ”₯Prefire starting from example project.

  • βœ… Easy to use
  • βœ… Fully automatic generation based on Sourcery
  • βœ… Generation Playbook (Demo) view
  • βœ… Generation Snapshot tests based on swift-snapshot-testing
  • βœ… Generation Accesability Snapshot tests
  • βœ… Xcode Plugin supported


Installation

Prefire can be install for an Xcode Project or only for one Package.

Xcode Project Plugin

You can integrate Prefire as an Xcode Build Tool Plug-in if you're working on a project in Xcode.

  1. Add Prefire as a package dependency to your project without linking any of the products.

  1. Select the target to which you want to add linting and open the Build Phases inspector. Open Run Build Tool Plug-ins and select the button. From the list, select PrefirePlaybookPlugin or PrefireTestsPlugin, and add it to the project.

Swift Package Plugin

You can integrate Prefire as a Swift Package Manager Plug-in if you're working with a Swift Package with a Package.swift manifest.

  1. Add Prefire as a package dependency to your Package.swift file.
dependencies: [
    .package(url: "https://github.com/BarredEwe/Prefire", from: "1.0.0")
]
  1. Add Prefire to a target using the plugins parameter.
.target(
    plugins: [
        // For Playbook (Demo) view
        .plugin(name: "PrefirePlaybookPlugin", package: "Prefire")
        // For Snapshot Tests
        .plugin(name: "PrefireTestsPlugin", package: "Prefire")
    ]
),

Usage

For generate tests and playbook, simply mark your preview using protocol - PrefireProvider:

struct Text_Previews: PreviewProvider, PrefireProvider {
    static var previews: some View { ... }
}

Playbook (Demo) View

To use Playbook, simply use PlaybookView

  • If you want to see a list of all the Views, use isComponent: true
  • If you want to sort by UserStory, use isComponent: false
import Prefire 

struct ContentView: View {
    var body: some View {
        PlaybookView(isComponent: true, previewModels: PreviewModels.models)
    }
}

Snapshot tests

Just run generated tests πŸš€

For detailed instruction you can see swift-snapshot-testing


API

New commands for previews:

  • Function for connecting preview together in one Flow:

    .previewUserStory(.auth)
    static var previews: some View {
        PrefireView()
            .previewUserStory(.auth)
    }
    
    static var previews: some View {
        AuthView()
            .previewUserStory(.auth)
    }

    For example Authorization flow: LoginView, OTPView and PincodeView


  • If a preview contains more than one View, you can mark State for these views.

    .previewState(.loading)
    static var previews: some View {
        TestView("Default")
    
        TestView("Loading")
            .previewState(.loading)
    }


Config

You can additionaly configure Prefire by adding a .prefire.yml file to root folder. For example:

test_configuration:
  - target: PrefireExample 
  - test_file_path: PrefireExampleTests/PreviewTests.generated.swift
  - simulator_device: "iPhone15,2"
  - required_os: 16
  • target - Your project Target for Snapshot tests. Default: FirstTarget
  • test_file_path - Filepath to generated file. Default: DeriveData
  • simulator_device - Device for Snapshot testing. Default: iPhone 14 Pro
  • required_os - iOS version for Snapshot testing. Default: iOS 16

Requirements

  • Swift 5.6
  • Xcode 14.0
  • iOS 14

Previews Troubleshooting

  • Don't forget remove #IF DEBUG for yours SwiftUI Previews. Xcode automatically removed Preview code, when you build release version.
  • NavigationView in Preview is not supported

About

πŸ”₯ A library based on SwiftUI Preview, for easy generation: Playbook view, Snapshot and Accessibility tests

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 100.0%