2015-01-23 03:27:36 -05:00
|
|
|
import {describe, xit, it, expect, beforeEach, ddescribe, iit, IS_DARTIUM, el} from 'test_lib/test_lib';
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-02-02 19:25:34 -05:00
|
|
|
import {DOM} from 'facade/src/dom';
|
2014-12-17 04:01:08 -05:00
|
|
|
|
|
|
|
import {Injector} from 'di/di';
|
2015-01-21 15:05:52 -05:00
|
|
|
import {Lexer, Parser, ChangeDetector, dynamicChangeDetection} from 'change_detection/change_detection';
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-02-02 19:25:34 -05:00
|
|
|
import {Compiler, CompilerCache} from 'core/src/compiler/compiler';
|
|
|
|
import {DirectiveMetadataReader} from 'core/src/compiler/directive_metadata_reader';
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-02-02 19:25:34 -05:00
|
|
|
import {Component} from 'core/src/annotations/annotations';
|
|
|
|
import {TemplateConfig} from 'core/src/annotations/template_config';
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-02-02 19:25:34 -05:00
|
|
|
import {NgIf} from 'directives/src/ng_if';
|
2014-12-17 04:01:08 -05:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('ng-if', () => {
|
|
|
|
var view, cd, compiler, component;
|
|
|
|
beforeEach(() => {
|
2015-01-21 15:05:52 -05:00
|
|
|
compiler = new Compiler(dynamicChangeDetection, null, new DirectiveMetadataReader(),
|
|
|
|
new Parser(new Lexer()), new CompilerCache());
|
2014-12-17 04:01:08 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
function createView(pv) {
|
|
|
|
component = new TestComponent();
|
|
|
|
view = pv.instantiate(null);
|
|
|
|
view.hydrate(new Injector([]), null, component);
|
|
|
|
cd = view.changeDetector;
|
|
|
|
}
|
|
|
|
|
|
|
|
function compileWithTemplate(template) {
|
2015-01-23 03:27:36 -05:00
|
|
|
return compiler.compile(TestComponent, el(template));
|
2014-12-17 04:01:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
it('should work in a template attribute', (done) => {
|
|
|
|
compileWithTemplate('<div><copy-me template="ng-if booleanCondition">hello</copy-me></div>').then((pv) => {
|
|
|
|
createView(pv);
|
|
|
|
cd.detectChanges();
|
|
|
|
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(1);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work in a template element', (done) => {
|
|
|
|
compileWithTemplate('<div><template [ng-if]="booleanCondition"><copy-me>hello2</copy-me></template></div>').then((pv) => {
|
|
|
|
createView(pv);
|
|
|
|
cd.detectChanges();
|
|
|
|
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(1);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should toggle node when condition changes', (done) => {
|
|
|
|
compileWithTemplate('<div><copy-me template="ng-if booleanCondition">hello</copy-me></div>').then((pv) => {
|
|
|
|
createView(pv);
|
2015-01-23 03:27:36 -05:00
|
|
|
|
2014-12-17 04:01:08 -05:00
|
|
|
component.booleanCondition = false;
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(0);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('');
|
|
|
|
|
|
|
|
|
|
|
|
component.booleanCondition = true;
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(1);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello');
|
|
|
|
|
|
|
|
component.booleanCondition = false;
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(0);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update several nodes with ng-if', (done) => {
|
|
|
|
var templateString =
|
|
|
|
'<div>' +
|
|
|
|
'<copy-me template="ng-if numberCondition + 1 >= 2">helloNumber</copy-me>' +
|
|
|
|
'<copy-me template="ng-if stringCondition == \'foo\'">helloString</copy-me>' +
|
|
|
|
'<copy-me template="ng-if functionCondition(stringCondition, numberCondition)">helloFunction</copy-me>' +
|
|
|
|
'</div>';
|
|
|
|
compileWithTemplate(templateString).then((pv) => {
|
|
|
|
createView(pv);
|
|
|
|
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(3);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('helloNumberhelloStringhelloFunction');
|
|
|
|
|
|
|
|
component.numberCondition = 0;
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(1);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('helloString');
|
|
|
|
|
|
|
|
component.numberCondition = 1;
|
|
|
|
component.stringCondition = "bar";
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(1);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('helloNumber');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-23 03:27:36 -05:00
|
|
|
|
2014-12-17 04:01:08 -05:00
|
|
|
if (!IS_DARTIUM) {
|
|
|
|
it('should leave the element if the condition is a non-empty string (JS)', (done) => {
|
|
|
|
compileWithTemplate('<div><copy-me template="ng-if stringCondition">hello</copy-me></div>').then((pv) => {
|
|
|
|
createView(pv);
|
|
|
|
cd.detectChanges();
|
|
|
|
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(1);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should leave the element if the condition is an object (JS)', (done) => {
|
|
|
|
compileWithTemplate('<div><copy-me template="ng-if objectCondition">hello</copy-me></div>').then((pv) => {
|
|
|
|
createView(pv);
|
|
|
|
cd.detectChanges();
|
|
|
|
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(1);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove the element if the condition is null (JS)', (done) => {
|
|
|
|
compileWithTemplate('<div><copy-me template="ng-if nullCondition">hello</copy-me></div>').then((pv) => {
|
|
|
|
createView(pv);
|
|
|
|
cd.detectChanges();
|
|
|
|
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(0);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not add the element twice if the condition goes from true to true (JS)', (done) => {
|
|
|
|
compileWithTemplate('<div><copy-me template="ng-if numberCondition">hello</copy-me></div>').then((pv) => {
|
|
|
|
createView(pv);
|
|
|
|
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(1);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello');
|
|
|
|
|
|
|
|
component.numberCondition = 2;
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(1);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not recreate the element if the condition goes from true to true (JS)', (done) => {
|
|
|
|
compileWithTemplate('<div><copy-me template="ng-if numberCondition">hello</copy-me></div>').then((pv) => {
|
|
|
|
createView(pv);
|
|
|
|
|
|
|
|
cd.detectChanges();
|
|
|
|
DOM.addClass(view.nodes[0].childNodes[1], "foo");
|
|
|
|
|
|
|
|
component.numberCondition = 2;
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(DOM.hasClass(view.nodes[0].childNodes[1], "foo")).toBe(true);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
it('should not create the element if the condition is not a boolean (DART)', (done) => {
|
|
|
|
compileWithTemplate('<div><copy-me template="ng-if numberCondition">hello</copy-me></div>').then((pv) => {
|
|
|
|
createView(pv);
|
|
|
|
expect(function(){cd.detectChanges();}).toThrowError();
|
|
|
|
expect(view.nodes[0].querySelectorAll('copy-me').length).toEqual(0);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test-cmp',
|
|
|
|
template: new TemplateConfig({
|
|
|
|
inline: '', // each test swaps with a custom template.
|
|
|
|
directives: [NgIf]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
class TestComponent {
|
|
|
|
booleanCondition: boolean;
|
|
|
|
numberCondition: number;
|
|
|
|
stringCondition: string;
|
|
|
|
functionCondition: Function;
|
|
|
|
objectCondition: any;
|
|
|
|
nullCondition: any;
|
|
|
|
constructor() {
|
|
|
|
this.booleanCondition = true;
|
|
|
|
this.numberCondition = 1;
|
|
|
|
this.stringCondition = "foo";
|
|
|
|
this.functionCondition = function(s, n){
|
|
|
|
return s == "foo" && n == 1;
|
|
|
|
};
|
|
|
|
this.objectCondition = {};
|
|
|
|
this.nullCondition = null;
|
|
|
|
}
|
|
|
|
}
|