Skip to content

Commit 9aa6ee1

Browse files
authored
Add a test that checks that nested dependencies are correctly overridden (#1647)
1 parent e0334a0 commit 9aa6ee1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Tests/DependenciesTests/DependencyValuesTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ final class DependencyValuesTests: XCTestCase {
176176
XCTAssertEqual(childDependencyLateBindingEscaped.fetch(), 1_000)
177177
}
178178
}
179+
180+
func testNestedDependencyIsOverridden() {
181+
DependencyValues.withValue(\.nestedValue.value, 10) {
182+
@Dependency(\.nestedValue) var nestedValue: NestedValue
183+
@Dependency(\.nestedValue.value) var value: Int
184+
XCTAssertEqual(nestedValue.value, 10)
185+
XCTAssertEqual(value, 10)
186+
}
187+
}
179188
}
180189

181190
struct SomeDependency: TestDependencyKey {
@@ -198,6 +207,11 @@ struct ChildDependencyLateBinding: TestDependencyKey {
198207
}
199208
}
200209
}
210+
struct NestedValue: TestDependencyKey {
211+
static var testValue: Self { .init() }
212+
var value: Int = 0
213+
}
214+
201215
extension DependencyValues {
202216
var someDependency: SomeDependency {
203217
get { self[SomeDependency.self] }
@@ -211,6 +225,10 @@ extension DependencyValues {
211225
get { self[ChildDependencyLateBinding.self] }
212226
set { self[ChildDependencyLateBinding.self] = newValue }
213227
}
228+
var nestedValue: NestedValue {
229+
get { self[NestedValue.self] }
230+
set { self[NestedValue.self] = newValue }
231+
}
214232
}
215233

216234
private let someDate = Date(timeIntervalSince1970: 1_234_567_890)

0 commit comments

Comments
 (0)