Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LayZeeDK/0ec8fd8f3499f019bf85c07f5a3259fd to your computer and use it in GitHub Desktop.
Save LayZeeDK/0ec8fd8f3499f019bf85c07f5a3259fd to your computer and use it in GitHub Desktop.
Spectacular 15 Feature testing API design
// Angular Testing Library + Spectacular 0.5
const {
fixture: {
debugElement: { injector },
},
} = await render(SpectacularAppComponent, {
excludeComponentDeclaration: true,
imports: [
SpectacularFeatureTestingModule.withFeature({
featureModule: HeroesModule,
featurePath: 'heroes',
}),
// does this work?
RouterTestingModule.withRoutes([], {
onSameUrlNavigation: 'reload',
}),
],
});
const router = injector.get(SpectacularFeatureRouter);
const location = injector.get(SpectacularFeatureLocation);
await router.navigateByUrl('~/');
expect(location.path()).toBe('~/');
// Angular Testing Library + Spectacular >=15
const {
fixture: {
debugElement: { injector },
},
} = await render(TestAppComponent, {
routes: [{ path: 'heroes', loadChildren: () => HeroesModule }],
providers: [
provideSpectacularFeatureTesting(
withFeaturePath('heroes'),
withInitialFeatureNavigation(),
withRouterConfig({
onSameUrlNavigation: 'reload',
})
),
],
});
const location = injector.get(SpectacularFeatureLocation);
expect(location.path()).toBe('~/');
// Angular Testing Library
const {
fixture: {
debugElement: { injector },
},
navigate,
} = await render(TestAppComponent, {
routes: [{ path: 'heroes', loadChildren: () => HeroesModule }],
imports: [
// does this work?
RouterTestingModule.withRoutes([], {
onSameUrlNavigation: 'reload',
}),
],
});
const ngZone = injector.get(NgZone);
const location = injector.get(Location);
await ngZone?.run(() => navigate('/heroes'));
expect(location.path()).toBe('/heroes');
// TestBed + RouterTestingHarness
TestBed.configureTestingModule({
providers: [
provideRouter(
[{ path: 'heroes', loadChildren: () => HeroesModule }],
withRouterConfig({
onSameUrlNavigation: 'reload',
})
),
provideLocationMocks(),
],
});
const harness = await RouterTestingHarness.create('/heroes');
const location = TestBed.inject(Location);
expect(location.path()).toBe('/heroes');
// TestBed + Spectacular 0.5
const harness = createFeatureHarness({
featureModule: HeroesModule,
featurePath: 'heroes',
routerOptions: {
onSameUrlNavigation: 'reload',
},
});
expect(harness.location.path()).toBe('~/');
// TestBed + Spectacular >=15
const harnesss = createFeatureHarness({
featurePath: 'heroes',
routes: [{ path: 'heroes', loadChildren: () => HeroesModule }],
routerOptions: {
onSameUrlNavigation: 'reload',
},
});
expect(harness.location.path()).toBe('~/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment