diff --git a/src/main/scala/com/greencatsoft/angularjs/Angular.scala b/src/main/scala/com/greencatsoft/angularjs/Angular.scala index 7c51961..1c4c051 100644 --- a/src/main/scala/com/greencatsoft/angularjs/Angular.scala +++ b/src/main/scala/com/greencatsoft/angularjs/Angular.scala @@ -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) diff --git a/src/main/scala/com/greencatsoft/angularjs/internal/Angular.scala b/src/main/scala/com/greencatsoft/angularjs/internal/Angular.scala index 5a30f56..8728d9d 100644 --- a/src/main/scala/com/greencatsoft/angularjs/internal/Angular.scala +++ b/src/main/scala/com/greencatsoft/angularjs/internal/Angular.scala @@ -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 diff --git a/src/main/scala/com/greencatsoft/angularjs/test/AngularMocks.scala b/src/main/scala/com/greencatsoft/angularjs/test/AngularMocks.scala new file mode 100644 index 0000000..4caf714 --- /dev/null +++ b/src/main/scala/com/greencatsoft/angularjs/test/AngularMocks.scala @@ -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 + } +} \ No newline at end of file diff --git a/src/main/scala/com/greencatsoft/angularjs/test/AngularTestEnvironment.scala b/src/main/scala/com/greencatsoft/angularjs/test/AngularTestEnvironment.scala new file mode 100644 index 0000000..89dcda8 --- /dev/null +++ b/src/main/scala/com/greencatsoft/angularjs/test/AngularTestEnvironment.scala @@ -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 = """""" + * 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] +}