Skip to content

Commit b575012

Browse files
authored
Merge pull request #52 from davesmith00000/patch-1
Added an example to the README
2 parents 0f48de2 + fc0ef72 commit b575012

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

README.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ This repository contains `scalajs-env-jsdom-nodejs` for Scala.js 1.x. In
88
Scala.js 0.6.x, the Node.js with jsdom environment is part of the core
99
distribution.
1010

11-
## Usage
11+
## Setup
12+
13+
These instructions setup your test environment on Node.js such that you can write tests as if they were running on an HTML page.
1214

1315
Add the following line to `project/plugins.sbt`:
1416

@@ -26,7 +28,56 @@ Finally, make sure that [jsdom](https://github.com/jsdom/jsdom) 10.0.0 or later
2628
You can install it with
2729

2830
```bash
29-
$ npm install jsdom
31+
$ npm install jsdom --save-dev
32+
```
33+
34+
Or with yarn if you prefer
35+
36+
```bash
37+
$ yarn add jsdom --dev
3038
```
3139

3240
See [the Scaladoc](https://javadoc.io/doc/org.scala-js/scalajs-env-jsdom-nodejs_2.13/latest/org/scalajs/jsenv/jsdomnodejs/index.html) for other configuration options.
41+
42+
## Usage - Writing a test
43+
44+
To access the dom, you will need to install [`scala-js-dom`](https://github.com/scala-js/scala-js-dom):
45+
46+
```scala
47+
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "2.1.0"
48+
```
49+
50+
Create a test in your favourite test framework, here is an example using [MUnit](https://scalameta.org/munit/).
51+
52+
```scala
53+
import org.scalajs.dom.document
54+
55+
class ExampleJsDomTests extends munit.FunSuite {
56+
57+
test("Example jsdom test") {
58+
val id = "my-fancy-element"
59+
val content = "Hi there and greetings!"
60+
61+
// Create a new div element
62+
val newDiv = document.createElement("div")
63+
64+
// Create an id attribute and assign it to the div
65+
val a = document.createAttribute("id")
66+
a.value = id
67+
newDiv.setAttributeNode(a)
68+
69+
// Create some text content
70+
val newContent = document.createTextNode(content)
71+
72+
// Add the text node to the newly created div
73+
newDiv.appendChild(newContent)
74+
75+
// Add the newly created element and its content into the DOM
76+
document.body.appendChild(newDiv)
77+
78+
// Find the element by id on the page, and compare the contents
79+
assertEquals(document.getElementById(id).innerHTML, content)
80+
}
81+
82+
}
83+
```

0 commit comments

Comments
 (0)