@@ -16,32 +16,35 @@ import scala.concurrent.ExecutionContext.Implicits.global
16
16
import org .junit .Assert ._
17
17
import org .junit .Test
18
18
19
+ import org .scalajs .junit .async ._
20
+
19
21
/* This is currently hard-coded for Node.js modules in particular.
20
22
* We are importing built-in Node.js modules, because we do not have any
21
23
* infrastructure to load non-built-in modules. In the future, we should use
22
24
* our own user-defined ES6 modules written in JavaScript.
23
25
*/
24
26
class DynamicImportTest {
27
+ import DynamicImportTest ._
28
+
29
+ @ Test def testSuccessfulImport (): AsyncResult = {
30
+ await(js.`import`[QueryStringAPI ](" querystring" ).toFuture.map { qs =>
31
+ assertEquals(" object" , js.typeOf(qs))
32
+
33
+ val dict = js.Dictionary (" foo" -> " bar" , " baz" -> " qux" )
25
34
26
- @ Test def testDynImportParsesAndExecutes (): Unit = {
27
- /* Since we do not have support for asynchronous tests, all we can do here
28
- * is test that `js.import` parses, links, and executes without error,
29
- * returning a `Promise`. We can't actually wait for the promise to
30
- * resolve and hence cannot test its result.
31
- *
32
- * We will be able to revisit this in Scala.js 1.x.
33
- */
34
-
35
- def isPromise (x : Any ): Boolean = x.isInstanceOf [js.Promise [_]]
36
-
37
- assertTrue(isPromise(js.`import`[js.Any ](" fs" )))
38
-
39
- val failedPromise = js.`import`[js.Any ](" non-existent-module" )
40
- assertTrue(isPromise(failedPromise))
41
- // Recover to avoid the unhandled rejected Promise warning of Node.js
42
- failedPromise.toFuture.recover {
43
- case th : Throwable => ()
44
- }
35
+ assertEquals(" foo=bar&baz=qux" , qs.stringify(dict))
36
+ assertEquals(" foo:bar;baz:qux" , qs.stringify(dict, " ;" , " :" ))
37
+ })
45
38
}
46
39
40
+ @ Test (expected = classOf [js.JavaScriptException ])
41
+ def testFailedImport (): AsyncResult =
42
+ await(js.`import`[js.Any ](" non-existent-module" ).toFuture)
43
+ }
44
+
45
+ object DynamicImportTest {
46
+ trait QueryStringAPI extends js.Any {
47
+ def stringify (obj : js.Dictionary [String ]): String
48
+ def stringify (obj : js.Dictionary [String ], sep : String , eq : String ): String
49
+ }
47
50
}
0 commit comments