This repository has been archived by the owner on Sep 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from svenwiegand/unit-testing
Make setup of unit tests easier
- Loading branch information
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/scala/com/greencatsoft/angularjs/test/AngularMocks.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/scala/com/greencatsoft/angularjs/test/AngularTestEnvironment.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |