1- import Combine
21import XCTest
32
43@testable import Atoms
@@ -9,10 +8,10 @@ final class RefreshableTests: XCTestCase {
98 let store = AtomStore ( )
109 let subscriberState = SubscriberState ( )
1110 let subscriber = Subscriber ( subscriberState)
12- let atom = TestCustomRefreshableAtom {
13- Just ( 0 )
14- } refresh: {
15- . success ( 1 )
11+ let atom = TestCustomRefreshableAtom { _ in
12+ 0
13+ } refresh: { _ in
14+ 1
1615 }
1716 let key = AtomKey ( atom)
1817 var snapshots = [ Snapshot] ( )
@@ -22,33 +21,32 @@ final class RefreshableTests: XCTestCase {
2221 do {
2322 // Should call custom refresh behavior
2423
25- let phase0 = await context. refresh ( atom)
26- XCTAssertEqual ( phase0 . value , 1 )
24+ let value0 = await context. refresh ( atom)
25+ XCTAssertEqual ( value0 , 1 )
2726 XCTAssertNil ( store. state. caches [ key] )
2827 XCTAssertNil ( store. state. states [ key] )
2928 XCTAssertTrue ( snapshots. isEmpty)
3029
3130 var updateCount = 0
32- let phase1 = context. watch (
31+ let value1 = context. watch (
3332 atom,
3433 subscriber: subscriber,
3534 subscription: Subscription {
3635 updateCount += 1
3736 }
3837 )
3938
40- XCTAssertTrue ( phase1 . isSuspending )
39+ XCTAssertEqual ( value1 , 0 )
4140
4241 snapshots. removeAll ( )
43-
44- let phase2 = await context. refresh ( atom)
45- XCTAssertEqual ( phase2. value, 1 )
42+ let value2 = await context. refresh ( atom)
43+ XCTAssertEqual ( value2, 1 )
4644 XCTAssertNotNil ( store. state. states [ key] )
47- XCTAssertEqual ( ( store. state. caches [ key] as? AtomCache < TestCustomRefreshableAtom < Just < Int > > > ) ? . value, . success ( 1 ) )
45+ XCTAssertEqual ( ( store. state. caches [ key] as? AtomCache < TestCustomRefreshableAtom < Int > > ) ? . value, 1 )
4846 XCTAssertEqual ( updateCount, 1 )
4947 XCTAssertEqual (
50- snapshots. map { $0. caches. mapValues { $0. value as? AsyncPhase < Int , Never > } } ,
51- [ [ key: . success ( 1 ) ] ]
48+ snapshots. map { $0. caches. mapValues { $0. value as? Int } } ,
49+ [ [ key: 1 ] ]
5250 )
5351
5452 context. unwatch ( atom, subscriber: subscriber)
@@ -64,30 +62,58 @@ final class RefreshableTests: XCTestCase {
6462 scopeID: ScopeID ( DefaultScopeID ( ) ) ,
6563 observers: [ ] ,
6664 overrides: [
67- OverrideKey ( atom) : Override < TestCustomRefreshableAtom < Just < Int > > > ( isScoped: true ) { _ in . success ( 2 ) }
65+ OverrideKey ( atom) : Override < TestCustomRefreshableAtom < Int > > ( isScoped: true ) { _ in 2 }
6866 ]
6967 )
7068
71- let phase0 = scopedContext. watch ( atom, subscriber: subscriber, subscription: Subscription ( ) )
72- XCTAssertEqual ( phase0 . value , 2 )
69+ let value0 = scopedContext. watch ( atom, subscriber: subscriber, subscription: Subscription ( ) )
70+ XCTAssertEqual ( value0 , 2 )
7371
74- let phase1 = await scopedContext. refresh ( atom)
75- XCTAssertEqual ( phase1 . value , 1 )
72+ let value1 = await scopedContext. refresh ( atom)
73+ XCTAssertEqual ( value1 , 1 )
7674 XCTAssertNotNil ( store. state. states [ overrideAtomKey] )
7775 XCTAssertEqual (
78- ( store. state. caches [ overrideAtomKey] as? AtomCache < TestCustomRefreshableAtom < Just < Int > > > ) ? . value,
79- . success ( 1 )
76+ ( store. state. caches [ overrideAtomKey] as? AtomCache < TestCustomRefreshableAtom < Int > > ) ? . value,
77+ 1
8078 )
8179 }
8280
8381 do {
8482 // Should not make new state and cache
8583
86- let phase = await context. refresh ( atom)
84+ let value = await context. refresh ( atom)
8785
88- XCTAssertEqual ( phase . value, 1 )
86+ XCTAssertEqual ( value, 1 )
8987 XCTAssertNil ( store. state. states [ key] )
9088 XCTAssertNil ( store. state. caches [ key] )
9189 }
9290 }
91+
92+ @MainActor
93+ func testTransitiveRefresh( ) async {
94+ let parentAtom = TestTaskAtom { 0 }
95+ let atom = TestCustomRefreshableAtom { context in
96+ context. watch ( parentAtom. phase)
97+ } refresh: { context in
98+ await context. refresh ( parentAtom. phase)
99+ }
100+ let context = AtomTestContext ( )
101+
102+ var updateCount = 0
103+ context. onUpdate = {
104+ updateCount += 1
105+ }
106+
107+ XCTAssertTrue ( context. watch ( atom) . isSuspending)
108+
109+ await context. waitForUpdate ( )
110+ XCTAssertEqual ( context. watch ( atom) . value, 0 )
111+ XCTAssertEqual ( updateCount, 1 )
112+
113+ let value = await context. refresh ( atom) . value
114+
115+ XCTAssertEqual ( value, 0 )
116+ XCTAssertEqual ( context. watch ( atom) . value, 0 )
117+ XCTAssertEqual ( updateCount, 2 )
118+ }
93119}
0 commit comments