GithubHelp home page GithubHelp logo

Comments (4)

jnizet avatar jnizet commented on June 7, 2024

Your test doesn't call fixture.detectChanges() before starting to interact with the component. So its lifecycle is never started (ngOnInit isn't called).

from angular.

JeanMeche avatar JeanMeche commented on June 7, 2024

Per above.

from angular.

adorogensky avatar adorogensky commented on June 7, 2024

Your test doesn't call fixture.detectChanges() before starting to interact with the component. So its lifecycle is never started (ngOnInit isn't called).

ok.. it does work when I move the change detection above dispatchEvent. But why? Below is an example from angular.io

it('should convert hero name to Title Case', () => {
  // get the name's input and display elements from the DOM
  const hostElement: HTMLElement = harness.routeNativeElement!;
  const nameInput: HTMLInputElement = hostElement.querySelector('input')!;
  const nameDisplay: HTMLElement = hostElement.querySelector('span')!;

  // simulate user entering a new name into the input box
  nameInput.value = 'quick BROWN  fOx';

  // Dispatch a DOM event so that Angular learns of input value change.
  nameInput.dispatchEvent(new Event('input'));

  // Tell Angular to update the display binding through the title pipe
  harness.detectChanges();

  expect(nameDisplay.textContent).toBe('Quick Brown  Fox');
});

from angular.

JeanMeche avatar JeanMeche commented on June 7, 2024

detectChanges is responsible for invoking the lifecycle hooks (ngOnInit) in your case.
If you fire the event before the subscription starts, you won't have any changes reported.

I agree the doc isn't great, as it doesn't show that the test file actually invokes detectChanges when creating the component.

harness = await RouterTestingHarness.create();
component = await harness.navigateByUrl(`/heroes/${id}`, HeroDetailComponent);
page = new Page();
const request = TestBed.inject(HttpTestingController).expectOne(`api/heroes/?id=${id}`);
const hero = getTestHeroes().find((h) => h.id === Number(id));
request.flush(hero ? [hero] : []);
harness.detectChanges();

from angular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.