Skip to content

Commit b2e7070

Browse files
committed
Fix tests query and use async/await in tests.
1 parent 7f90464 commit b2e7070

File tree

3 files changed

+44
-57
lines changed

3 files changed

+44
-57
lines changed

graphql_client/src/web.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ impl Client {
7979
_query: Q,
8080
variables: Q::Variables,
8181
) -> Result<crate::Response<Q::ResponseData>, ClientError> {
82-
// this can be removed when we convert to async/await
8382
let window = web_sys::window().ok_or_else(|| ClientError::NoWindow)?;
8483
let body =
8584
serde_json::to_string(&Q::build_query(variables)).map_err(|_| ClientError::Body)?;

graphql_client/tests/Germany.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ query Germany {
77
}
88
}
99

10-
query Country($countryCode: String!) {
10+
query Country($countryCode: ID!) {
1111
country(code: $countryCode) {
1212
name
1313
continent {

graphql_client/tests/web.rs

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#![cfg(target_arch = "wasm32")]
22

3-
use futures::Future;
43
use graphql_client::{web::Client, GraphQLQuery};
5-
use wasm_bindgen::JsValue;
6-
use wasm_bindgen_test::wasm_bindgen_test_configure;
74
use wasm_bindgen_test::*;
85

96
wasm_bindgen_test_configure!(run_in_browser);
@@ -18,30 +15,27 @@ fn build_client() {
1815
#[derive(GraphQLQuery)]
1916
#[graphql(
2017
schema_path = "tests/countries_schema.json",
21-
query_path = "tests/Germany.graphql"
18+
query_path = "tests/Germany.graphql",
19+
response_derives = "Debug"
2220
)]
2321
struct Germany;
2422

25-
#[wasm_bindgen_test(async)]
26-
fn test_germany() -> impl Future<Item = (), Error = JsValue> {
27-
Client::new("https://countries.trevorblades.com/")
23+
#[wasm_bindgen_test]
24+
async fn test_germany() {
25+
let response = Client::new("https://countries.trevorblades.com/")
2826
.call(Germany, germany::Variables)
29-
.map(|response| {
30-
let continent_name = response
31-
.data
32-
.expect("response data is not null")
33-
.country
34-
.expect("country is not null")
35-
.continent
36-
.expect("continent is not null")
37-
.name
38-
.expect("germany is on a continent");
39-
40-
assert_eq!(continent_name, "Europe");
41-
})
42-
.map_err(|err| {
43-
panic!("{:?}", err);
44-
})
27+
.await
28+
.expect("successful response");
29+
let continent_name = response
30+
.data
31+
.expect("response data is not null")
32+
.country
33+
.expect("country is not null")
34+
.continent
35+
.expect("continent is not null")
36+
.name
37+
.expect("germany is on a continent");
38+
assert_eq!(continent_name, "Europe");
4539
}
4640

4741
#[derive(GraphQLQuery)]
@@ -51,50 +45,44 @@ fn test_germany() -> impl Future<Item = (), Error = JsValue> {
5145
)]
5246
struct Country;
5347

54-
#[wasm_bindgen_test(async)]
55-
fn test_country() -> impl Future<Item = (), Error = JsValue> {
56-
Client::new("https://countries.trevorblades.com/")
48+
#[wasm_bindgen_test]
49+
async fn test_country() {
50+
let response = Client::new("https://countries.trevorblades.com/")
5751
.call(
5852
Country,
5953
country::Variables {
6054
country_code: "CN".to_owned(),
6155
},
6256
)
63-
.map(|response| {
64-
let continent_name = response
65-
.data
66-
.expect("response data is not null")
67-
.country
68-
.expect("country is not null")
69-
.continent
70-
.expect("continent is not null")
71-
.name
72-
.expect("country is on a continent");
73-
74-
assert_eq!(continent_name, "Asia");
75-
})
76-
.map_err(|err| {
77-
panic!("{:?}", err);
78-
})
57+
.await
58+
.expect("successful response");
59+
let continent_name = response
60+
.data
61+
.expect("response data is not null")
62+
.country
63+
.expect("country is not null")
64+
.continent
65+
.expect("continent is not null")
66+
.name
67+
.expect("country is on a continent");
68+
assert_eq!(continent_name, "Asia");
7969
}
8070

81-
#[wasm_bindgen_test(async)]
82-
fn test_bad_url() -> impl Future<Item = (), Error = JsValue> {
83-
Client::new("https://example.com/non-existent/graphql/endpoint")
71+
#[wasm_bindgen_test]
72+
async fn test_bad_url() {
73+
let result = Client::new("https://example.com/non-existent/graphql/endpoint")
8474
.call(
8575
Country,
8676
country::Variables {
8777
country_code: "CN".to_owned(),
8878
},
8979
)
90-
.map(|_response| panic!("The API endpoint does not exist, this should not be called."))
91-
.map_err(|err| {
92-
assert_eq!(
93-
err,
94-
graphql_client::web::ClientError::Network(
95-
"NetworkError when attempting to fetch resource.".into()
96-
)
97-
);
98-
})
99-
.then(|_| Ok(()))
80+
.await;
81+
match result {
82+
Ok(_response) => panic!("The API endpoint does not exist, this should not be called."),
83+
Err(graphql_client::web::ClientError::Network(msg)) => {
84+
assert_eq!(msg, "NetworkError when attempting to fetch resource.")
85+
}
86+
Err(err) => panic!("unexpected error: {}", err),
87+
}
10088
}

0 commit comments

Comments
 (0)