2015-09-08 12:57:51 -04:00
|
|
|
import {
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
xdescribe,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
xit,
|
|
|
|
expect,
|
|
|
|
beforeEach,
|
|
|
|
afterEach,
|
|
|
|
AsyncTestCompleter,
|
2015-09-14 18:59:09 -04:00
|
|
|
inject,
|
|
|
|
beforeEachBindings
|
2015-09-08 12:57:51 -04:00
|
|
|
} from 'angular2/test_lib';
|
2015-09-14 18:59:09 -04:00
|
|
|
import {bind} from 'angular2/src/core/di';
|
2015-09-08 12:57:51 -04:00
|
|
|
|
|
|
|
import {CONST_EXPR} from 'angular2/src/core/facade/lang';
|
|
|
|
import {MapWrapper} from 'angular2/src/core/facade/collection';
|
|
|
|
import {Promise} from 'angular2/src/core/facade/async';
|
|
|
|
|
|
|
|
import {ChangeDetectionCompiler} from 'angular2/src/compiler/change_detector_compiler';
|
|
|
|
|
|
|
|
import {
|
2015-09-14 18:59:09 -04:00
|
|
|
NormalizedDirectiveMetadata,
|
2015-09-08 12:57:51 -04:00
|
|
|
TypeMetadata,
|
2015-09-14 18:59:09 -04:00
|
|
|
ChangeDetectionMetadata
|
|
|
|
} from 'angular2/src/compiler/directive_metadata';
|
|
|
|
import {SourceModule, SourceExpression, moduleRef} from 'angular2/src/compiler/source_module';
|
2015-09-08 12:57:51 -04:00
|
|
|
|
|
|
|
import {TemplateParser} from 'angular2/src/compiler/template_parser';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ChangeDetectorGenConfig,
|
|
|
|
ChangeDetectionStrategy,
|
|
|
|
ChangeDispatcher,
|
|
|
|
DirectiveIndex,
|
|
|
|
Locals,
|
|
|
|
BindingTarget,
|
|
|
|
ChangeDetector
|
|
|
|
} from 'angular2/src/core/change_detection/change_detection';
|
|
|
|
|
|
|
|
import {evalModule} from './eval_module';
|
|
|
|
|
2015-09-14 18:59:09 -04:00
|
|
|
import {TEST_BINDINGS} from './test_bindings';
|
2015-09-08 12:57:51 -04:00
|
|
|
import {TestContext, TestDispatcher, TestPipes} from './change_detector_mocks';
|
2015-09-14 18:59:09 -04:00
|
|
|
import {codeGenValueFn, codeGenExportVariable} from 'angular2/src/compiler/util';
|
2015-09-08 12:57:51 -04:00
|
|
|
|
|
|
|
// Attention: These module names have to correspond to real modules!
|
2015-09-14 18:59:09 -04:00
|
|
|
const THIS_MODULE = 'angular2/test/compiler/change_detector_compiler_spec';
|
|
|
|
var THIS_MODULE_REF = moduleRef(THIS_MODULE);
|
2015-09-08 12:57:51 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('ChangeDetectorCompiler', () => {
|
2015-09-14 18:59:09 -04:00
|
|
|
beforeEachBindings(() => TEST_BINDINGS);
|
2015-09-08 12:57:51 -04:00
|
|
|
|
2015-09-14 18:59:09 -04:00
|
|
|
var parser: TemplateParser;
|
|
|
|
var compiler: ChangeDetectionCompiler;
|
2015-09-08 12:57:51 -04:00
|
|
|
|
2015-09-14 18:59:09 -04:00
|
|
|
beforeEach(inject([TemplateParser, ChangeDetectionCompiler], (_parser, _compiler) => {
|
|
|
|
parser = _parser;
|
|
|
|
compiler = _compiler;
|
|
|
|
}));
|
2015-09-08 12:57:51 -04:00
|
|
|
|
|
|
|
describe('compileComponentRuntime', () => {
|
|
|
|
function detectChanges(compiler: ChangeDetectionCompiler, template: string,
|
2015-09-14 18:59:09 -04:00
|
|
|
directives: NormalizedDirectiveMetadata[] = CONST_EXPR([])): string[] {
|
|
|
|
var type = new TypeMetadata({name: 'SomeComp'});
|
|
|
|
var parsedTemplate = parser.parse(template, directives, 'TestComp');
|
2015-09-08 12:57:51 -04:00
|
|
|
var factories =
|
|
|
|
compiler.compileComponentRuntime(type, ChangeDetectionStrategy.Default, parsedTemplate);
|
|
|
|
return testChangeDetector(factories[0]);
|
|
|
|
}
|
|
|
|
|
2015-09-14 18:59:09 -04:00
|
|
|
describe('no jit', () => {
|
|
|
|
beforeEachBindings(() => [
|
|
|
|
bind(ChangeDetectorGenConfig)
|
|
|
|
.toValue(new ChangeDetectorGenConfig(true, true, false, false))
|
|
|
|
]);
|
|
|
|
it('should watch element properties', () => {
|
|
|
|
expect(detectChanges(compiler, '<div [el-prop]="someProp">'))
|
|
|
|
.toEqual(['elementProperty(elProp)=someValue']);
|
|
|
|
});
|
2015-09-08 12:57:51 -04:00
|
|
|
});
|
|
|
|
|
2015-09-14 18:59:09 -04:00
|
|
|
describe('jit', () => {
|
|
|
|
beforeEachBindings(() => [
|
|
|
|
bind(ChangeDetectorGenConfig)
|
|
|
|
.toValue(new ChangeDetectorGenConfig(true, true, false, true))
|
|
|
|
]);
|
|
|
|
it('should watch element properties', () => {
|
|
|
|
expect(detectChanges(compiler, '<div [el-prop]="someProp">'))
|
|
|
|
.toEqual(['elementProperty(elProp)=someValue']);
|
|
|
|
});
|
|
|
|
|
2015-09-08 12:57:51 -04:00
|
|
|
});
|
2015-09-14 18:59:09 -04:00
|
|
|
|
|
|
|
|
2015-09-08 12:57:51 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('compileComponentCodeGen', () => {
|
|
|
|
function detectChanges(compiler: ChangeDetectionCompiler, template: string,
|
2015-09-14 18:59:09 -04:00
|
|
|
directives: NormalizedDirectiveMetadata[] = CONST_EXPR([])):
|
|
|
|
Promise<string[]> {
|
|
|
|
var type = new TypeMetadata({name: 'SomeComp'});
|
|
|
|
var parsedTemplate = parser.parse(template, directives, 'TestComp');
|
|
|
|
var sourceExpression =
|
2015-09-08 12:57:51 -04:00
|
|
|
compiler.compileComponentCodeGen(type, ChangeDetectionStrategy.Default, parsedTemplate);
|
2015-09-14 18:59:09 -04:00
|
|
|
var testableModule = createTestableModule(sourceExpression, 0).getSourceWithImports();
|
2015-09-08 12:57:51 -04:00
|
|
|
return evalModule(testableModule.source, testableModule.imports, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should watch element properties', inject([AsyncTestCompleter], (async) => {
|
2015-09-14 18:59:09 -04:00
|
|
|
detectChanges(compiler, '<div [el-prop]="someProp">')
|
2015-09-08 12:57:51 -04:00
|
|
|
.then((value) => {
|
|
|
|
expect(value).toEqual(['elementProperty(elProp)=someValue']);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-14 18:59:09 -04:00
|
|
|
function createTestableModule(source: SourceExpression, changeDetectorIndex: number): SourceModule {
|
|
|
|
var resultExpression =
|
|
|
|
`${THIS_MODULE_REF}testChangeDetector((${source.expression})[${changeDetectorIndex}])`;
|
|
|
|
var testableSource = `${source.declarations.join('\n')}
|
|
|
|
${codeGenExportVariable('run')}${codeGenValueFn(['_'], resultExpression)};`;
|
|
|
|
return new SourceModule(null, testableSource);
|
2015-09-08 12:57:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function testChangeDetector(changeDetectorFactory: Function): string[] {
|
|
|
|
var dispatcher = new TestDispatcher([], []);
|
|
|
|
var cd = changeDetectorFactory(dispatcher);
|
|
|
|
var ctx = new TestContext();
|
|
|
|
ctx.someProp = 'someValue';
|
|
|
|
var locals = new Locals(null, MapWrapper.createFromStringMap({'someVar': null}));
|
|
|
|
cd.hydrate(ctx, locals, dispatcher, new TestPipes());
|
|
|
|
cd.detectChanges();
|
|
|
|
return dispatcher.log;
|
|
|
|
}
|