@@ -5,70 +5,87 @@ import XCTest
55
66final class StoreContextTests : XCTestCase {
77 @MainActor
8- func testInit ( ) {
8+ func testRegisterRootAndScopes ( ) throws {
99 let store = AtomStore ( )
1010 let atom = TestAtom ( value: 0 )
11- let subscriberState = SubscriberState ( )
12- let subscriber = Subscriber ( subscriberState)
13- let scopeToken = ScopeKey . Token ( )
11+ let rootScopeToken = ScopeKey . Token ( )
12+ let scope0Token = ScopeKey . Token ( )
13+ let scope1Token = ScopeKey . Token ( )
14+ let scope2Token = ScopeKey . Token ( )
15+
1416 let context = StoreContext . registerRoot (
1517 in: store,
16- scopeKey: scopeToken . key,
18+ scopeKey: rootScopeToken . key,
1719 overrides: [
18- OverrideKey ( atom) : Override< TestAtom< Int>> { _ in
19- 10
20- }
20+ OverrideKey ( atom) : Override< TestAtom< Int>> { _ in 0 }
2121 ] ,
22- observers: [ ]
22+ observers: [
23+ Observer { _ in }
24+ ]
2325 )
2426
25- XCTAssertEqual ( context. watch ( atom, subscriber: subscriber, subscription: Subscription ( ) ) , 10 )
26- XCTAssertEqual (
27- store. state. caches. compactMapValues { $0 as? AtomCache < TestAtom < Int > > } ,
28- [
29- AtomKey ( atom) : AtomCache ( atom: atom, value: 10 )
27+ let rootScope = try XCTUnwrap ( store. state. scopes [ rootScopeToken. key] )
28+ XCTAssertEqual ( rootScope. overrides. count, 1 )
29+ XCTAssertEqual ( rootScope. observers. count, 1 )
30+ XCTAssertTrue ( rootScope. inheritedScopeKeys. isEmpty)
31+
32+ _ = context. registerScope (
33+ scopeID: ScopeID ( 0 ) ,
34+ scopeKey: scope0Token. key,
35+ overrides: [
36+ OverrideKey ( atom) : Override< TestAtom< Int>> { _ in 0 }
37+ ] ,
38+ observers: [
39+ Observer { _ in }
3040 ]
3141 )
32- }
3342
34- @ MainActor
35- func testScoped ( ) {
36- let store = AtomStore ( )
37- let subscriberState = SubscriberState ( )
38- let subscriber = Subscriber ( subscriberState )
39- let scopeToken = ScopeKey . Token ( )
40- let atom = TestAtom ( value : 0 )
41- var snapshots0 = [ Snapshot ] ( )
42- var snapshots1 = [ Snapshot ] ( )
43- let rootScopeToken = ScopeKey . Token ( )
44- let context = StoreContext . registerRoot (
45- in : store ,
46- scopeKey : rootScopeToken . key ,
47- overrides : [ : ] ,
43+ let scope0 = try XCTUnwrap ( store . state . scopes [ scope0Token . key ] )
44+ XCTAssertEqual ( scope0 . overrides . count , 1 )
45+ XCTAssertEqual ( scope0 . observers . count , 1 )
46+ XCTAssertEqual (
47+ scope0 . inheritedScopeKeys ,
48+ [ ScopeID ( 0 ) : scope0Token . key ]
49+ )
50+
51+ let scoped1Context = context . registerScope (
52+ scopeID : ScopeID ( 1 ) ,
53+ scopeKey : scope1Token . key ,
54+ overrides : [
55+ OverrideKey ( atom ) : Override < TestAtom < Int>> { _ in 0 }
56+ ] ,
4857 observers: [
49- Observer { snapshots0 . append ( $0 ) }
58+ Observer { _ in }
5059 ]
5160 )
52- let scopedContext = context. registerScope (
53- scopeID: ScopeID ( DefaultScopeID ( ) ) ,
54- scopeKey: scopeToken. key,
61+
62+ let scope1 = try XCTUnwrap ( store. state. scopes [ scope1Token. key] )
63+ XCTAssertEqual ( scope1. overrides. count, 1 )
64+ XCTAssertEqual ( scope1. observers. count, 1 )
65+ XCTAssertEqual (
66+ scope1. inheritedScopeKeys,
67+ [ ScopeID ( 1 ) : scope1Token. key]
68+ )
69+
70+ _ = scoped1Context. registerScope (
71+ scopeID: ScopeID ( 2 ) ,
72+ scopeKey: scope2Token. key,
5573 overrides: [
56- OverrideKey ( atom) : Override< TestAtom< Int>> { _ in
57- 10
58- }
74+ OverrideKey ( atom) : Override< TestAtom< Int>> { _ in 0 }
5975 ] ,
6076 observers: [
61- Observer { snapshots1 . append ( $0 ) }
77+ Observer { _ in }
6278 ]
6379 )
6480
65- XCTAssertEqual ( scopedContext . watch ( atom , subscriber : subscriber , subscription : Subscription ( ) ) , 10 )
66- XCTAssertFalse ( snapshots0 . isEmpty )
67- XCTAssertFalse ( snapshots1 . isEmpty )
81+ let scope2 = try XCTUnwrap ( store . state . scopes [ scope2Token . key ] )
82+ XCTAssertEqual ( scope2 . overrides . count , 1 )
83+ XCTAssertEqual ( scope2 . observers . count , 1 )
6884 XCTAssertEqual (
69- store . state . caches . compactMapValues { $0 as? AtomCache < TestAtom < Int > > } ,
85+ scope2 . inheritedScopeKeys ,
7086 [
71- AtomKey ( atom, scopeKey: scopeToken. key) : AtomCache ( atom: atom, value: 10 )
87+ ScopeID ( 1 ) : scope1Token. key,
88+ ScopeID ( 2 ) : scope2Token. key,
7289 ]
7390 )
7491 }
0 commit comments