|
| 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