2017-12-01 17:23:03 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2018-02-06 19:11:20 -05:00
|
|
|
import {defineDirective} from '../../src/render3/index';
|
2018-03-21 18:10:34 -04:00
|
|
|
import {bind, elementEnd, elementProperty, elementStart, loadDirective} from '../../src/render3/instructions';
|
2018-04-10 23:57:09 -04:00
|
|
|
import {RenderFlags} from '../../src/render3/interfaces/definition';
|
2017-12-01 17:23:03 -05:00
|
|
|
import {renderToHtml} from './render_util';
|
|
|
|
|
|
|
|
describe('directive', () => {
|
|
|
|
|
|
|
|
describe('host', () => {
|
|
|
|
|
|
|
|
it('should support host bindings in directives', () => {
|
|
|
|
let directiveInstance: Directive|undefined;
|
|
|
|
|
|
|
|
class Directive {
|
|
|
|
klass = 'foo';
|
2018-01-09 00:57:50 -05:00
|
|
|
static ngDirectiveDef = defineDirective({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: Directive,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['', 'dir', '']],
|
2018-01-09 00:57:50 -05:00
|
|
|
factory: () => directiveInstance = new Directive,
|
2018-01-22 22:52:06 -05:00
|
|
|
hostBindings: (directiveIndex: number, elementIndex: number) => {
|
2018-03-21 18:10:34 -04:00
|
|
|
elementProperty(
|
|
|
|
elementIndex, 'className', bind(loadDirective<Directive>(directiveIndex).klass));
|
2018-01-09 00:57:50 -05:00
|
|
|
}
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
function Template(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'span', ['dir', '']);
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 15:58:41 -04:00
|
|
|
const defs = [Directive];
|
2018-03-26 00:32:39 -04:00
|
|
|
expect(renderToHtml(Template, {}, defs)).toEqual('<span class="foo" dir=""></span>');
|
2017-12-01 17:23:03 -05:00
|
|
|
directiveInstance !.klass = 'bar';
|
2018-03-26 00:32:39 -04:00
|
|
|
expect(renderToHtml(Template, {}, defs)).toEqual('<span class="bar" dir=""></span>');
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|