-
-
Notifications
You must be signed in to change notification settings - Fork 2
Add TestingRouterStore and enhanced injectTestingRouterStore to ease test setup and improve DX #362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: LayZeeDK <[email protected]>
Co-authored-by: LayZeeDK <[email protected]>
@copilot Add an injectTestingRouterStore function so consumers can avoid casting. |
Co-authored-by: LayZeeDK <[email protected]>
Added the // Before (manual casting)
const routerStore = TestBed.inject(RouterStore) as TestingRouterStore;
// After (injection helper)
TestBed.runInInjectionContext(() => {
const routerStore = injectTestingRouterStore();
routerStore.setRouteParam('id', '123'); // Direct access
}); The function follows Angular's injection conventions and provides better type safety with full IntelliSense support. Updated all documentation and examples to show both approaches. Implemented in commit 9668fcf. |
@copilot The inject function should support InjectOptions similar to TestBed.inject. It should also be possible to pass a component and use its Injector to get a local router store. |
…ector support Co-authored-by: LayZeeDK <[email protected]>
Enhanced the InjectOptions support (similar to TestBed.inject): // Basic usage
const routerStore = injectTestingRouterStore();
// With injection options
const routerStore = injectTestingRouterStore({
optional: true,
host: true,
skipSelf: false
}); Component injector support (for local router stores): // Inject from specific component's injector
const routerStore = injectTestingRouterStore({
component: HeroComponent,
fixture,
options: { host: true } // Optional InjectOptions
}); The function now has multiple overloads with proper TypeScript typing, including null safety for optional injection. This is particularly useful for testing local router stores provided at the component level. Implemented in commit e07f738. |
Implements testing utilities to simplify unit testing of components and services that depend on
RouterStore
, addressing the need for better testing support mentioned in issue #282.Problem
Testing components and services that inject
RouterStore
currently requires complex setup withRouterTestingModule
and full routing configuration, even for simple unit tests. This creates unnecessary complexity and coupling in test code.Solution
Introduces
TestingRouterStore
- a lightweight testing implementation of theRouterStore
interface that uses stubbed observables, allowing precise control over router state without routing infrastructure. Also includes enhancedinjectTestingRouterStore()
function with advanced injection options.Key Features
TestingRouterStore Class
RouterStore
interface with stubbedBehaviorSubject
observablesEnhanced injectTestingRouterStore Function
TestingRouterStore
inject()
function (optional, skipSelf, self, host)Testing Utility Methods
setUrl(url)
,setFragment(fragment)
,setTitle(title)
- Basic router state controlsetRouteParam(param, value)
/setRouteParams(params)
- Route parameter managementsetQueryParam(param, value)
/setQueryParams(params)
- Query parameter managementsetRouteDataParam(key, value)
/setRouteData(data)
- Route data manipulationsetCurrentRoute(route)
- Complete route snapshot controlreset()
- Restore all values to defaultsProvider Factory
provideTestingRouterStore()
replacesRouterStore
DI token withTestingRouterStore
Usage Examples
Basic Component Testing
Local Router Store Testing
Service Testing
Benefits
RouterTestingModule
needed for unit testsinjectTestingRouterStore()
provides direct access to testing methodsinject()
functionDocumentation
Added comprehensive testing section to README with:
RouterTestingModule
for full routing testsTesting
TestingRouterStore
and enhancedinjectTestingRouterStore
functionalityThis implementation provides advanced testing utilities that go beyond the original issue requirements, making it significantly easier to test RouterStore-dependent code in all scenarios while maintaining full compatibility with existing patterns.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.