外部テンプレートを使用してコンポーネントをテストしたいと思います。しかし、コンポーネント内の要素を見つけようとするたびに、何の結果も得られません。あたかもコンポーネントが空であるかのように見えます。コンポーネントを外部テンプレート(angularjs = 1.5)でテストする
テンプレートをロードする方法に問題がありますか、またはコンポーネントを正しくコンパイルしていますか?
プロジェクト構造:
libs/
app/
arc/
app.component.js
app.module.js
feature1/
A.component.js
A.component.spec.js
A.controller.js
A.html
feature2/
B.component.js
B.component.spec.js
B.controller.js
B.html
karma.conf.js
//jshint strict: false
module.exports = function(config) {
config.set({
basePath: './src/app',
frameworks: ['jasmine'],
files: [
'../../libs/angular/angular.js',
'../../libs/angular-mocks/angular-mocks.js',
'../../libs/angular-ui-router/release/angular-ui-router.js',
'../../libs/angular-animate/angular-animate.js',
'../../libs/angular-resource/angular-resource.js',
'**/*.module.js',
'**/*.spec.js',
'**/*.html',
],
preprocessors: {
'**/*.html': ['ng-html2js'],
'**/!(*.mock|*.spec).js': ['coverage']
},
ngHtml2JsPreprocessor: {
// strip this from the file path
///stripPrefix: '',
cacheIdFromPath: function(filepath) {
let cacheId = filepath.split('/');
return cacheId[ cacheId.length - 1 ];
},
// create a single module that contains templates from all the files
moduleName: 'templates'
},
colors: true,
autoWatch: true,
browsers: ['Chrome'],
reporters: ["spec"],
specReporter: {
maxLogLines: 5, // limit number of lines logged per test
suppressErrorSummary: true, // do not print error summary
suppressFailed: false, // do not print information about failed tests
suppressPassed: false, // do not print information about passed tests
suppressSkipped: true, // do not print information about skipped tests
showSpecTiming: false, // print the time elapsed for each spec
failFast: true // test would finish with error when a first fail occurs.
}
});
};
A.html
<header> test </header>
A.component.js
var AComponent = {
templateUrl: './A.html',
controller: 'AController',
bindings: {
}
};
angular
.module('app')
.component('feature1', AComponent);
A.component.spec.js
'use strict';
describe('Component: AComponent', function() {
beforeEach(module('app'));
beforeEach(module('templates'));
var element;
var component;
var scope;
beforeEach(inject(function($rootScope, $compile){
scope = $rootScope.$new();
element = angular.element('<feature1> </feature1>');
scope.$apply(function(){
$compile(element)(scope);
});
}));
it('should contain header element', function() {
console.log(element.find('header').length)
});
});
''ちょうどタイプミスだった私の悪いああオープンタグ –
を、一致する最後の 'S'を削除します。しかし、根本的な原因ではありません。 – Alexus
'stat'クラスを含む要素が表示されないので、' element.find( '。stat') 'は何も見つからないでしょう。 –