summaryrefslogtreecommitdiff
path: root/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/jsinterop/DictionaryTest.scala
blob: 8b45395cec45c196302ddca560157cd478aeff9f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js Test Suite        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */
package scala.scalajs.testsuite.jsinterop

import scala.scalajs.js
import org.scalajs.jasminetest.JasmineTest

object DictionaryTest extends JasmineTest {

  describe("scala.scalajs.js.Dictionary") {

    it("should provide an equivalent of the JS delete keyword - #255") {
      val obj = js.Dictionary.empty[js.Any]
      obj("foo") = 42
      obj("bar") = "foobar"

      expect(obj("foo")).toEqual(42)
      expect(obj("bar")).toEqual("foobar")
      obj.delete("foo")
      expect(obj("foo")).toBeUndefined
      expect(obj.asInstanceOf[js.Object].hasOwnProperty("foo")).toBeFalsy
      expect(obj("bar")).toEqual("foobar")
    }

    // This doesn't work on Rhino due to lack of full strict mode support - #679
    unless("rhino").
    it("should behave as specified when deleting a non-configurable property - #461 - #679") {
      val obj = js.Dictionary.empty[js.Any]
      js.Object.defineProperty(obj.asInstanceOf[js.Object], "nonconfig",
          js.Dynamic.literal(value = 4, writable = false).asInstanceOf[js.PropertyDescriptor])
      expect(obj("nonconfig")).toEqual(4)
      expect(() => obj.delete("nonconfig")).toThrow
      expect(obj("nonconfig")).toEqual(4)
    }

    it("should provide `get`") {
      val obj = js.Dictionary.empty[Int]
      obj("hello") = 1

      expect(obj.get("hello").isDefined).toBeTruthy
      expect(obj.get("world").isDefined).toBeFalsy
    }

    it("should treat delete as a statement - #907") {
      val obj = js.Dictionary("a" -> "A")
      obj.delete("a")
    }

    it("should desugar arguments to delete statements - #908") {
      val kh = js.Dynamic.literal( key = "a" ).asInstanceOf[KeyHolder]
      val dict = js.Dictionary[String]("a" -> "A")
      def a[T](foo: String) = dict.asInstanceOf[T]
      a[js.Dictionary[String]]("foo").delete(kh.key)
    }

  }

  trait KeyHolder extends js.Object {
    def key: String = js.native
  }

  describe("scala.scalajs.js.JSConverters.JSRichGenMap") {

    import js.JSConverters._

    it("should provide toJSDictionary") {
      expect(Map("a" -> 1, "b" -> 2).toJSDictionary).toEqual(
          js.Dynamic.literal(a = 1, b = 2))
      expect(Map("a" -> "foo", "b" -> "bar").toJSDictionary).toEqual(
          js.Dynamic.literal(a = "foo", b = "bar"))
    }

  }
}