Skip to content

Commit 15b16af

Browse files
committed
[PM-16908][TEST] Add some test functions
1 parent d01d841 commit 15b16af

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

crates/bitwarden-wasm-internal/src/client.rs

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ impl BitwardenClient {
4141
res.text().await.map_err(|e| e.to_string())
4242
}
4343

44+
pub fn test(&self) -> crate::test::TestClient {
45+
crate::test::TestClient::new(self.0.clone())
46+
}
47+
4448
pub fn crypto(&self) -> CryptoClient {
4549
CryptoClient::new(self.0.clone())
4650
}

crates/bitwarden-wasm-internal/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod init;
55
mod pure_crypto;
66
mod ssh;
77
mod vault;
8+
mod test;
89

910
pub use client::BitwardenClient;
1011
pub use crypto::CryptoClient;
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use std::rc::Rc;
2+
3+
use bitwarden_core::Client;
4+
use js_sys::Promise;
5+
use wasm_bindgen::prelude::*;
6+
7+
#[wasm_bindgen]
8+
pub struct TestClient(Rc<Client>);
9+
10+
impl TestClient {
11+
pub fn new(client: Rc<Client>) -> Self {
12+
Self(client)
13+
}
14+
}
15+
16+
#[wasm_bindgen]
17+
pub struct TestSub1Client(Rc<Client>);
18+
19+
#[wasm_bindgen]
20+
pub struct TestSub2Client(Rc<Client>);
21+
22+
#[allow(clippy::unused_async)]
23+
#[wasm_bindgen]
24+
impl TestClient {
25+
pub fn sub1(&self) -> TestSub1Client {
26+
TestSub1Client(self.0.clone())
27+
}
28+
29+
#[allow(clippy::unused_async)]
30+
pub async fn async_echo(&self, msg: String) -> String {
31+
format!("ECHOED: '{}'", msg.to_uppercase())
32+
}
33+
}
34+
35+
#[allow(clippy::unused_async)]
36+
#[wasm_bindgen]
37+
impl TestSub1Client {
38+
pub fn sub2(&self) -> TestSub2Client {
39+
TestSub2Client(self.0.clone())
40+
}
41+
42+
pub async fn async_echo_cb(
43+
&self,
44+
#[wasm_bindgen(unchecked_param_type = "(text: string) => Promise<string>")]
45+
f: &js_sys::Function,
46+
) -> Result<String, JsValue> {
47+
let this = JsValue::null();
48+
let val = f.call1(&this, &"hello".into())?;
49+
50+
let pr: Promise = val.dyn_into()?;
51+
let fut = wasm_bindgen_futures::JsFuture::from(pr);
52+
let val = fut
53+
.await?
54+
.as_string()
55+
.ok_or_else(|| js_sys::Error::new("result is not a string"))?;
56+
57+
Ok(format!("Result async: '{}'", val.to_uppercase()))
58+
}
59+
}
60+
61+
#[allow(clippy::unused_async)]
62+
#[wasm_bindgen]
63+
impl TestSub2Client {
64+
#[allow(clippy::unused_async)]
65+
pub async fn get_flags(&self) -> String {
66+
format!("{:?}", self.0.internal.get_flags())
67+
}
68+
}

0 commit comments

Comments
 (0)