Tests #3
Replies: 2 comments 9 replies
-
Thanks for the kind words, I'm really glad you like it 😃 It's on my todo list but to be entirely honest, I need to think about how to actually do it. Adding unit tests to the If you have ideas, suggestions or want to help, that would be greatly appreciated 👍 😄 |
Beta Was this translation helpful? Give feedback.
-
Hey @andybezaire, it's been quite a while. I'm really sorry about that. My other project, a new SwiftUI app architecture has pretty much consumed all of my free time for the past few months. But now I have finally had the time to work on the problem again and I think I have come up with something that might work and I'm curious what you think about it :) The key to making unit tests work (and a lot more, as it turns out!) was, unfortunately, introducing a breaking change (meaning this will require a 2.0.0 release). I pulled out the inner guts of the In practice, this changes the following: Previously, you would use struct MyView: View {
@Queryable<Void, Bool> var deletionConfirmation
var body: some View { /* ... */ }
} Now, there's no custom property wrapper anymore, just a plain old struct MyView: View {
@StateObject var deletionConfirmation = Queryable<String, Bool>()
var body: some View { /* ... */ }
} (With iOS 17 and the All the What's new is that you can now define that // A view model or other observable object:
class SomeObservableObject: ObservableObject {
let var deletionConfirmation = Queryable<String, Bool>()
}
// A globally shared object, reachable from anywhere in the app:
class SomeSingleton {
static let shared: SomeSingleton = .init()
let var deletionConfirmation = Queryable<String, Bool>()
} As long as you have the reference to it, you can pass it to one of the Additionally, this allowed me to actually add support for unit testing, which was the whole point of this. You can find a first draft of these here: QueryableTests.swift I got this working by building a small query observer helper class gives you control over the asynchronous queries: QueryableObserver.swift For example: func testBasic() async throws {
let queryable = Queryable<Void, Bool>()
_ = QueryableObserver(queryable: queryable) { queryId, resolver in
if queryId == "0" {
resolver.answer(with: true)
} else if queryId == "1" {
resolver.answer(with: false)
}
}
do {
let trueResult = try await queryable.query(id: "0")
XCTAssertTrue(trueResult)
let falseResult = try await queryable.query(id: "1")
XCTAssertFalse(falseResult)
} catch {
XCTFail()
}
} It's still rough around the edges, but it's a start. What do you think? |
Beta Was this translation helpful? Give feedback.
-
Have you considered adding tests?
Great helper. This should be in SwiftUI IMO.
It works great and also preserves the testability of your system.
Let me know if you would be interested to add tests and I'd love to help.
Beta Was this translation helpful? Give feedback.
All reactions