93 lines · 2.7 KB · SwiftRaw
// The chrome's paper dress (phase 20): the sheet, the question and the// receipt. Behaviour, not pictures — what each keeps doing now that it is a// piece of paper: a sheet's detents, a question's destructive role and its// in-flight lock, a receipt's undo inside its window on a fake clock.import SwiftUIimport XCTest@testable import CottonUI@MainActorfinal class SheetDressTests: XCTestCase {}@MainActorfinal class ConfirmDecisionDressTests: XCTestCase { private func dialog( tone: ConfirmDecisionTone = .danger, isLoading: Bool, confirmed: @escaping () -> Void = {}, cancelled: @escaping () -> Void = {} ) -> ConfirmDecisionDialog { ConfirmDecisionDialog( title: "Empty the bin?", message: "Everything in the bin is erased.", confirmLabel: "Empty", tone: tone, isLoading: isLoading, identifier: "bin-empty", onConfirm: confirmed, onCancel: cancelled ) } func testConfirmAndCancelEachReachTheirOwnCallback() { var confirmed = 0 var cancelled = 0 let open = dialog(isLoading: false, confirmed: { confirmed += 1 }, cancelled: { cancelled += 1 }) open.confirm() XCTAssertEqual(confirmed, 1) XCTAssertEqual(cancelled, 0) open.cancel() XCTAssertEqual(confirmed, 1) XCTAssertEqual(cancelled, 1) } func testNeitherVerbWorksWhileTheActIsInFlight() { // A double-tapped Empty is two erasures. var calls = 0 let busy = dialog(isLoading: true, confirmed: { calls += 1 }, cancelled: { calls += 1 }) busy.confirm() busy.cancel() XCTAssertEqual(calls, 0) }}@MainActorfinal class ToastDressTests: XCTestCase { func testUndoInsideTheWindowTakesTheActBack() { let clock = ManualToastScheduler() let toasts = ToastCenter(scheduler: clock) var restored = false toasts.show("Bin: Pay the water bill", undo: { restored = true }) XCTAssertEqual(clock.scheduledWindows, [ToastWindow.rowUndo]) toasts.undo() XCTAssertTrue(restored) XCTAssertNil(toasts.current) // The window's timer was called off with the receipt. XCTAssertEqual(clock.liveCount, 0) } func testUndoAfterTheWindowHasClosedDoesNothing() { let clock = ManualToastScheduler() let toasts = ToastCenter(scheduler: clock) var restored = false toasts.show("Bin: Pay the water bill", undo: { restored = true }) clock.advance() XCTAssertNil(toasts.current) toasts.undo() XCTAssertFalse(restored) }}