3

I have a test like

@Component(
  selector: 'my-test',
  template: 'none',
  changeDetection: ChangeDetectionStrategy.OnPush,
)
class MyTestComponent {
  final AngularClientCache clientCache;
  MyTestComponent(this.clientCache) {
    assert(clientCache != null);
  }
}

@Component(
  selector: 'test',
  directives: const <dynamic>[MyTestComponent],
  template: r'<my-test></my-test>',
)
class ComplexTestComponent {
  @ViewChild(MyTestComponent) MyTestComponent testComponent;
}

@AngularEntrypoint()
void main() {
  tearDown(() => disposeAnyRunningTest());

  group('AngularClientCache', () {
    test('should store and return value', () async {

      final testBed =
          new NgTestBed<ComplexTestComponent>().addProviders(<dynamic>[
        provide(AngularClientCache, useValue: new AngularClientCache())
      ]);
      final fixture = await testBed.create();

      // Create an instance of the in-browser page loader that uses our fixture.
      final loader = new HtmlPageLoader(
        fixture.rootElement.parent,
        executeSyncedFn: (c) async {
          await c();
          return fixture.update;
        },
        useShadowDom: false,
      );

      final complex =
          await loader.getInstance<ComplexTestComponent>(ComplexTestComponent);
      print(complex.testComponent); // <<<=== print `null`
    });
  });
}

How can I get a reference to the MyTestComponent component. I expected @ViewChild() to do it, but it doesn't. Any other approach is welcome as well.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567

1 Answers1

2

Matan Lurey explained in (https://github.com/dart-lang/angular_test/issues/50#issuecomment-291044761 broken)

resolvePageObject is only for getting a package:pageloader object.

What you're trying to do, I think:

await fixture.update((c) {
  expect(c.testComponent, isNotNull);
});
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • The link in your answer is not working. I am in similar situation where I need @viewChild reference/object to pass while doing unit test. so asked another question https://stackoverflow.com/questions/46003458/how-to-get-viewchiled-reference-object-in-unit-test-spec-in-angular – Aniruddha Das Sep 01 '17 at 15:25
  • Thanks for reporting. The link was to a PR and the seem to vanish when merged. This q+a is about Dart and will work entirely different in TS tests. – Günter Zöchbauer Sep 01 '17 at 15:34