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-10-13 03:29:13 -04:00
|
|
|
} from 'angular2/testing_internal';
|
2015-10-11 01:11:13 -04:00
|
|
|
import {provide} from 'angular2/src/core/di';
|
2015-09-08 12:57:51 -04:00
|
|
|
|
2015-11-06 20:34:07 -05:00
|
|
|
import {CONST_EXPR, stringify} from 'angular2/src/facade/lang';
|
|
|
|
import {MapWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {Promise} from 'angular2/src/facade/async';
|
2015-09-08 12:57:51 -04:00
|
|
|
|
2015-11-05 17:07:57 -05:00
|
|
|
import {ChangeDetectionCompiler} from 'angular2/src/compiler/change_detector_compiler';
|
2015-09-08 12:57:51 -04:00
|
|
|
|
|
|
|
import {
|
2015-09-18 13:33:23 -04:00
|
|
|
CompileDirectiveMetadata,
|
|
|
|
CompileTypeMetadata
|
2015-11-05 17:07:57 -05:00
|
|
|
} from 'angular2/src/compiler/directive_metadata';
|
2015-09-18 13:33:23 -04:00
|
|
|
import {
|
|
|
|
SourceModule,
|
|
|
|
SourceExpression,
|
|
|
|
SourceExpressions,
|
|
|
|
moduleRef
|
2015-11-05 17:07:57 -05:00
|
|
|
} from 'angular2/src/compiler/source_module';
|
2015-09-08 12:57:51 -04:00
|
|
|
|
2015-11-05 17:07:57 -05:00
|
|
|
import {TemplateParser} from 'angular2/src/compiler/template_parser';
|
2015-09-08 12:57:51 -04:00
|
|
|
|
|
|
|
import {
|
|
|
|
ChangeDetectorGenConfig,
|
|
|
|
ChangeDetectionStrategy,
|
|
|
|
ChangeDispatcher,
|
|
|
|
DirectiveIndex,
|
|
|
|
Locals,
|
|
|
|
BindingTarget,
|
|
|
|
ChangeDetector
|
|
|
|
} from 'angular2/src/core/change_detection/change_detection';
|
|
|
|
|
|
|
|
import {evalModule} from './eval_module';
|
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
import {TEST_PROVIDERS} from './test_bindings';
|
2015-10-01 13:07:49 -04:00
|
|
|
import {TestDispatcher, TestPipes} from './change_detector_mocks';
|
2015-11-05 17:07:57 -05:00
|
|
|
import {codeGenValueFn, codeGenExportVariable, MODULE_SUFFIX} from 'angular2/src/compiler/util';
|
2015-09-08 12:57:51 -04:00
|
|
|
|
|
|
|
// Attention: These module names have to correspond to real modules!
|
2015-11-05 17:07:57 -05:00
|
|
|
var THIS_MODULE_ID = 'angular2/test/compiler/change_detector_compiler_spec';
|
2015-10-01 13:07:49 -04:00
|
|
|
var THIS_MODULE_URL = `package:${THIS_MODULE_ID}${MODULE_SUFFIX}`;
|
|
|
|
var THIS_MODULE_REF = moduleRef(THIS_MODULE_URL);
|
2015-09-08 12:57:51 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('ChangeDetectorCompiler', () => {
|
2015-10-11 01:11:13 -04:00
|
|
|
beforeEachBindings(() => TEST_PROVIDERS);
|
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-18 13:33:23 -04:00
|
|
|
directives: CompileDirectiveMetadata[] = CONST_EXPR([])): string[] {
|
2015-10-01 13:07:49 -04:00
|
|
|
var type =
|
|
|
|
new CompileTypeMetadata({name: stringify(SomeComponent), moduleUrl: THIS_MODULE_URL});
|
2015-09-14 18:59:09 -04:00
|
|
|
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(() => [
|
2015-10-11 01:11:13 -04:00
|
|
|
provide(ChangeDetectorGenConfig,
|
2015-10-28 13:06:53 -04:00
|
|
|
{useValue: new ChangeDetectorGenConfig(true, false, false)})
|
2015-09-14 18:59:09 -04:00
|
|
|
]);
|
|
|
|
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(() => [
|
2015-10-11 01:11:13 -04:00
|
|
|
provide(ChangeDetectorGenConfig,
|
2015-10-28 13:06:53 -04:00
|
|
|
{useValue: new ChangeDetectorGenConfig(true, false, true)})
|
2015-09-14 18:59:09 -04:00
|
|
|
]);
|
|
|
|
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', () => {
|
2015-10-28 03:59:19 -04:00
|
|
|
function detectChanges(
|
|
|
|
compiler: ChangeDetectionCompiler, template: string,
|
|
|
|
directives: CompileDirectiveMetadata[] = CONST_EXPR([])): Promise<string[]> {
|
2015-10-01 13:07:49 -04:00
|
|
|
var type =
|
|
|
|
new CompileTypeMetadata({name: stringify(SomeComponent), moduleUrl: THIS_MODULE_URL});
|
2015-09-14 18:59:09 -04:00
|
|
|
var parsedTemplate = parser.parse(template, directives, 'TestComp');
|
2015-09-18 13:33:23 -04:00
|
|
|
var sourceExpressions =
|
2015-09-08 12:57:51 -04:00
|
|
|
compiler.compileComponentCodeGen(type, ChangeDetectionStrategy.Default, parsedTemplate);
|
2015-09-18 13:33:23 -04:00
|
|
|
var testableModule = createTestableModule(sourceExpressions, 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-10-28 03:59:19 -04:00
|
|
|
function createTestableModule(source: SourceExpressions,
|
|
|
|
changeDetectorIndex: number): SourceModule {
|
2015-09-14 18:59:09 -04:00
|
|
|
var resultExpression =
|
2015-09-18 13:33:23 -04:00
|
|
|
`${THIS_MODULE_REF}testChangeDetector(([${source.expressions.join(',')}])[${changeDetectorIndex}])`;
|
2015-09-14 18:59:09 -04:00
|
|
|
var testableSource = `${source.declarations.join('\n')}
|
2015-11-02 11:39:14 -05:00
|
|
|
${codeGenValueFn(['_'], resultExpression, '_run')};
|
|
|
|
${codeGenExportVariable('run')}_run;`;
|
2015-09-14 18:59:09 -04:00
|
|
|
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);
|
2015-10-01 13:07:49 -04:00
|
|
|
var ctx = new SomeComponent();
|
2015-09-08 12:57:51 -04:00
|
|
|
ctx.someProp = 'someValue';
|
|
|
|
var locals = new Locals(null, MapWrapper.createFromStringMap({'someVar': null}));
|
|
|
|
cd.hydrate(ctx, locals, dispatcher, new TestPipes());
|
|
|
|
cd.detectChanges();
|
|
|
|
return dispatcher.log;
|
|
|
|
}
|
2015-10-01 13:07:49 -04:00
|
|
|
|
|
|
|
class SomeComponent {
|
|
|
|
someProp: string;
|
2015-10-02 10:57:29 -04:00
|
|
|
}
|