#test my component with jasmine

9 messages · Page 1 of 1 (latest)

half cove
#

Create a stub ActivatedRoute. Provide that stub route in the providers of your testing module. Create your component. Change the query params on your stub route. Check that your component's ID is the right one.

half cove
summer fox
#

i cannot add another extern library :/

safe briar
#

You can also trigger navigation in your test and pass the query params you want.

@Component({ template: `<router-outlet></router-outlet>` })
class TestComponent {}

describe("Your test", () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [TestComponent],
      imports: [
        RouterTestingModule.withRoutes({
          path: 'root',
          component: YourComponent
        })
      ]
    })
  })

  it("should navigate", fakeAsync(() => {
    const fixture = TestBed.createComponent(TestComponent)
    const router = TestBed.inject(Router)

    // navigate to "/root?id=1"
    router.navigate(['/root'], { queryParams: { id: 1 }})
    flush()

    // add your expectations here
  }))
})
summer fox
summer fox
half cove
#

Well, that's what the last line of your test does, doesn't it? What's the issue?

summer fox
#

When I run my test my date has not changed it is still today while in my parameter the date I put will change the date variable of my component