|
| 1 | +import XCTest |
| 2 | + |
| 3 | +@testable import Atoms |
| 4 | + |
| 5 | +@MainActor |
| 6 | +final class ChangesModifierTests: XCTestCase { |
| 7 | + func testChanges() { |
| 8 | + let atom = TestStateAtom(defaultValue: "") |
| 9 | + let context = AtomTestContext() |
| 10 | + var updatedCount = 0 |
| 11 | + |
| 12 | + context.onUpdate = { |
| 13 | + updatedCount += 1 |
| 14 | + } |
| 15 | + |
| 16 | + XCTAssertEqual(updatedCount, 0) |
| 17 | + XCTAssertEqual(context.watch(atom.changes), "") |
| 18 | + |
| 19 | + context[atom] = "modified" |
| 20 | + |
| 21 | + XCTAssertEqual(updatedCount, 1) |
| 22 | + XCTAssertEqual(context.watch(atom.changes), "modified") |
| 23 | + |
| 24 | + context[atom] = "modified" |
| 25 | + |
| 26 | + // Should not be updated with an equivalent value. |
| 27 | + XCTAssertEqual(updatedCount, 1) |
| 28 | + } |
| 29 | + |
| 30 | + func testKey() { |
| 31 | + let modifier = ChangesModifier<Int>() |
| 32 | + |
| 33 | + XCTAssertEqual(modifier.key, modifier.key) |
| 34 | + XCTAssertEqual(modifier.key.hashValue, modifier.key.hashValue) |
| 35 | + } |
| 36 | + |
| 37 | + func testShouldUpdate() { |
| 38 | + let modifier = ChangesModifier<Int>() |
| 39 | + |
| 40 | + XCTAssertFalse(modifier.shouldUpdate(newValue: 100, oldValue: 100)) |
| 41 | + XCTAssertTrue(modifier.shouldUpdate(newValue: 100, oldValue: 200)) |
| 42 | + } |
| 43 | + |
| 44 | + func testModify() { |
| 45 | + let atom = TestValueAtom(value: 0) |
| 46 | + let modifier = ChangesModifier<Int>() |
| 47 | + let transaction = Transaction(key: AtomKey(atom)) {} |
| 48 | + let context = AtomModifierContext<Int>(transaction: transaction) { _ in } |
| 49 | + let value = modifier.modify(value: 100, context: context) |
| 50 | + |
| 51 | + XCTAssertEqual(value, 100) |
| 52 | + } |
| 53 | + |
| 54 | + func testAssociateOverridden() { |
| 55 | + let atom = TestValueAtom(value: 0) |
| 56 | + let modifier = ChangesModifier<Int>() |
| 57 | + let transaction = Transaction(key: AtomKey(atom)) {} |
| 58 | + let context = AtomModifierContext<Int>(transaction: transaction) { _ in } |
| 59 | + let value = modifier.associateOverridden(value: 100, context: context) |
| 60 | + |
| 61 | + XCTAssertEqual(value, 100) |
| 62 | + } |
| 63 | +} |
0 commit comments