2015-09-01 19:24:23 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xit,
|
2015-09-14 18:59:09 -04:00
|
|
|
TestComponentBuilder,
|
|
|
|
beforeEachBindings
|
2015-09-01 19:24:23 -04:00
|
|
|
} from 'angular2/test_lib';
|
|
|
|
import {MapWrapper} from 'angular2/src/core/facade/collection';
|
2015-09-14 18:59:09 -04:00
|
|
|
import {
|
|
|
|
NormalizedDirectiveMetadata,
|
|
|
|
TypeMetadata,
|
|
|
|
ChangeDetectionMetadata
|
|
|
|
} from 'angular2/src/compiler/directive_metadata';
|
2015-09-01 19:24:23 -04:00
|
|
|
import {TemplateParser} from 'angular2/src/compiler/template_parser';
|
|
|
|
import {
|
|
|
|
Parser,
|
|
|
|
Lexer,
|
|
|
|
ChangeDetectorDefinition,
|
|
|
|
ChangeDetectorGenConfig,
|
|
|
|
DynamicProtoChangeDetector,
|
|
|
|
ChangeDetectionStrategy,
|
|
|
|
ChangeDispatcher,
|
|
|
|
DirectiveIndex,
|
|
|
|
Locals,
|
|
|
|
BindingTarget,
|
|
|
|
ChangeDetector
|
|
|
|
} from 'angular2/src/core/change_detection/change_detection';
|
|
|
|
import {Pipes} from 'angular2/src/core/change_detection/pipes';
|
|
|
|
import {createChangeDetectorDefinitions} from 'angular2/src/compiler/change_definition_factory';
|
2015-09-08 12:57:51 -04:00
|
|
|
import {TestContext, TestDirective, TestDispatcher, TestPipes} from './change_detector_mocks';
|
2015-09-01 19:24:23 -04:00
|
|
|
|
2015-09-14 18:59:09 -04:00
|
|
|
import {TEST_BINDINGS} from './test_bindings';
|
|
|
|
|
2015-09-01 19:24:23 -04:00
|
|
|
export function main() {
|
|
|
|
describe('ChangeDefinitionFactory', () => {
|
2015-09-14 18:59:09 -04:00
|
|
|
beforeEachBindings(() => TEST_BINDINGS);
|
|
|
|
|
2015-09-01 19:24:23 -04:00
|
|
|
var parser: TemplateParser;
|
|
|
|
var dispatcher: TestDispatcher;
|
|
|
|
var context: TestContext;
|
|
|
|
var directive: TestDirective;
|
|
|
|
var locals: Locals;
|
|
|
|
var pipes: Pipes;
|
|
|
|
var eventLocals: Locals;
|
|
|
|
|
2015-09-14 18:59:09 -04:00
|
|
|
beforeEach(inject([TemplateParser], (_templateParser) => {
|
|
|
|
parser = _templateParser;
|
2015-09-01 19:24:23 -04:00
|
|
|
context = new TestContext();
|
|
|
|
directive = new TestDirective();
|
|
|
|
dispatcher = new TestDispatcher([directive], []);
|
|
|
|
locals = new Locals(null, MapWrapper.createFromStringMap({'someVar': null}));
|
|
|
|
eventLocals = new Locals(null, MapWrapper.createFromStringMap({'$event': null}));
|
|
|
|
pipes = new TestPipes();
|
2015-09-14 18:59:09 -04:00
|
|
|
}));
|
2015-09-01 19:24:23 -04:00
|
|
|
|
2015-09-14 18:59:09 -04:00
|
|
|
function createChangeDetector(template: string, directives: NormalizedDirectiveMetadata[],
|
2015-09-01 19:24:23 -04:00
|
|
|
protoViewIndex: number = 0): ChangeDetector {
|
|
|
|
var protoChangeDetectors =
|
2015-09-14 18:59:09 -04:00
|
|
|
createChangeDetectorDefinitions(new TypeMetadata({name: 'SomeComp'}),
|
|
|
|
ChangeDetectionStrategy.Default,
|
|
|
|
new ChangeDetectorGenConfig(true, true, false, false),
|
|
|
|
parser.parse(template, directives, 'TestComp'))
|
2015-09-01 19:24:23 -04:00
|
|
|
.map(definition => new DynamicProtoChangeDetector(definition));
|
|
|
|
var changeDetector = protoChangeDetectors[protoViewIndex].instantiate(dispatcher);
|
|
|
|
changeDetector.hydrate(context, locals, dispatcher, pipes);
|
|
|
|
return changeDetector;
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should watch element properties', () => {
|
|
|
|
var changeDetector = createChangeDetector('<div [el-prop]="someProp">', [], 0);
|
|
|
|
|
|
|
|
context.someProp = 'someValue';
|
|
|
|
changeDetector.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual(['elementProperty(elProp)=someValue']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should watch text nodes', () => {
|
|
|
|
var changeDetector = createChangeDetector('{{someProp}}', [], 0);
|
|
|
|
|
|
|
|
context.someProp = 'someValue';
|
|
|
|
changeDetector.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual(['textNode(null)=someValue']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle events', () => {
|
|
|
|
var changeDetector = createChangeDetector('<div on-click="onEvent($event)">', [], 0);
|
|
|
|
|
|
|
|
eventLocals.set('$event', 'click');
|
|
|
|
changeDetector.handleEvent('click', 0, eventLocals);
|
|
|
|
expect(context.eventLog).toEqual(['click']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should watch variables', () => {
|
|
|
|
var changeDetector = createChangeDetector('<div #some-var [el-prop]="someVar">', [], 0);
|
|
|
|
|
|
|
|
locals.set('someVar', 'someValue');
|
|
|
|
changeDetector.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual(['elementProperty(elProp)=someValue']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should write directive properties', () => {
|
2015-09-14 18:59:09 -04:00
|
|
|
var dirMeta = new NormalizedDirectiveMetadata({
|
|
|
|
type: new TypeMetadata({name: 'SomeDir'}),
|
2015-09-01 19:24:23 -04:00
|
|
|
selector: 'div',
|
|
|
|
changeDetection: new ChangeDetectionMetadata({properties: ['dirProp']})
|
|
|
|
});
|
|
|
|
|
|
|
|
var changeDetector = createChangeDetector('<div [dir-prop]="someProp">', [dirMeta], 0);
|
|
|
|
|
|
|
|
context.someProp = 'someValue';
|
|
|
|
changeDetector.detectChanges();
|
|
|
|
expect(directive.dirProp).toEqual('someValue');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should watch directive host properties', () => {
|
2015-09-14 18:59:09 -04:00
|
|
|
var dirMeta = new NormalizedDirectiveMetadata({
|
|
|
|
type: new TypeMetadata({name: 'SomeDir'}),
|
2015-09-01 19:24:23 -04:00
|
|
|
selector: 'div',
|
|
|
|
changeDetection: new ChangeDetectionMetadata({hostProperties: {'elProp': 'dirProp'}})
|
|
|
|
});
|
|
|
|
|
|
|
|
var changeDetector = createChangeDetector('<div>', [dirMeta], 0);
|
|
|
|
|
|
|
|
directive.dirProp = 'someValue';
|
|
|
|
changeDetector.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual(['elementProperty(elProp)=someValue']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle directive events', () => {
|
2015-09-14 18:59:09 -04:00
|
|
|
var dirMeta = new NormalizedDirectiveMetadata({
|
|
|
|
type: new TypeMetadata({name: 'SomeDir'}),
|
2015-09-01 19:24:23 -04:00
|
|
|
selector: 'div',
|
|
|
|
changeDetection:
|
|
|
|
new ChangeDetectionMetadata({hostListeners: {'click': 'onEvent($event)'}})
|
|
|
|
});
|
|
|
|
|
|
|
|
var changeDetector = createChangeDetector('<div>', [dirMeta], 0);
|
|
|
|
|
|
|
|
eventLocals.set('$event', 'click');
|
|
|
|
changeDetector.handleEvent('click', 0, eventLocals);
|
|
|
|
expect(directive.eventLog).toEqual(['click']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create change detectors for embedded templates', () => {
|
|
|
|
var changeDetector = createChangeDetector('<template>{{someProp}}<template>', [], 1);
|
|
|
|
|
|
|
|
context.someProp = 'someValue';
|
|
|
|
changeDetector.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual(['textNode(null)=someValue']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should watch expressions after embedded templates', () => {
|
|
|
|
var changeDetector =
|
|
|
|
createChangeDetector('<template>{{someProp2}}</template>{{someProp}}', [], 0);
|
|
|
|
|
|
|
|
context.someProp = 'someValue';
|
|
|
|
changeDetector.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual(['textNode(null)=someValue']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|