Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unit test deadlock #15

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions packages/dom/tests/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use std::sync::{Arc, LazyLock, Mutex};
use std::sync::{Arc, LazyLock, Mutex, MutexGuard};

use testing_library_dom::{configure, get_config, ConfigFnOrPartial, PartialConfig};

static CONFIG_LOCK: Mutex<()> = Mutex::new(());
static ORIGINAL_CONFIG: LazyLock<Arc<Mutex<PartialConfig>>> =
LazyLock::new(|| Arc::new(Mutex::new(PartialConfig::default())));

fn before_each() {
fn before_each() -> MutexGuard<'static, ()> {
// Ensure only one test modifies the config at the same time.
let lock = CONFIG_LOCK
.lock()
.expect("Config mutex should be acquired.");

configure(ConfigFnOrPartial::Fn(Box::new(|existing_config| {
// Grab the existing configuration so we can restore it at the end of the test.
let mut original_config = ORIGINAL_CONFIG
Expand All @@ -16,39 +22,43 @@ fn before_each() {
// Don't change the existing config.
PartialConfig::default()
})));

lock
}

fn after_each() {
fn after_each(lock: MutexGuard<'_, ()>) {
let original_config = ORIGINAL_CONFIG
.lock()
.expect("Original config mutex should be acquired.");

configure(ConfigFnOrPartial::Partial((*original_config).clone()));

drop(lock);
}

#[test]
fn get_config_returns_existing_configuration() {
before_each();
let lock = before_each();

let config = get_config();
assert_eq!("data-testid", config.test_id_attribute);

after_each();
after_each(lock);
}

#[test]
fn configure_merges_a_delta_rather_than_replacing_the_whole_config() {
before_each();
let lock = before_each();

let config = get_config();
assert_eq!("data-testid", config.test_id_attribute);

after_each();
after_each(lock);
}

#[test]
fn configure_overrides_existing_values() {
before_each();
let lock = before_each();

configure(ConfigFnOrPartial::Partial(
PartialConfig::default().test_id_attribute("new-id".into()),
Expand All @@ -57,12 +67,12 @@ fn configure_overrides_existing_values() {
let config = get_config();
assert_eq!("new-id", config.test_id_attribute);

after_each();
after_each(lock);
}

#[test]
fn configure_passes_existing_config_out_to_config_function() {
before_each();
let lock = before_each();

// Create a new config key based on the value of an existing one.
configure(ConfigFnOrPartial::Fn(Box::new(|existing_config| {
Expand All @@ -74,5 +84,5 @@ fn configure_passes_existing_config_out_to_config_function() {
let config = get_config();
assert_eq!("data-testid-derived", config.test_id_attribute);

after_each();
after_each(lock);
}