BarredEwe / Prefire

🔥 A library based on SwiftUI Preview, for easy generation: Playbook view, Snapshot and Accessibility tests
Apache License 2.0
251 stars 16 forks source link

Using file name as a function name when generating tests #63

Closed BarredEwe closed 2 weeks ago

BarredEwe commented 2 weeks ago

Short description 📝

There is an inconvenience, which is described in this issue: https://github.com/BarredEwe/Prefire/issues/49. It consists in the fact that now the first line is taken as the Preview name in the macro. This is a problem if we wrap all views, for example in VStack.

Solution 📦

Using the filename as the main Preview name and adding an index to keep consistency and handle the case where multiple Previews are used in the same file.

Implementation 👩‍💻👨‍💻

The above is implemented plus tests are written.

if let previewBodies = previewBodies(from: content, defaultEnabled: defaultEnabled) {
    let fileName = url.fileName
    previewBodies.enumerated().forEach { index, previewBody in
        previewBodyDictionary["\(fileName)_\(index)"] = previewBody
    }
}