Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
Add additional tests from change_detection_integration_test.
Browse files Browse the repository at this point in the history
Started an additional `container_test.dart` for change detection that has more
to do with how change detection is propogated through containers and doesn't
fit the patterns in binding_test.dart.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=155661818
  • Loading branch information
matanlurey committed May 12, 2017
1 parent dce8d27 commit fad7626
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/core/change_detection/binding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ void main() {
await new _GetValue<TestChainedPropertyAccess>().runTest();
});

test('should support a function call', () async {
await new _GetValue<TestFunctionCall>().runTest();
});

test('should support assigning explicitly to null', () async {
await new _GetValue<TestAssignNull>().runTest();
});
Expand Down Expand Up @@ -510,6 +514,22 @@ class TestChainedPropertyAccess implements ValueTest {
get expected => isTrue;
}

@Component(
selector: 'test',
directives: const [ChildComponent],
template: r'''<child [value]="list.toList().length.isEven"></child>''',
)
class TestFunctionCall implements ValueTest {
@ViewChild(ChildComponent)
@override
ChildComponent child;

get list => const ['foo', 'bar'];

@override
get expected => isTrue;
}

@Component(
selector: 'test',
directives: const [ChildComponent],
Expand Down
136 changes: 136 additions & 0 deletions test/core/change_detection/container_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
@TestOn('browser')
import 'package:angular2/angular2.dart';
import 'package:angular_test/angular_test.dart';
import 'package:test/test.dart';

void main() {
tearDown(disposeAnyRunningTest);

test('should *not* assign any values if the initial value is null', () async {
final fixture = await new NgTestBed<BoundValueTest>().create();
await fixture.update(expectAsync1((comp) {
expect(comp.child.updates, 0, reason: 'No changes should have happened');
expect(comp.child.value, isNull);
}));
});

test('should propagate null if the initial value is non-null', () async {
final fixture = await new NgTestBed<BoundValueTest>().create(
beforeChangeDetection: (comp) => comp.boundValue = 'Hello',
);
await fixture.update(expectAsync1((comp) {
expect(comp.child.updates, 1, reason: 'One CD should have happened');
expect(comp.child.value, 'Hello');
comp.boundValue = null;
}));
await fixture.update(expectAsync1((comp) {
expect(comp.child.updates, 2, reason: 'Two CDs should have happened');
expect(comp.child.value, isNull);
}));
});

test('should not recreate literal maps unless content changes', () async {
Map boundMap;
final fixture = await new NgTestBed<BoundMapTest>().create(
beforeChangeDetection: (comp) {
comp.value = 'bar';
},
);
await fixture.update(expectAsync1((comp) {
boundMap = comp.child.value;
expect(boundMap, {'key': 'bar'});
}));
await fixture.update(expectAsync1((comp) {
expect(boundMap, same(comp.child.value), reason: 'Should be identical');
comp.value = 'foo';
}));
await fixture.update(expectAsync1((comp) {
expect(comp.child.value, {'key': 'foo'});
}));
});

test('should not recreate literal lists unless content changes', () async {
List boundList;
final fixture = await new NgTestBed<BoundListTest>().create(
beforeChangeDetection: (comp) {
comp.value = 'bar';
},
);
await fixture.update(expectAsync1((comp) {
boundList = comp.child.value;
expect(boundList, ['bar']);
}));
await fixture.update(expectAsync1((comp) {
expect(boundList, same(comp.child.value), reason: 'Should be identical');
comp.value = 'foo';
}));
await fixture.update(expectAsync1((comp) {
expect(comp.child.value, ['foo']);
}));
});

test('should support interpolation', () async {
final fixture = await new NgTestBed<BoundValueTest>().create(
beforeChangeDetection: (comp) => comp.boundValue = 'Hello World',
);
expect(fixture.text, 'Hello World');
});

test('should output empty for null values in interpolation', () async {
final fixture = await new NgTestBed<BoundValueTest>().create();
expect(fixture.text, isEmpty);
});
}

@Component(
selector: 'child',
template: r'{{value}}',
)
class ChildComponent {
var _value;
var updates = 0;

@Input()
set value(value) {
updates++;
_value = value;
}

get value => _value;
}

@Component(
selector: 'test',
directives: const [ChildComponent],
template: '<child [value]="boundValue"></child>',
)
class BoundValueTest {
var boundValue;

@ViewChild(ChildComponent)
ChildComponent child;
}

@Component(
selector: 'test',
directives: const [ChildComponent],
template: r'''<child [value]="{'key': value}"></child>''',
)
class BoundMapTest {
var value;

@ViewChild(ChildComponent)
ChildComponent child;
}

@Component(
selector: 'test',
directives: const [ChildComponent],
template: r'''<child [value]="[value]"></child>''',
)
class BoundListTest {
var value;

@ViewChild(ChildComponent)
ChildComponent child;
}

0 comments on commit fad7626

Please sign in to comment.