Skip to content

Commit

Permalink
[PM-16908][TEST] Add some test functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Feb 28, 2025
1 parent d01d841 commit 5e433c9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/bitwarden-wasm-internal/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ impl BitwardenClient {
res.text().await.map_err(|e| e.to_string())
}

pub fn test(&self) -> crate::test::TestClient {
crate::test::TestClient::new(self.0.clone())
}

Check warning on line 46 in crates/bitwarden-wasm-internal/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/client.rs#L44-L46

Added lines #L44 - L46 were not covered by tests

pub fn crypto(&self) -> CryptoClient {
CryptoClient::new(self.0.clone())
}
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-wasm-internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod custom_types;
mod init;
mod pure_crypto;
mod ssh;
mod test;
mod vault;

pub use client::BitwardenClient;
Expand Down
68 changes: 68 additions & 0 deletions crates/bitwarden-wasm-internal/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::rc::Rc;

use bitwarden_core::Client;
use js_sys::Promise;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct TestClient(Rc<Client>);

impl TestClient {
pub fn new(client: Rc<Client>) -> Self {
Self(client)
}

Check warning on line 13 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L11-L13

Added lines #L11 - L13 were not covered by tests
}

#[wasm_bindgen]
pub struct TestSub1Client(Rc<Client>);

#[wasm_bindgen]
pub struct TestSub2Client(Rc<Client>);

#[allow(clippy::unused_async)]
#[wasm_bindgen]
impl TestClient {
pub fn sub1(&self) -> TestSub1Client {
TestSub1Client(self.0.clone())
}

Check warning on line 27 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L25-L27

Added lines #L25 - L27 were not covered by tests

#[allow(clippy::unused_async)]
pub async fn async_echo(&self, msg: String) -> String {
format!("ECHOED: '{}'", msg.to_uppercase())
}

Check warning on line 32 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L30-L32

Added lines #L30 - L32 were not covered by tests
}

#[allow(clippy::unused_async)]
#[wasm_bindgen]
impl TestSub1Client {
pub fn sub2(&self) -> TestSub2Client {
TestSub2Client(self.0.clone())
}

Check warning on line 40 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L38-L40

Added lines #L38 - L40 were not covered by tests

pub async fn async_echo_cb(
&self,
#[wasm_bindgen(unchecked_param_type = "(text: string) => Promise<string>")]
f: &js_sys::Function,
) -> Result<String, JsValue> {
let this = JsValue::null();
let val = f.call1(&this, &"hello".into())?;

Check warning on line 48 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L42-L48

Added lines #L42 - L48 were not covered by tests

let pr: Promise = val.dyn_into()?;
let fut = wasm_bindgen_futures::JsFuture::from(pr);
let val = fut
.await?
.as_string()
.ok_or_else(|| js_sys::Error::new("result is not a string"))?;

Check warning on line 55 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L50-L55

Added lines #L50 - L55 were not covered by tests

Ok(format!("Result async: '{}'", val.to_uppercase()))
}

Check warning on line 58 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L57-L58

Added lines #L57 - L58 were not covered by tests
}

#[allow(clippy::unused_async)]
#[wasm_bindgen]
impl TestSub2Client {
#[allow(clippy::unused_async)]
pub async fn get_flags(&self) -> String {
format!("{:?}", self.0.internal.get_flags())
}

Check warning on line 67 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L65-L67

Added lines #L65 - L67 were not covered by tests
}

0 comments on commit 5e433c9

Please sign in to comment.