Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #92 from svenwiegand/unit-testing
Browse files Browse the repository at this point in the history
Make setup of unit tests easier
  • Loading branch information
mysticfall committed May 29, 2016
2 parents 5062016 + 2650fa3 commit 167efa8
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/scala/com/greencatsoft/angularjs/Angular.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ object Angular {
def apply(name: String): Option[Module] =
angular.module(name).toOption.map(new Module(_))

def bootstrap(element: Element, modules: String*): Injector = angular.bootstrap(element, modules.toJSArray)

def injector: Injector = angular.injector()

def injector(modules: String*): Injector = angular.injector(modules.toJSArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import scala.scalajs.js.{ UndefOr, | }
@js.native
private[angularjs] trait Angular extends js.Object {

def bootstrap(element: Element, modules: js.Array[String]): Injector = js.native

def injector(): Injector = js.native

def injector(modules: js.Array[String]): Injector = js.native
Expand Down
17 changes: 17 additions & 0 deletions src/main/scala/com/greencatsoft/angularjs/test/AngularMocks.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.greencatsoft.angularjs.test

import com.greencatsoft.angularjs.core.Timeout
import com.greencatsoft.angularjs.injectable

import scala.scalajs.js

object AngularMocks {
val ModuleName = "ngMock"

@js.native
@injectable("$timeout")
trait TimeoutMock extends Timeout {

def flush(): Unit = js.native
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.greencatsoft.angularjs.test

import com.greencatsoft.angularjs.core.Injector
import com.greencatsoft.angularjs.{Angular, Module, internal}
import org.scalajs.dom.document

import scala.language.experimental.macros

/** Provides an injector for your test suites.
*
* Setup for example like this:
* {{{
* class MyDirectiveSpec extends FunSpec with AngularTestEnvironment with ScopeOps with MustMatchers {
* override val module = Angular.module("app", Seq("ngAnimate", "ngMaterial")).directive[MyDirective]
* override val moduleName = "app"
*
* describe("MyDirective") {
* it("must render") {
* val scope = inject[RootScope].$new(true)
* scope.dynamic.greeting = "Hello World!"
*
* val tag = """<my-directive greeting="{{greeting}}"></my-directive>"""
* val element = inject[Compile](tag)(scope, null)
* scope.$digest()
*
* element.textContent must be ("Hello World!")
* }
* }
* }
* }}}
*/
trait AngularTestEnvironment {
/** Your angular module to be used during the test.
*
* For example {{{Angular.module("app", Seq("ngAnimate", "ngMaterial")).directive[MyDirective]}}}
*/
val module: Module

/** The name of your application module */
val moduleName: String

/** Injector you can use in your tests to access services.
*
* You may want to use the `inject[A]` method for more readable code.
*/
implicit lazy val injector: Injector = {
val rootElement = document.documentElement
Angular.bootstrap(rootElement, moduleName)
}

/** Provides readable access to angular services.
*
* Example: {{{inject[RootScope].$new(true)}}}
*/
def inject[A](implicit injector: Injector): A = macro internal.Injector.get[A]
}

0 comments on commit 167efa8

Please sign in to comment.