mirror of
https://github.com/phranck/TUIkit.git
synced 2026-06-20 09:54:37 +00:00
AppStateTests: Tests AppState as isolated instances without touching RenderNotifier.current. Removed .serialized (no shared state). StateStorageIdentityTests: Replaced testEnvironment() with testStorage() that only creates a StateStorage. No longer sets RenderNotifier.current. StateBox.didSet harmlessly calls setNeedsRender() on the default global. StatePropertyTests: The one test that needs RenderNotifier.current (stateTriggerRender) now swaps it atomically — set, mutate, check, restore in a single synchronous sequence with no yield points. Root cause: All three suites ran .serialized internally but parallel to each other. Since RenderNotifier.current is a process-global static variable, concurrent writes caused one suite to overwrite another's AppState, making needsRender checks fail intermittently.
69 lines
1.8 KiB
Swift
69 lines
1.8 KiB
Swift
//
|
|
// AppStateTests.swift
|
|
// TUIkit
|
|
//
|
|
// Tests for AppState: render flag management and observer notification.
|
|
//
|
|
|
|
import Testing
|
|
|
|
@testable import TUIkit
|
|
|
|
@Suite("AppState Tests")
|
|
struct AppStateTests {
|
|
|
|
@Test("AppState initially does not need render")
|
|
func initialState() {
|
|
let appState = AppState()
|
|
#expect(appState.needsRender == false)
|
|
}
|
|
|
|
@Test("setNeedsRender marks state as dirty")
|
|
func setNeedsRender() {
|
|
let appState = AppState()
|
|
appState.setNeedsRender()
|
|
#expect(appState.needsRender == true)
|
|
}
|
|
|
|
@Test("didRender resets needsRender flag")
|
|
func didRenderResets() {
|
|
let appState = AppState()
|
|
appState.setNeedsRender()
|
|
#expect(appState.needsRender == true)
|
|
appState.didRender()
|
|
#expect(appState.needsRender == false)
|
|
}
|
|
|
|
@Test("setNeedsRender notifies observers")
|
|
func observerNotified() {
|
|
let appState = AppState()
|
|
nonisolated(unsafe) var notified = false
|
|
appState.observe {
|
|
notified = true
|
|
}
|
|
appState.setNeedsRender()
|
|
#expect(notified == true)
|
|
}
|
|
|
|
@Test("Multiple observers all get notified")
|
|
func multipleObservers() {
|
|
let appState = AppState()
|
|
nonisolated(unsafe) var count = 0
|
|
appState.observe { count += 1 }
|
|
appState.observe { count += 1 }
|
|
appState.observe { count += 1 }
|
|
appState.setNeedsRender()
|
|
#expect(count == 3)
|
|
}
|
|
|
|
@Test("clearObservers removes all observers")
|
|
func clearObservers() {
|
|
let appState = AppState()
|
|
nonisolated(unsafe) var notified = false
|
|
appState.observe { notified = true }
|
|
appState.clearObservers()
|
|
appState.setNeedsRender()
|
|
#expect(notified == false)
|
|
}
|
|
}
|