Skip to content

Commit 179d545

Browse files
committed
Another badge of very dull, very important unit tests. Is the end in sight? (It is not.)
1 parent b14a7d9 commit 179d545

18 files changed

+645
-61
lines changed

Tests/IgniteTesting/Actions/ClickModifier.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// See LICENSE for license information.
66
//
77

8-
import Foundation
98
import Testing
109

1110
@testable import Ignite
@@ -14,8 +13,23 @@ import Testing
1413
@Suite("ClickModifier Tests")
1514
@MainActor
1615
class ClickModifierTests: IgniteTestSuite {
17-
@Test("ExampleTest")
18-
func example() async throws {
16+
@Test("onClick adds onclick attribute to HTML element")
17+
func onClickAddsAttribute() async throws {
18+
let element = Text("Tap me")
19+
.onClick { ShowAlert(message: "Hi") }
1920

21+
let output = element.markupString()
22+
23+
#expect(output.contains(#"onclick="alert('Hi')"#))
24+
}
25+
26+
@Test("onClick adds onclick attribute to inline element")
27+
func onClickInlineElement() async throws {
28+
let element = Emphasis("Click me")
29+
.onClick { ShowAlert(message: "Clicked") }
30+
31+
let output = element.markupString()
32+
33+
#expect(output.contains(#"onclick="alert('Clicked')"#))
2034
}
2135
}

Tests/IgniteTesting/Actions/DismissModal.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// See LICENSE for license information.
66
//
77

8-
import Foundation
98
import Testing
109

1110
@testable import Ignite
@@ -14,8 +13,13 @@ import Testing
1413
@Suite("DismissModal Tests")
1514
@MainActor
1615
class DismissModalTests: IgniteTestSuite {
17-
@Test("ExampleTest")
18-
func example() async throws {
16+
@Test("compile() injects the provided modal ID and hide flow")
17+
func compilesWithProvidedID() async throws {
18+
let action = DismissModal(id: "modal-42")
19+
let output = action.compile()
1920

21+
#expect(output.contains("document.getElementById('modal-42')"))
22+
#expect(output.contains("bootstrap.Modal.getInstance(modal)"))
23+
#expect(output.contains("if (modalInstance) { modalInstance.hide(); }"))
2024
}
2125
}

Tests/IgniteTesting/Actions/DoubleClickModifier.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// See LICENSE for license information.
66
//
77

8-
import Foundation
98
import Testing
109

1110
@testable import Ignite
@@ -14,8 +13,23 @@ import Testing
1413
@Suite("DoubleClickModifier Tests")
1514
@MainActor
1615
class DoubleClickModifierTests: IgniteTestSuite {
17-
@Test("ExampleTest")
18-
func example() async throws {
16+
@Test("onDoubleClick adds ondblclick attribute to HTML element")
17+
func onDoubleClickAddsAttribute() async throws {
18+
let element = Text("Double tap me")
19+
.onDoubleClick { ShowAlert(message: "Double") }
1920

21+
let output = element.markupString()
22+
23+
#expect(output.contains(#"ondblclick="alert('Double')"#))
24+
}
25+
26+
@Test("onDoubleClick adds ondblclick attribute to inline element")
27+
func onDoubleClickInlineElement() async throws {
28+
let element = Emphasis("Double click me")
29+
.onDoubleClick { ShowAlert(message: "Dbl") }
30+
31+
let output = element.markupString()
32+
33+
#expect(output.contains(#"ondblclick="alert('Dbl')"#))
2034
}
2135
}

Tests/IgniteTesting/Actions/HoverModifier.swift

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// See LICENSE for license information.
66
//
77

8-
import Foundation
98
import Testing
109

1110
@testable import Ignite
@@ -14,8 +13,54 @@ import Testing
1413
@Suite("HoverModifier Tests")
1514
@MainActor
1615
class HoverModifierTests: IgniteTestSuite {
17-
@Test("ExampleTest")
18-
func example() async throws {
16+
@Test("onHover adds both onmouseover and onmouseout attributes")
17+
func hoverAddsMouseEvents() async throws {
18+
let element = Text("Hover me")
19+
.onHover { isHovering in
20+
if isHovering {
21+
ShowAlert(message: "entered")
22+
} else {
23+
ShowAlert(message: "left")
24+
}
25+
}
1926

27+
let output = element.markupString()
28+
29+
#expect(output.contains("onmouseover="))
30+
#expect(output.contains("onmouseout="))
31+
}
32+
33+
@Test("Hover true actions go to onmouseover")
34+
func hoverTrueGoesToMouseOver() async throws {
35+
let element = Text("Hover me")
36+
.onHover { isHovering in
37+
if isHovering {
38+
ShowAlert(message: "over")
39+
} else {
40+
ShowAlert(message: "out")
41+
}
42+
}
43+
44+
let output = element.markupString()
45+
46+
#expect(output.contains(#"onmouseover="alert('over')"#))
47+
#expect(output.contains(#"onmouseout="alert('out')"#))
48+
}
49+
50+
@Test("onHover works on inline elements")
51+
func hoverOnInlineElement() async throws {
52+
let element = Emphasis("Hover me")
53+
.onHover { isHovering in
54+
if isHovering {
55+
ShowAlert(message: "in")
56+
} else {
57+
ShowAlert(message: "out")
58+
}
59+
}
60+
61+
let output = element.markupString()
62+
63+
#expect(output.contains("onmouseover="))
64+
#expect(output.contains("onmouseout="))
2065
}
2166
}

Tests/IgniteTesting/Actions/ShowAlert.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// See LICENSE for license information.
66
//
77

8-
import Foundation
98
import Testing
109

1110
@testable import Ignite
@@ -14,8 +13,27 @@ import Testing
1413
@Suite("ShowAlert Tests")
1514
@MainActor
1615
class ShowAlertTests: IgniteTestSuite {
17-
@Test("ExampleTest")
18-
func example() async throws {
16+
@Test("compile() wraps plain message in alert()")
17+
func plainMessage() async throws {
18+
let action = ShowAlert(message: "Hello")
19+
#expect(action.compile() == "alert('Hello')")
20+
}
21+
22+
@Test("compile() escapes single quotes in the message")
23+
func singleQuotesEscaped() async throws {
24+
let action = ShowAlert(message: "It's a test")
25+
#expect(action.compile() == #"alert('It\'s a test')"#)
26+
}
27+
28+
@Test("compile() escapes double quotes in the message")
29+
func doubleQuotesEscaped() async throws {
30+
let action = ShowAlert(message: #"She said "hi""#)
31+
#expect(action.compile() == "alert('She said "hi"')")
32+
}
1933

34+
@Test("compile() produces empty alert for empty message")
35+
func emptyMessage() async throws {
36+
let action = ShowAlert(message: "")
37+
#expect(action.compile() == "alert('')")
2038
}
2139
}

Tests/IgniteTesting/Actions/ShowModal.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// See LICENSE for license information.
66
//
77

8-
import Foundation
98
import Testing
109

1110
@testable import Ignite
@@ -14,8 +13,26 @@ import Testing
1413
@Suite("ShowModal Tests")
1514
@MainActor
1615
class ShowModalTests: IgniteTestSuite {
17-
@Test("ExampleTest")
18-
func example() async throws {
16+
// Note: Individual option tests (.backdrop, .focus, .keyboard, .noBackdrop)
17+
// are covered by Modal.swift's checkModalPresentationOptions parameterized test.
1918

19+
@Test("compile() with no options produces empty options block and modal.show()")
20+
func noOptions() async throws {
21+
let action = ShowModal(id: "myModal")
22+
let output = action.compile()
23+
24+
#expect(output.contains("const options = {"))
25+
#expect(output.contains("new bootstrap.Modal(document.getElementById('myModal'), options)"))
26+
#expect(output.contains("modal.show();"))
27+
}
28+
29+
@Test("compile() with multiple options joins them in the options block")
30+
func multipleOptions() async throws {
31+
let action = ShowModal(id: "m", options: [.focus(true), .keyboard(false)])
32+
let output = action.compile()
33+
34+
#expect(output.contains("focus: true"))
35+
#expect(output.contains("keyboard: false"))
36+
#expect(output.contains("modal.show();"))
2037
}
2138
}

Tests/IgniteTesting/Actions/SwitchTheme.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// See LICENSE for license information.
66
//
77

8-
import Foundation
98
import Testing
109

1110
@testable import Ignite
@@ -14,8 +13,15 @@ import Testing
1413
@Suite("SwitchTheme Tests")
1514
@MainActor
1615
class SwitchThemeTests: IgniteTestSuite {
17-
@Test("ExampleTest")
18-
func example() async throws {
16+
@Test("Light theme compiles to igniteSwitchTheme with light ID")
17+
func lightTheme() async throws {
18+
let action = SwitchTheme(.light)
19+
#expect(action.compile() == "igniteSwitchTheme('light');")
20+
}
1921

22+
@Test("Dark theme compiles to igniteSwitchTheme with dark ID")
23+
func darkTheme() async throws {
24+
let action = SwitchTheme(.dark)
25+
#expect(action.compile() == "igniteSwitchTheme('dark');")
2026
}
2127
}

Tests/IgniteTesting/Elements/ButtonGroup.swift

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// See LICENSE for license information.
66
//
77

8-
import Foundation
98
import Testing
109

1110
@testable import Ignite
@@ -14,8 +13,50 @@ import Testing
1413
@Suite("ButtonGroup Tests")
1514
@MainActor
1615
class ButtonGroupTests: IgniteTestSuite {
17-
@Test("ExampleTest")
18-
func example() async throws {
16+
@Test("ButtonGroup renders with btn-group class")
17+
func hasBtnGroupClass() async throws {
18+
let group = ButtonGroup(accessibilityLabel: "Actions") {
19+
Button("OK")
20+
}
1921

22+
let output = group.markupString()
23+
24+
#expect(output.contains(#"class="btn-group"#))
25+
}
26+
27+
@Test("ButtonGroup renders with group role")
28+
func hasGroupRole() async throws {
29+
let group = ButtonGroup(accessibilityLabel: "Actions") {
30+
Button("OK")
31+
}
32+
33+
let output = group.markupString()
34+
35+
#expect(output.contains(#"role="group""#))
36+
}
37+
38+
@Test("ButtonGroup renders accessibility label in aria-label")
39+
func hasAriaLabel() async throws {
40+
let group = ButtonGroup(accessibilityLabel: "Editing tools") {
41+
Button("Cut")
42+
Button("Copy")
43+
}
44+
45+
let output = group.markupString()
46+
47+
#expect(output.contains(#"aria-label="Editing tools""#))
48+
}
49+
50+
@Test("ButtonGroup contains rendered buttons")
51+
func containsButtons() async throws {
52+
let group = ButtonGroup(accessibilityLabel: "Actions") {
53+
Button("Save")
54+
Button("Cancel")
55+
}
56+
57+
let output = group.markupString()
58+
59+
#expect(output.contains("Save"))
60+
#expect(output.contains("Cancel"))
2061
}
2162
}

Tests/IgniteTesting/Elements/Strikethrough.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,31 @@
55
// See LICENSE for license information.
66
//
77

8-
import Foundation
98
import Testing
109

1110
@testable import Ignite
1211

1312
/// Tests for the `Strikethrough` element.
1413
@Suite("Strikethrough Tests")
1514
@MainActor
16-
struct StrikethroughTests {
17-
@Test("ExampleTest")
18-
func example() async throws {
15+
class StrikethroughTests: IgniteTestSuite {
16+
@Test("String content is wrapped in <s> tags")
17+
func stringContent() async throws {
18+
let element = Strikethrough("deleted text")
19+
let output = element.markupString()
1920

21+
#expect(output == "<s>deleted text</s>")
22+
}
23+
24+
@Test("Inline element content is rendered inside <s> tags")
25+
func inlineElementContent() async throws {
26+
let element = Strikethrough {
27+
Emphasis("important")
28+
}
29+
let output = element.markupString()
30+
31+
#expect(output.contains("<s>"))
32+
#expect(output.contains("</s>"))
33+
#expect(output.contains("<em>important</em>"))
2034
}
2135
}

0 commit comments

Comments
 (0)