|
| 1 | +import {describe, xit, it, expect, beforeEach, ddescribe, iit} from 'test_lib/test_lib'; |
| 2 | +import {DOM} from 'facade/dom'; |
| 3 | +import {Injector} from 'di/di'; |
| 4 | +import {Lexer, Parser, ChangeDetector} from 'change_detection/change_detection'; |
| 5 | +import {Compiler, CompilerCache} from 'core/compiler/compiler'; |
| 6 | +import {DirectiveMetadataReader} from 'core/compiler/directive_metadata_reader'; |
| 7 | +import {Decorator, Component} from 'core/annotations/annotations'; |
| 8 | +import {TemplateConfig} from 'core/annotations/template_config'; |
| 9 | +import {NgElement} from 'core/dom/element'; |
| 10 | +import {NgNonBindable} from 'directives/ng_non_bindable'; |
| 11 | + |
| 12 | +export function main() { |
| 13 | + describe('ng-non-bindable', () => { |
| 14 | + var view, cd, compiler, component; |
| 15 | + beforeEach(() => { |
| 16 | + compiler = new Compiler(null, new DirectiveMetadataReader(), new Parser(new Lexer()), new CompilerCache()); |
| 17 | + }); |
| 18 | + |
| 19 | + function createElement(html) { |
| 20 | + return DOM.createTemplate(html).content.firstChild; |
| 21 | + } |
| 22 | + |
| 23 | + function createView(pv) { |
| 24 | + component = new TestComponent(); |
| 25 | + view = pv.instantiate(null); |
| 26 | + view.hydrate(new Injector([]), null, component); |
| 27 | + cd = new ChangeDetector(view.recordRange); |
| 28 | + } |
| 29 | + |
| 30 | + function compileWithTemplate(template) { |
| 31 | + return compiler.compile(TestComponent, createElement(template)); |
| 32 | + } |
| 33 | + |
| 34 | + it('should not interpolate children', (done) => { |
| 35 | + var template = '<div>{{text}}<span ng-non-bindable>{{text}}</span></div>'; |
| 36 | + compileWithTemplate(template).then((pv) => { |
| 37 | + createView(pv); |
| 38 | + cd.detectChanges(); |
| 39 | + expect(DOM.getText(view.nodes[0])).toEqual('foo{{text}}'); |
| 40 | + done(); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should ignore directives on child nodes', (done) => { |
| 45 | + var template = '<div ng-non-bindable><span id=child test-dec>{{text}}</span></div>'; |
| 46 | + compileWithTemplate(template).then((pv) => { |
| 47 | + createView(pv); |
| 48 | + cd.detectChanges(); |
| 49 | + var span = DOM.querySelector(view.nodes[0], '#child'); |
| 50 | + expect(DOM.hasClass(span, 'compiled')).toBeFalsy(); |
| 51 | + done(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should trigger directives on the same node', (done) => { |
| 56 | + var template = '<div><span id=child ng-non-bindable test-dec>{{text}}</span></div>'; |
| 57 | + compileWithTemplate(template).then((pv) => { |
| 58 | + createView(pv); |
| 59 | + cd.detectChanges(); |
| 60 | + var span = DOM.querySelector(view.nodes[0], '#child'); |
| 61 | + expect(DOM.hasClass(span, 'compiled')).toBeTruthy(); |
| 62 | + done(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +@Component({ |
| 69 | + selector: 'test-cmp', |
| 70 | + template: new TemplateConfig({ |
| 71 | + inline: '', // each test swaps with a custom template. |
| 72 | + directives: [NgNonBindable, TestDecorator] |
| 73 | + }) |
| 74 | +}) |
| 75 | +class TestComponent { |
| 76 | + text: string; |
| 77 | + constructor() { |
| 78 | + this.text = 'foo'; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +@Decorator({ |
| 83 | + selector: '[test-dec]' |
| 84 | +}) |
| 85 | +class TestDecorator { |
| 86 | + constructor(el: NgElement) { |
| 87 | + DOM.addClass(el.domElement, 'compiled'); |
| 88 | + } |
| 89 | +} |
0 commit comments