Я ищу подход к макету функции jQuery attr
для возврата тестового значения, но я не уверен, как эффективно макетировать jQuery в моей настройке AngularJS / Jasmine / TypeScript. Любая помощь будет оценена.
Спасибо!
factory.ts
module extractors {
export interface IExtractorFactory {
extractMyDataAttribute(): string | undefined;
}
export class ExtractorFactory {
constructor(private $document: ng.IDocumentService) { }
extractMyDataAttribute = (): string | undefined => {
let attr: string | undefined = this.$document.find('html').attr('myDataAttribute');
//Perform subsequent code...removed for brevity and instead just returning attr
return attr;
}
}
factory.$inject = ['$document'];
function factory($document: ng.IDocumentService): IExtractorFactory {
return new ExtractorFactory($document);
}
angular.module('extractors').factory('extractorFactory', factory);
}
factory.spec.ts
module extractors {
describe('extractors', () => {
let extractorFactory: IExtractorFactory;
beforeEach(angular.mock.module('app'));
beforeEach(inject((
_extractorFactory_: IExtractorFactory
) => {
extractorFactory = _extractorFactory_;
}));
fit('extracts a value', () => {
// arrange
//TODO: What should the spy be to return a value from the attr function?
// act
let myDataAttribute: string | undefined = extractorFactory.extractMyDataAttribute();
// assert
expect(myDataAttribute).toEqual('foo');
});
});
}