Giter VIP home page Giter VIP logo

scala-js-env-jsdom-nodejs's Introduction

scalajs-env-jsdom-nodejs

scalajs-env-jsdom-nodejs is a JavaScript environment for Scala.js (a JSEnv) running Node.js with jsdom.

This repository contains scalajs-env-jsdom-nodejs for Scala.js 1.x. In Scala.js 0.6.x, the Node.js with jsdom environment is part of the core distribution.

Setup

These instructions setup your test environment on Node.js such that you can write tests as if they were running on an HTML page.

Add the following line to project/plugins.sbt:

libraryDependencies += "org.scala-js" %% "scalajs-env-jsdom-nodejs" % "1.1.0"

and the following line to build.sbt (possibly in the settings/jsSettings of Scala.js projects):

jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv()

Finally, make sure that jsdom 10.0.0 or later is avilable in your project. You can install it with

$ npm install jsdom --save-dev

Or with yarn if you prefer

$ yarn add jsdom --dev

See the Scaladoc for other configuration options.

Usage - Writing a test

To access the dom, you will need to install scala-js-dom:

libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "2.1.0"

Create a test in your favourite test framework, here is an example using MUnit.

import org.scalajs.dom.document

class ExampleJsDomTests extends munit.FunSuite {

  test("Example jsdom test") {
    val id = "my-fancy-element"
    val content = "Hi there and greetings!"

    // Create a new div element
    val newDiv = document.createElement("div")

    // Create an id attribute and assign it to the div
    val a = document.createAttribute("id")
    a.value = id
    newDiv.setAttributeNode(a)

    // Create some text content
    val newContent = document.createTextNode(content)

    // Add the text node to the newly created div
    newDiv.appendChild(newContent)

    // Add the newly created element and its content into the DOM
    document.body.appendChild(newDiv)

    // Find the element by id on the page, and compare the contents
    assertEquals(document.getElementById(id).innerHTML, content)
  }

}

scala-js-env-jsdom-nodejs's People

Contributors

davesmith00000 avatar gzm0 avatar julienrf avatar nicolasstucki avatar sjrd avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.