feat: export `inject` API

To avoid the cumbersome `angular.mock.inject` usage to get a service,
rename `getAngularService` to `inject` and export it for end users of
ATL.
ts
Jason Staten 5 years ago
parent fd1ea4b558
commit f3997536e5

@ -34,8 +34,8 @@ function render(
// they're passing us a custom container or not. // they're passing us a custom container or not.
mountedContainers.add(container) mountedContainers.add(container)
const $rootScope = getAngularService('$rootScope') const $rootScope = inject('$rootScope')
const $compile = getAngularService('$compile') const $compile = inject('$compile')
const $scope = $rootScope.$new() const $scope = $rootScope.$new()
Object.assign($scope, scope) Object.assign($scope, scope)
@ -107,7 +107,7 @@ function toCamel(s) {
function assertNoUnknownElements(element) { function assertNoUnknownElements(element) {
const {tagName} = element const {tagName} = element
if (tagName.includes('-')) { if (tagName.includes('-')) {
const $injector = getAngularService('$injector') const $injector = inject('$injector')
const directiveName = `${toCamel(tagName)}Directive` const directiveName = `${toCamel(tagName)}Directive`
if (!$injector.has(directiveName)) { if (!$injector.has(directiveName)) {
throw Error( throw Error(
@ -118,7 +118,7 @@ function assertNoUnknownElements(element) {
Array.from(element.children).forEach(assertNoUnknownElements) Array.from(element.children).forEach(assertNoUnknownElements)
} }
function getAngularService(name) { function inject(name) {
let service let service
angular.mock.inject([ angular.mock.inject([
name, name,
@ -130,7 +130,7 @@ function getAngularService(name) {
} }
function fireEvent(...args) { function fireEvent(...args) {
const $rootScope = getAngularService('$rootScope') const $rootScope = inject('$rootScope')
const result = dtlFireEvent(...args) const result = dtlFireEvent(...args)
$rootScope.$digest() $rootScope.$digest()
return result return result
@ -138,7 +138,7 @@ function fireEvent(...args) {
Object.keys(dtlFireEvent).forEach(key => { Object.keys(dtlFireEvent).forEach(key => {
fireEvent[key] = (...args) => { fireEvent[key] = (...args) => {
const $rootScope = getAngularService('$rootScope') const $rootScope = inject('$rootScope')
const result = dtlFireEvent[key](...args) const result = dtlFireEvent[key](...args)
$rootScope.$digest() $rootScope.$digest()
return result return result
@ -150,9 +150,9 @@ fireEvent.mouseEnter = fireEvent.mouseOver
fireEvent.mouseLeave = fireEvent.mouseOut fireEvent.mouseLeave = fireEvent.mouseOut
function flush(millis = 50) { function flush(millis = 50) {
const $browser = getAngularService('$browser') const $browser = inject('$browser')
const $rootScope = getAngularService('$rootScope') const $rootScope = inject('$rootScope')
const $interval = getAngularService('$interval') const $interval = inject('$interval')
$interval.flush(millis) $interval.flush(millis)
$browser.defer.flush(millis) $browser.defer.flush(millis)
$rootScope.$digest() $rootScope.$digest()
@ -168,4 +168,4 @@ function wait(callback, options = {}) {
} }
export * from '@testing-library/dom' export * from '@testing-library/dom'
export {render, cleanup, fireEvent, wait, flush} export {render, cleanup, fireEvent, wait, flush, inject}

Loading…
Cancel
Save