//! Integration tests exercising the public API of `decamelize-keys`.

use decamelize_keys::{decamelize_keys, decamelize_keys_with, Options};
use serde_json::json;

#[test]
fn api_payload_to_snake_case() {
    let payload = json!({
        "userId": 1,
        "displayName": "Ada",
        "contactInfo": { "emailAddress": "ada@example.com" }
    });
    assert_eq!(
        decamelize_keys_with(&payload, &Options::new().deep(true)),
        json!({
            "user_id": 1,
            "display_name": "Ada",
            "contact_info": { "email_address": "ada@example.com" }
        })
    );
}

#[test]
fn dash_separator() {
    assert_eq!(
        decamelize_keys_with(&json!({ "backgroundColor": "red" }), &Options::new().separator("-")),
        json!({ "background-color": "red" })
    );
}

#[test]
fn exclude_keys() {
    let opts = Options::new().exclude(["XMLHttpRequest"]);
    assert_eq!(
        decamelize_keys_with(&json!({ "XMLHttpRequest": 1, "fooBar": 2 }), &opts),
        json!({ "XMLHttpRequest": 1, "foo_bar": 2 })
    );
}

#[test]
fn array_of_records() {
    assert_eq!(
        decamelize_keys(&json!([{ "rowId": 1 }, { "rowId": 2 }])),
        json!([{ "row_id": 1 }, { "row_id": 2 }])
    );
}
