Add ViewBuilder capacity to preview variables in the test template#148
Conversation
|
Hi! Thanks for the fix. Regarding tests, I think what we have in PrefireExample is enough here. |
I see. So I can add a new Preview inside PrefireExample that contains multiple views. |
d5b3e1e to
7c14fde
Compare
|
I added the Preview macro with multiple views to the Example project, but then I realized that my initial implementation was too naive. It broke support for The current solution still isn’t perfect, because it’s technically possible to build a Preview with a single @available(iOS 17.0, macOS 14.0, tvOS 17.0, *)
@freestanding(declaration)
public macro Preview(
_ name: String? = nil,
traits: PreviewTrait<Preview.ViewTraits>...,
@PreviewMacroBodyBuilder<UIViewController> body: @escaping @MainActor () -> UIViewController
) = #externalMacro(module: "PreviewsMacros", type: "KitViewMacro")Initially, I tried to create my own result builder that supports both So the current implementation works correctly for SwiftUI views using |
|
A working solution, but I think we can make it a bit simpler using generic functions. public init(@ViewBuilder _ view: @escaping @MainActor () -> Content, name: String, isScreen: Bool, device: DeviceConfig, traits: UITraitCollection = .init()) {I quickly sketched out an implementation here - take a look. If you like it, we can merge it. |
7c14fde to
2a7e62b
Compare
|
@BarredEwe : Your solution is much nicer, directly modifying the I managed to reproduce the error with this #Preview example: #Preview(traits: .sizeThatFitsLayout) {
let userStory: PreviewModel.UserStory = .testStory
VStack
{
TestView(isLoading: true)
.previewUserStory(userStory)
.snapshot(delay: 0.1, precision: 0.9)
.previewUserStory(.auth)
}.padding()
}I’ve updated the merge request. |
|
Great! I’ve checked it, it works well. Thanks for contributing! |
Short description 📝
The generated tests does not support
#Previewwith multiple views. Fix issue #146^ This preview generate a test that doesn't build.
Solution 📦
Because
#Previewis of type@ViewBuilder, the preview variables, used in the generated tests, should also be@ViewBuilder.Test 🧪
I did not find any existing test for the
PreviewTestsTemplate, but it would be nice to add some. @BarredEwe : Do you have some suggestions of tests I could add ?