-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce a buildComponent test helper
- Loading branch information
Showing
11 changed files
with
96 additions
and
56 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import angular from 'angular'; | ||
import 'angular-mocks'; | ||
import {camelCaseToDashes, dashesToCamelCase} from './utils'; | ||
|
||
let counter = 0; | ||
|
||
export function buildComponent(ComponentClass) { | ||
if (!ComponentClass.annotation || !ComponentClass.annotation.module || !ComponentClass.annotation.module.name) { | ||
throw new Error(`ComponentClass is not annotated: ${ComponentClass.name}`); | ||
} | ||
|
||
const tagName = camelCaseToDashes(ComponentClass.annotation.name).toLowerCase(); | ||
const template = `<${tagName}></${tagName}>`; | ||
const elProperty = dashesToCamelCase(tagName); | ||
|
||
counter += 1; | ||
const componentName = `TestedComponents${counter}`; | ||
|
||
angular.module(componentName, [ComponentClass.annotation.module.name]); | ||
|
||
let controller = null; | ||
|
||
angular.mock.module(componentName); | ||
angular.mock.inject((_$compile_, _$rootScope_) => { | ||
const compiledTemplate = compileTemplate(template, _$compile_, _$rootScope_); | ||
controller = compiledTemplate.controller(elProperty); | ||
controller._element = compiledTemplate; | ||
}); | ||
|
||
return controller; | ||
} | ||
|
||
function compileTemplate(template, $compile, $rootScope) { | ||
const el = angular.element(template.trim()); | ||
$compile(el)($rootScope.$new()); | ||
$rootScope.$digest(); | ||
return el; | ||
} |
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,16 @@ | ||
/*eslint-env node, jasmine*/ | ||
import {buildComponent} from 'anglue/anglue'; | ||
|
||
describe('Test helpers', () => { | ||
describe('buildComponent', () => { | ||
|
||
class Generic {} | ||
|
||
it('should throw an error when called with a class without annotation', () => { | ||
expect(() => { | ||
buildComponent(Generic); | ||
}).toThrow(new Error('ComponentClass is not annotated: Generic')); | ||
}); | ||
|
||
}); | ||
}); |
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