重构全局 Toast 为自动消失的顶部横幅

采用全宽主色横幅居中展示文案,2.2 秒后自动隐藏,并补充定时器重置行为的测试。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-23 14:48:31 +08:00
parent 89386e8fbc
commit 09922f70fe
5 changed files with 146 additions and 27 deletions

View File

@ -0,0 +1,60 @@
//
// ToastCenterTests.swift
// suixinkanTests
//
// Created by Codex on 2026/6/23.
//
import XCTest
@testable import suixinkan
@MainActor
/// Toast
final class ToastCenterTests: XCTestCase {
/// show Toast
func testShowStoresMessage() {
let center = ToastCenter(autoDismissNanoseconds: 100_000_000)
center.show("保存成功")
XCTAssertEqual(center.snapshotForTests, ToastSnapshot(message: "保存成功"))
}
/// Toast
func testToastAutoDismissesAfterDelay() async throws {
let center = ToastCenter(autoDismissNanoseconds: 60_000_000)
center.show("保存成功")
try await Task.sleep(nanoseconds: 90_000_000)
XCTAssertEqual(center.snapshotForTests, ToastSnapshot(message: nil))
}
/// show
func testRepeatedShowResetsAutoDismissTimer() async throws {
let center = ToastCenter(autoDismissNanoseconds: 100_000_000)
center.show("第一条")
try await Task.sleep(nanoseconds: 70_000_000)
center.show("第二条")
try await Task.sleep(nanoseconds: 60_000_000)
XCTAssertEqual(center.snapshotForTests, ToastSnapshot(message: "第二条"))
try await Task.sleep(nanoseconds: 70_000_000)
XCTAssertEqual(center.snapshotForTests, ToastSnapshot(message: nil))
}
/// dismiss
func testDismissClearsMessageImmediately() async throws {
let center = ToastCenter(autoDismissNanoseconds: 100_000_000)
center.show("保存成功")
center.dismiss()
XCTAssertEqual(center.snapshotForTests, ToastSnapshot(message: nil))
try await Task.sleep(nanoseconds: 130_000_000)
XCTAssertEqual(center.snapshotForTests, ToastSnapshot(message: nil))
}
}