Skip to content

Commit e626128

Browse files
committed
Update test
1 parent 129bcf9 commit e626128

22 files changed

+1020
-566
lines changed

Sources/Atoms/Core/StoreContext.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ internal struct StoreContext {
3434
// The dependencies added in `willSet/didSet` will not be released until the value is invalidated and
3535
// is going to be a bug, so `AtomTransactionContenxt` will no longer be passed.
3636
// https://github.com/ra1028/swiftui-atom-properties/issues/18
37-
let store = getStore()
3837
let key = AtomKey(atom)
3938
let transaction = Transaction(key: key) {
4039
// Do nothing.
@@ -43,8 +42,6 @@ internal struct StoreContext {
4342
update(atom: atom, with: value, updatesChildrenOnNextRunLoop: updatesChildrenOnNextRunLoop)
4443
}
4544

46-
store.state.transactions[key] = transaction
47-
4845
context.transaction { context in
4946
atom.willSet(newValue: value, oldValue: oldValue, context: context)
5047
update(atom: atom, with: value)

Tests/AtomsTests/Context/AtomRelationContextTests.swift

Lines changed: 0 additions & 153 deletions
This file was deleted.

Tests/AtomsTests/Context/AtomTestContextTests.swift

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,6 @@ final class AtomTestContextTests: XCTestCase {
7979
}
8080

8181
func testObserve() {
82-
final class TestObserver: AtomObserver {
83-
var asignedAtomKeys = [AtomKey]()
84-
var unassignedAtomKeys = [AtomKey]()
85-
var changedAtomKeys = [AtomKey]()
86-
87-
func atomAssigned<Node: Atom>(atom: Node) {
88-
asignedAtomKeys.append(AtomKey(atom))
89-
}
90-
91-
func atomUnassigned<Node: Atom>(atom: Node) {
92-
unassignedAtomKeys.append(AtomKey(atom))
93-
}
94-
95-
func atomChanged<Node: Atom>(snapshot: Snapshot<Node>) {
96-
changedAtomKeys.append(AtomKey(snapshot.atom))
97-
}
98-
}
99-
10082
let atom = TestStateAtom(defaultValue: 100)
10183
let key = AtomKey(atom)
10284
let observers = [TestObserver(), TestObserver()]
@@ -109,23 +91,23 @@ final class AtomTestContextTests: XCTestCase {
10991
context.watch(atom)
11092

11193
for observer in observers {
112-
XCTAssertEqual(observer.asignedAtomKeys, [key])
94+
XCTAssertEqual(observer.assignedAtomKeys, [key])
11395
XCTAssertEqual(observer.unassignedAtomKeys, [])
11496
XCTAssertEqual(observer.changedAtomKeys, [key])
11597
}
11698

11799
context[atom] = 200
118100

119101
for observer in observers {
120-
XCTAssertEqual(observer.asignedAtomKeys, [key])
102+
XCTAssertEqual(observer.assignedAtomKeys, [key])
121103
XCTAssertEqual(observer.unassignedAtomKeys, [])
122104
XCTAssertEqual(observer.changedAtomKeys, [key, key])
123105
}
124106

125107
context.unwatch(atom)
126108

127109
for observer in observers {
128-
XCTAssertEqual(observer.asignedAtomKeys, [key])
110+
XCTAssertEqual(observer.assignedAtomKeys, [key])
129111
XCTAssertEqual(observer.unassignedAtomKeys, [key])
130112
XCTAssertEqual(observer.changedAtomKeys, [key, key])
131113
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import XCTest
2+
3+
@testable import Atoms
4+
5+
@MainActor
6+
final class AtomTransactionContextTests: XCTestCase {
7+
func testRead() {
8+
let atom = TestValueAtom(value: 100)
9+
let store = Store()
10+
let transaction = Transaction(key: AtomKey(atom)) {}
11+
let context = AtomTransactionContext(store: StoreContext(store), transaction: transaction)
12+
13+
XCTAssertEqual(context.read(atom), 100)
14+
}
15+
16+
func testSet() {
17+
let atom = TestStateAtom(defaultValue: 100)
18+
let store = Store()
19+
let transaction = Transaction(key: AtomKey(atom)) {}
20+
let context = AtomTransactionContext(store: StoreContext(store), transaction: transaction)
21+
22+
XCTAssertEqual(context.watch(atom), 100)
23+
24+
context.set(200, for: atom)
25+
26+
XCTAssertEqual(context.watch(atom), 200)
27+
}
28+
29+
func testRefresh() async {
30+
let atom = TestTaskAtom(value: 100)
31+
let store = Store()
32+
let transaction = Transaction(key: AtomKey(atom)) {}
33+
let context = AtomTransactionContext(store: StoreContext(store), transaction: transaction)
34+
35+
let value = await context.refresh(atom).value
36+
37+
XCTAssertEqual(value, 100)
38+
}
39+
40+
func testReset() {
41+
let atom = TestStateAtom(defaultValue: 0)
42+
let store = Store()
43+
let transaction = Transaction(key: AtomKey(atom)) {}
44+
let context = AtomTransactionContext(store: StoreContext(store), transaction: transaction)
45+
46+
XCTAssertEqual(context.watch(atom), 0)
47+
48+
context[atom] = 100
49+
50+
XCTAssertEqual(context.watch(atom), 100)
51+
52+
context.reset(atom)
53+
54+
XCTAssertEqual(context.read(atom), 0)
55+
}
56+
57+
func testWatch() {
58+
let atom0 = TestValueAtom(value: 100)
59+
let atom1 = TestStateAtom(defaultValue: 200)
60+
let store = Store()
61+
let transaction = Transaction(key: AtomKey(atom0)) {}
62+
let context = AtomTransactionContext(store: StoreContext(store), transaction: transaction)
63+
64+
let value = context.watch(atom1)
65+
66+
XCTAssertEqual(value, 200)
67+
XCTAssertEqual(store.graph.children, [AtomKey(atom1): [AtomKey(atom0)]])
68+
XCTAssertEqual(store.graph.dependencies, [AtomKey(atom0): [AtomKey(atom1)]])
69+
}
70+
71+
func testAddTermination() {
72+
let atom = TestValueAtom(value: 100)
73+
let store = Store()
74+
let transaction = Transaction(key: AtomKey(atom)) {}
75+
let context = AtomTransactionContext(store: StoreContext(store), transaction: transaction)
76+
77+
context.addTermination {}
78+
79+
XCTAssertEqual(transaction.terminations.count, 1)
80+
81+
transaction.terminate()
82+
context.addTermination {}
83+
84+
XCTAssertEqual(transaction.terminations.count, 0)
85+
}
86+
87+
func testKeepUntilTermination() {
88+
let atom = TestValueAtom(value: 100)
89+
let store = Store()
90+
let transaction = Transaction(key: AtomKey(atom)) {}
91+
let context = AtomTransactionContext(store: StoreContext(store), transaction: transaction)
92+
var object: Object? = Object()
93+
weak var objectRef = object
94+
95+
context.keepUntilTermination(object!)
96+
97+
XCTAssertNotNil(objectRef)
98+
99+
object = nil
100+
XCTAssertNotNil(objectRef)
101+
102+
transaction.terminate()
103+
XCTAssertNil(objectRef)
104+
}
105+
}

0 commit comments

Comments
 (0)