Skip to content

Commit

Permalink
Notifications: make the failure messages clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Mar 20, 2024
1 parent 9514e2f commit 706c387
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,32 @@ private fun DynaNodeGroup.notificationsTests() {
}

test("expectNotifications() fails if there are expected notifications but no actual notifications") {
expectThrows(AssertionError::class) {
expectThrows(AssertionError::class, "Notifications: Expected [Error, Warning, baz] but there are no notifications. Dump:") {
expectNotifications("Error", "Warning", "baz")
}
}

test("expectNotifications() fails if no notifications are expected but there are some") {
Notification.show("Foo")
expectThrows(AssertionError::class, "Notifications: expected [] but got [Foo]. Dump:") {
expectNoNotifications()
}
}

test("expectNotifications() fails if notifications don't match expected value") {
Notification.show("Foo")
expectThrows(AssertionError::class, "Notifications: expected [Bar, Baz] but got [Foo]. Dump:") {
expectNotifications("Bar", "Baz")
}
}

test("expectNotifications() fails if notifications don't match expected value 2") {
Notification.show("Bar")
expectThrows(AssertionError::class, "Notifications: expected [Bar, Baz] but got [Bar]. Dump:") {
expectNotifications("Bar", "Baz")
}
}

test("assert on shown notifications") {
Notification.show("Error")
expectNotifications("Error")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.github.mvysny.kaributools.*
import com.vaadin.flow.component.UI
import com.vaadin.flow.component.dialog.Dialog
import com.vaadin.flow.component.notification.Notification
import kotlin.streams.toList
import kotlin.test.fail

/**
* Returns the list of currently displayed notifications.
Expand Down Expand Up @@ -32,9 +32,18 @@ public fun getNotifications(): List<Notification> {
/**
* Expects that given list of notifications is displayed. Also clears the notifications.
*/
public fun expectNotifications(vararg texts: String) {
public fun expectNotifications(vararg expected: String) {
val notifications: List<Notification> = getNotifications()
expectList(*texts) { notifications.map { it.getText() } }
val actual = notifications.map { it.getText() }
if (actual.isEmpty()) {
if (expected.isNotEmpty()) {
fail("Notifications: Expected ${expected.toList()} but there are no notifications. Dump:\n${UI.getCurrent().toPrettyTree()}")
}
} else {
if (actual != expected.toList()) {
fail("Notifications: expected ${expected.toList()} but got $actual. Dump:\n${UI.getCurrent().toPrettyTree()}")
}
}
clearNotifications()
}

Expand Down

0 comments on commit 706c387

Please sign in to comment.