2015-03-13 06:10:11 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
IS_DARTIUM,
|
|
|
|
it,
|
|
|
|
xit,
|
|
|
|
} from 'angular2/test_lib';
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-02-27 17:50:06 -05:00
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-02-05 16:08:05 -05:00
|
|
|
import {Injector} from 'angular2/di';
|
|
|
|
import {Lexer, Parser, ChangeDetector, dynamicChangeDetection} from 'angular2/change_detection';
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-02-05 16:08:05 -05:00
|
|
|
import {Compiler, CompilerCache} from 'angular2/src/core/compiler/compiler';
|
|
|
|
import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader';
|
2015-01-30 03:43:21 -05:00
|
|
|
import {NativeShadowDomStrategy} from 'angular2/src/core/compiler/shadow_dom_strategy';
|
2015-02-12 08:44:59 -05:00
|
|
|
import {TemplateLoader} from 'angular2/src/core/compiler/template_loader';
|
2015-02-24 10:05:45 -05:00
|
|
|
import {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mapper';
|
|
|
|
import {UrlResolver} from 'angular2/src/core/compiler/url_resolver';
|
|
|
|
import {StyleUrlResolver} from 'angular2/src/core/compiler/style_url_resolver';
|
2015-03-02 09:02:48 -05:00
|
|
|
import {CssProcessor} from 'angular2/src/core/compiler/css_processor';
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-02-05 16:08:05 -05:00
|
|
|
import {Component} from 'angular2/src/core/annotations/annotations';
|
2015-02-12 08:44:59 -05:00
|
|
|
import {Template} from 'angular2/src/core/annotations/template';
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-02-24 09:32:44 -05:00
|
|
|
import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock';
|
|
|
|
|
2015-02-04 16:27:31 -05:00
|
|
|
import {If} from 'angular2/src/directives/if';
|
2014-12-17 04:01:08 -05:00
|
|
|
|
|
|
|
export function main() {
|
2015-02-04 16:27:31 -05:00
|
|
|
describe('if directive', () => {
|
2015-02-12 08:44:59 -05:00
|
|
|
var view, cd, compiler, component, tplResolver;
|
|
|
|
|
2014-12-17 04:01:08 -05:00
|
|
|
beforeEach(() => {
|
2015-02-24 10:05:45 -05:00
|
|
|
var urlResolver = new UrlResolver();
|
2015-02-24 09:32:44 -05:00
|
|
|
tplResolver = new MockTemplateResolver();
|
2015-02-24 10:05:45 -05:00
|
|
|
compiler = new Compiler(
|
|
|
|
dynamicChangeDetection,
|
|
|
|
new TemplateLoader(null, null),
|
|
|
|
new DirectiveMetadataReader(),
|
|
|
|
new Parser(new Lexer()),
|
|
|
|
new CompilerCache(),
|
|
|
|
new NativeShadowDomStrategy(new StyleUrlResolver(urlResolver)),
|
|
|
|
tplResolver,
|
|
|
|
new ComponentUrlMapper(),
|
2015-03-02 09:02:48 -05:00
|
|
|
urlResolver,
|
2015-03-03 05:32:19 -05:00
|
|
|
new CssProcessor(null)
|
2015-03-02 09:02:48 -05:00
|
|
|
);
|
2014-12-17 04:01:08 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
function createView(pv) {
|
|
|
|
component = new TestComponent();
|
2015-03-11 14:57:21 -04:00
|
|
|
view = pv.instantiate(null, null);
|
2015-03-12 00:11:39 -04:00
|
|
|
view.hydrate(new Injector([]), null, null, component, null);
|
2014-12-17 04:01:08 -05:00
|
|
|
cd = view.changeDetector;
|
|
|
|
}
|
|
|
|
|
2015-02-12 08:44:59 -05:00
|
|
|
function compileWithTemplate(html) {
|
|
|
|
var template = new Template({
|
|
|
|
inline: html,
|
|
|
|
directives: [If]
|
|
|
|
});
|
|
|
|
tplResolver.setTemplate(TestComponent, template);
|
|
|
|
return compiler.compile(TestComponent);
|
2014-12-17 04:01:08 -05:00
|
|
|
}
|
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
it('should work in a template attribute', inject([AsyncTestCompleter], (async) => {
|
2015-02-04 16:27:31 -05:00
|
|
|
compileWithTemplate('<div><copy-me template="if booleanCondition">hello</copy-me></div>').then((pv) => {
|
2014-12-17 04:01:08 -05:00
|
|
|
createView(pv);
|
|
|
|
cd.detectChanges();
|
|
|
|
|
2015-03-09 12:41:49 -04:00
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(1);
|
2014-12-17 04:01:08 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-17 04:01:08 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
it('should work in a template element', inject([AsyncTestCompleter], (async) => {
|
2015-02-04 16:27:31 -05:00
|
|
|
compileWithTemplate('<div><template [if]="booleanCondition"><copy-me>hello2</copy-me></template></div>').then((pv) => {
|
2014-12-17 04:01:08 -05:00
|
|
|
createView(pv);
|
|
|
|
cd.detectChanges();
|
|
|
|
|
2015-03-09 12:41:49 -04:00
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(1);
|
2014-12-17 04:01:08 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello2');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-17 04:01:08 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
it('should toggle node when condition changes', inject([AsyncTestCompleter], (async) => {
|
2015-02-04 16:27:31 -05:00
|
|
|
compileWithTemplate('<div><copy-me template="if booleanCondition">hello</copy-me></div>').then((pv) => {
|
2014-12-17 04:01:08 -05:00
|
|
|
createView(pv);
|
2015-01-23 03:27:36 -05:00
|
|
|
|
2014-12-17 04:01:08 -05:00
|
|
|
component.booleanCondition = false;
|
|
|
|
cd.detectChanges();
|
2015-03-09 12:41:49 -04:00
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(0);
|
2014-12-17 04:01:08 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('');
|
|
|
|
|
|
|
|
|
|
|
|
component.booleanCondition = true;
|
|
|
|
cd.detectChanges();
|
2015-03-09 12:41:49 -04:00
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(1);
|
2014-12-17 04:01:08 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello');
|
|
|
|
|
|
|
|
component.booleanCondition = false;
|
|
|
|
cd.detectChanges();
|
2015-03-09 12:41:49 -04:00
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(0);
|
2014-12-17 04:01:08 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('');
|
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-17 04:01:08 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-03-19 05:04:42 -04:00
|
|
|
it('should handle nested if correctly', inject([AsyncTestCompleter], (async) => {
|
|
|
|
compileWithTemplate('<div><template [if]="booleanCondition"><copy-me *if="nestedBooleanCondition">hello</copy-me></template></div>').then((pv) => {
|
|
|
|
createView(pv);
|
|
|
|
|
|
|
|
component.booleanCondition = false;
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(0);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('');
|
|
|
|
|
|
|
|
component.booleanCondition = true;
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(1);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello');
|
|
|
|
|
|
|
|
component.nestedBooleanCondition = false;
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(0);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('');
|
|
|
|
|
|
|
|
component.nestedBooleanCondition = true;
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(1);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello');
|
|
|
|
|
|
|
|
component.booleanCondition = false;
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(0);
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
it('should update several nodes with if', inject([AsyncTestCompleter], (async) => {
|
2014-12-17 04:01:08 -05:00
|
|
|
var templateString =
|
|
|
|
'<div>' +
|
2015-02-04 16:27:31 -05:00
|
|
|
'<copy-me template="if numberCondition + 1 >= 2">helloNumber</copy-me>' +
|
|
|
|
'<copy-me template="if stringCondition == \'foo\'">helloString</copy-me>' +
|
|
|
|
'<copy-me template="if functionCondition(stringCondition, numberCondition)">helloFunction</copy-me>' +
|
2014-12-17 04:01:08 -05:00
|
|
|
'</div>';
|
|
|
|
compileWithTemplate(templateString).then((pv) => {
|
|
|
|
createView(pv);
|
|
|
|
|
|
|
|
cd.detectChanges();
|
2015-03-09 12:41:49 -04:00
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(3);
|
2014-12-17 04:01:08 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('helloNumberhelloStringhelloFunction');
|
|
|
|
|
|
|
|
component.numberCondition = 0;
|
|
|
|
cd.detectChanges();
|
2015-03-09 12:41:49 -04:00
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(1);
|
2014-12-17 04:01:08 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('helloString');
|
|
|
|
|
|
|
|
component.numberCondition = 1;
|
|
|
|
component.stringCondition = "bar";
|
|
|
|
cd.detectChanges();
|
2015-03-09 12:41:49 -04:00
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(1);
|
2014-12-17 04:01:08 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('helloNumber');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-17 04:01:08 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-01-23 03:27:36 -05:00
|
|
|
|
2015-03-19 09:21:45 -04:00
|
|
|
if (!IS_DARTIUM) {;
|
2015-03-13 06:10:11 -04:00
|
|
|
it('should not add the element twice if the condition goes from true to true (JS)', inject([AsyncTestCompleter], (async) => {
|
2015-02-04 16:27:31 -05:00
|
|
|
compileWithTemplate('<div><copy-me template="if numberCondition">hello</copy-me></div>').then((pv) => {
|
2014-12-17 04:01:08 -05:00
|
|
|
createView(pv);
|
|
|
|
|
|
|
|
cd.detectChanges();
|
2015-03-09 12:41:49 -04:00
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(1);
|
2014-12-17 04:01:08 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello');
|
|
|
|
|
|
|
|
component.numberCondition = 2;
|
|
|
|
cd.detectChanges();
|
2015-03-09 12:41:49 -04:00
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(1);
|
2014-12-17 04:01:08 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('hello');
|
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-17 04:01:08 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-17 04:01:08 -05:00
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
it('should not recreate the element if the condition goes from true to true (JS)', inject([AsyncTestCompleter], (async) => {
|
2015-02-04 16:27:31 -05:00
|
|
|
compileWithTemplate('<div><copy-me template="if numberCondition">hello</copy-me></div>').then((pv) => {
|
2014-12-17 04:01:08 -05:00
|
|
|
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);
|
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-17 04:01:08 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-17 04:01:08 -05:00
|
|
|
} else {
|
2015-03-13 06:10:11 -04:00
|
|
|
it('should not create the element if the condition is not a boolean (DART)', inject([AsyncTestCompleter], (async) => {
|
2015-02-04 16:27:31 -05:00
|
|
|
compileWithTemplate('<div><copy-me template="if numberCondition">hello</copy-me></div>').then((pv) => {
|
2014-12-17 04:01:08 -05:00
|
|
|
createView(pv);
|
|
|
|
expect(function(){cd.detectChanges();}).toThrowError();
|
2015-03-09 12:41:49 -04:00
|
|
|
expect(DOM.querySelectorAll(view.nodes[0], 'copy-me').length).toEqual(0);
|
2014-12-17 04:01:08 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-17 04:01:08 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-17 04:01:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-12 08:44:59 -05:00
|
|
|
@Component({selector: 'test-cmp'})
|
2014-12-17 04:01:08 -05:00
|
|
|
class TestComponent {
|
|
|
|
booleanCondition: boolean;
|
2015-03-19 05:04:42 -04:00
|
|
|
nestedBooleanCondition: boolean;
|
2014-12-17 04:01:08 -05:00
|
|
|
numberCondition: number;
|
|
|
|
stringCondition: string;
|
|
|
|
functionCondition: Function;
|
|
|
|
constructor() {
|
|
|
|
this.booleanCondition = true;
|
2015-03-19 05:04:42 -04:00
|
|
|
this.nestedBooleanCondition = true;
|
2014-12-17 04:01:08 -05:00
|
|
|
this.numberCondition = 1;
|
|
|
|
this.stringCondition = "foo";
|
|
|
|
this.functionCondition = function(s, n){
|
|
|
|
return s == "foo" && n == 1;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|