1
1
#![ cfg( target_arch = "wasm32" ) ]
2
2
3
- use futures:: Future ;
4
3
use graphql_client:: { web:: Client , GraphQLQuery } ;
5
- use wasm_bindgen:: JsValue ;
6
- use wasm_bindgen_test:: wasm_bindgen_test_configure;
7
4
use wasm_bindgen_test:: * ;
8
5
9
6
wasm_bindgen_test_configure ! ( run_in_browser) ;
@@ -18,30 +15,27 @@ fn build_client() {
18
15
#[ derive( GraphQLQuery ) ]
19
16
#[ graphql(
20
17
schema_path = "tests/countries_schema.json" ,
21
- query_path = "tests/Germany.graphql"
18
+ query_path = "tests/Germany.graphql" ,
19
+ response_derives = "Debug"
22
20
) ]
23
21
struct Germany ;
24
22
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/" )
28
26
. 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" ) ;
45
39
}
46
40
47
41
#[ derive( GraphQLQuery ) ]
@@ -51,50 +45,44 @@ fn test_germany() -> impl Future<Item = (), Error = JsValue> {
51
45
) ]
52
46
struct Country ;
53
47
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/" )
57
51
. call (
58
52
Country ,
59
53
country:: Variables {
60
54
country_code : "CN" . to_owned ( ) ,
61
55
} ,
62
56
)
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" ) ;
79
69
}
80
70
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" )
84
74
. call (
85
75
Country ,
86
76
country:: Variables {
87
77
country_code : "CN" . to_owned ( ) ,
88
78
} ,
89
79
)
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
+ }
100
88
}
0 commit comments