StanfordSpezi / Spezi

Open-source framework for rapid development of modern, interoperable digital health applications.
https://swiftpackageindex.com/StanfordSpezi/Spezi/documentation
MIT License
130 stars 10 forks source link

Improve support for Previews and Unit Testing #95

Closed Supereg closed 6 months ago

Supereg commented 6 months ago

Improve support for Previews and Unit Testing

:recycle: Current situation & Problem

As outlined in #63, there is a need to more easily configure the Spezi framework within SwiftUI Previews. This currently is not easily possible, as we require an SpeziAppDelegate to be configured to support infrastructure like the LifecycleHandler protocol. This is not available for Previews.

This PR adds two new shorthand previewWith(simulateLifecycle:_:) and previewWith(standard:simulateLifecycle:_:) modifiers that are targeted for preview usage only. As documented, LifecycleHandler are not fully supported with this configuration due to the missing App delegate. However, there is an option to partially simulate its behavior.

Further, #64 illustrates difficulties to unit test Spezi Modules due to missing abilities to resolve Module dependencies without initializing a SwiftUI App. This PR adds first-hand support for this scenario using the methods withDependencyResolution(simulateLifecycle:_:) and withDependencyResolution(standard:simulateLifecycle:_:) in the XCTSpezi package.

:gear: Release Notes

:books: Documentation

Documentation was added to illustrate that these modifiers should only be used within Previews. Further, they are placed in a dedicated Previews section in the DocC landing page.

New DocC bundle was added for the XCTSpezi target.

:white_check_mark: Testing

Several test cases were added to support this new functionality.

:pencil: Code of Conduct & Contributing Guidelines

By submitting creating this pull request, you agree to follow our Code of Conduct and Contributing Guidelines:

codecov[bot] commented 6 months ago

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (d8b6645) 91.53% compared to head (d8e8643) 91.20%.

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/StanfordSpezi/Spezi/pull/95/graphs/tree.svg?width=650&height=150&src=pr&token=KHU2K1HTAM&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi)](https://app.codecov.io/gh/StanfordSpezi/Spezi/pull/95?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi) ```diff @@ Coverage Diff @@ ## main #95 +/- ## ========================================== - Coverage 91.53% 91.20% -0.33% ========================================== Files 31 34 +3 Lines 661 693 +32 ========================================== + Hits 605 632 +27 - Misses 56 61 +5 ``` | [Files](https://app.codecov.io/gh/StanfordSpezi/Spezi/pull/95?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi) | Coverage Δ | | |---|---|---| | [...pabilities/Lifecycle/Spezi+LifecycleHandlers.swift](https://app.codecov.io/gh/StanfordSpezi/Spezi/pull/95?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi#diff-U291cmNlcy9TcGV6aS9DYXBhYmlsaXRpZXMvTGlmZWN5Y2xlL1NwZXppK0xpZmVjeWNsZUhhbmRsZXJzLnN3aWZ0) | `100.00% <100.00%> (ø)` | | | [Sources/Spezi/Configuration/Configuration.swift](https://app.codecov.io/gh/StanfordSpezi/Spezi/pull/95?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi#diff-U291cmNlcy9TcGV6aS9Db25maWd1cmF0aW9uL0NvbmZpZ3VyYXRpb24uc3dpZnQ=) | `100.00% <ø> (ø)` | | | [Sources/Spezi/Spezi/View+Spezi.swift](https://app.codecov.io/gh/StanfordSpezi/Spezi/pull/95?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi#diff-U291cmNlcy9TcGV6aS9TcGV6aS9WaWV3K1NwZXppLnN3aWZ0) | `100.00% <ø> (ø)` | | | [...Spezi/Utilities/ProcessInfo+PreviewSimulator.swift](https://app.codecov.io/gh/StanfordSpezi/Spezi/pull/95?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi#diff-U291cmNlcy9TcGV6aS9VdGlsaXRpZXMvUHJvY2Vzc0luZm8rUHJldmlld1NpbXVsYXRvci5zd2lmdA==) | `100.00% <100.00%> (ø)` | | | [Sources/XCTSpezi/DependencyResolution.swift](https://app.codecov.io/gh/StanfordSpezi/Spezi/pull/95?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi#diff-U291cmNlcy9YQ1RTcGV6aS9EZXBlbmRlbmN5UmVzb2x1dGlvbi5zd2lmdA==) | `90.00% <90.00%> (ø)` | | | [Sources/Spezi/Spezi/Spezi+Preview.swift](https://app.codecov.io/gh/StanfordSpezi/Spezi/pull/95?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi#diff-U291cmNlcy9TcGV6aS9TcGV6aS9TcGV6aStQcmV2aWV3LnN3aWZ0) | `78.95% <78.95%> (ø)` | | ------ [Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/StanfordSpezi/Spezi/pull/95?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi). > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi) > `Δ = absolute (impact)`, `ø = not affected`, `? = missing data` > Powered by [Codecov](https://app.codecov.io/gh/StanfordSpezi/Spezi/pull/95?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi). Last update [d8b6645...d8e8643](https://app.codecov.io/gh/StanfordSpezi/Spezi/pull/95?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StanfordSpezi).
Supereg commented 6 months ago

@PSchmiedmayer incorporated the ideas from our meeting today. Feel free to review. We are currently failing slightly on diff coverage. Do think it is necessary to cover this case with an UI test still?

PSchmiedmayer commented 6 months ago

Does it make sense to add https://github.com/StanfordSpezi/Spezi/issues/64#issuecomment-1881658917 in the same PR as it will share a bit of the functionality with the lifecycle method and could e.g. use the same type in the configuration.