Function tauri::test::assert_ipc_response

source ·
pub fn assert_ipc_response<T: Serialize + Debug>(
    window: &Window<MockRuntime>,
    payload: InvokePayload,
    expected: Result<T, T>
)
Available on crate feature test only.
Expand description

Executes the given IPC message and assert the response matches the expected value.

§Examples

#[tauri::command]
fn ping() -> &'static str {
  "pong"
}

fn create_app<R: tauri::Runtime>(mut builder: tauri::Builder<R>) -> tauri::App<R> {
  builder
    .invoke_handler(tauri::generate_handler![ping])
    // remove the string argument on your app
    .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
    .expect("failed to build app")
}

use tauri::Manager;
use tauri::test::mock_builder;
fn main() {
  // app createion with a `MockRuntime`
  let app = create_app(mock_builder());
  let window = app.get_window("main").unwrap();

  // run the `ping` command and assert it returns `pong`
  tauri::test::assert_ipc_response(
    &window,
    tauri::InvokePayload {
      cmd: "ping".into(),
      tauri_module: None,
      callback: tauri::api::ipc::CallbackFn(0),
      error: tauri::api::ipc::CallbackFn(1),
      inner: serde_json::Value::Null,
    },
    // the expected response is a success with the "pong" payload
    // we could also use Err("error message") here to ensure the command failed
    Ok("pong")
  );
}