-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: initial SpectatorWithRouting proposal * lint fixes * docs: update README.md * refactor: lint fix * refactor: fix build * feat: add Jest implementation of SpectatorWithRouting * fix: lint fixes
- Loading branch information
Showing
23 changed files
with
11,877 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Type } from '@angular/core'; | ||
import { | ||
createRoutedComponentFactory as baseCreateRoutedComponentFactory, | ||
isType, | ||
SpectatorWithRouting as BaseSpectatorWithRouting, | ||
SpectatorWithRoutingOptions, | ||
SpectatorWithRoutingOverrides, | ||
Token | ||
} from '@netbasal/spectator'; | ||
|
||
import { mockProvider, SpyObject } from './mock'; | ||
|
||
/** | ||
* @publicApi | ||
*/ | ||
export class SpectatorWithRouting<C> extends BaseSpectatorWithRouting<C> { | ||
public get<T>(type: Token<T> | Token<any>, fromComponentInjector: boolean = false): SpyObject<T> { | ||
return super.get(type, fromComponentInjector) as SpyObject<T>; | ||
} | ||
} | ||
|
||
/** | ||
* @publicApi | ||
*/ | ||
export type SpectatorWithRoutingFactory<C> = (overrides?: SpectatorWithRoutingOverrides<C>) => SpectatorWithRouting<C>; | ||
|
||
/** | ||
* @publicApi | ||
*/ | ||
export function createRoutedComponentFactory<C>(typeOrOptions: SpectatorWithRoutingOptions<C> | Type<C>): SpectatorWithRoutingFactory<C> { | ||
return baseCreateRoutedComponentFactory({ | ||
mockProvider, | ||
...(isType(typeOrOptions) ? { component: typeOrOptions } : typeOrOptions) | ||
}) as SpectatorWithRoutingFactory<C>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
projects/spectator/jest/test/with-routing/my-page.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { Router, RouterLink } from '@angular/router'; | ||
import { createRoutedComponentFactory } from '@netbasal/spectator/jest'; | ||
|
||
import { MyPageComponent } from '../../../test/with-routing/my-page.component'; | ||
|
||
describe('MyPageComponent', () => { | ||
describe('simple use', () => { | ||
const createComponent = createRoutedComponentFactory(MyPageComponent); | ||
|
||
it('should create', () => { | ||
const spectator = createComponent(); | ||
|
||
expect(spectator.query('.foo')).toExist(); | ||
}); | ||
}); | ||
|
||
describe('route options', () => { | ||
const createComponent = createRoutedComponentFactory({ | ||
component: MyPageComponent, | ||
data: { title: 'lorem', dynamicTitle: 'ipsum' }, | ||
params: { foo: '1', bar: '2' }, | ||
queryParams: { baz: '3' } | ||
}); | ||
|
||
it('should create with default options', () => { | ||
const spectator = createComponent(); | ||
|
||
expect(spectator.query('.title')).toHaveText('lorem'); | ||
expect(spectator.query('.dynamic-title')).toHaveText('ipsum'); | ||
|
||
expect(spectator.query('.foo')).toHaveText('1'); | ||
expect(spectator.query('.bar')).toHaveText('2'); | ||
expect(spectator.query('.baz')).toHaveText('3'); | ||
}); | ||
|
||
it('should create with overridden options', () => { | ||
const spectator = createComponent({ | ||
params: { foo: 'A', bar: 'B' } | ||
}); | ||
|
||
expect(spectator.query('.foo')).toHaveText('A'); | ||
expect(spectator.query('.bar')).toHaveText('B'); | ||
expect(spectator.query('.baz')).toHaveText('3'); | ||
}); | ||
|
||
it('should respond to updates', () => { | ||
const spectator = createComponent({ | ||
params: { foo: 'A', bar: 'B' } | ||
}); | ||
|
||
expect(spectator.query('.foo')).toHaveText('A'); | ||
expect(spectator.query('.bar')).toHaveText('B'); | ||
expect(spectator.query('.baz')).toHaveText('3'); | ||
|
||
spectator.setRouteParam('bar', 'X'); | ||
|
||
expect(spectator.query('.foo')).toHaveText('A'); | ||
expect(spectator.query('.bar')).toHaveText('X'); | ||
expect(spectator.query('.baz')).toHaveText('3'); | ||
expect(spectator.component.fragment).toBeNull(); | ||
|
||
spectator.setRouteQueryParam('baz', 'Y'); | ||
spectator.setRouteFragment('lorem'); | ||
|
||
expect(spectator.query('.foo')).toHaveText('A'); | ||
expect(spectator.query('.bar')).toHaveText('X'); | ||
expect(spectator.query('.baz')).toHaveText('Y'); | ||
expect(spectator.component.fragment).toBe('lorem'); | ||
}); | ||
|
||
it('should support snapshot data', () => { | ||
const spectator = createComponent(); | ||
|
||
expect(spectator.query('.title')).toHaveText('lorem'); | ||
expect(spectator.query('.dynamic-title')).toHaveText('ipsum'); | ||
|
||
spectator.triggerNavigation({ | ||
data: { title: 'new-title', dynamicTitle: 'new-dynamic-title' } | ||
}); | ||
|
||
expect(spectator.query('.title')).toHaveText('lorem'); | ||
expect(spectator.query('.dynamic-title')).toHaveText('new-dynamic-title'); | ||
}); | ||
}); | ||
|
||
describe('routerLinks', () => { | ||
const createComponent = createRoutedComponentFactory(MyPageComponent); | ||
|
||
it('should mock routerLinks', () => { | ||
const spectator = createComponent(); | ||
|
||
// tslint:disable-next-line:no-unnecessary-type-assertion | ||
const link1 = spectator.query('.link-1', { read: RouterLink })!; | ||
|
||
expect(link1.routerLink).toEqual(['foo']); | ||
}); | ||
}); | ||
|
||
describe('default router mocking', () => { | ||
const createComponent = createRoutedComponentFactory({ | ||
component: MyPageComponent | ||
}); | ||
|
||
it('should support mocks', () => { | ||
const spectator = createComponent(); | ||
|
||
spectator.click('.link-2'); | ||
|
||
expect(spectator.get(Router).navigate).toHaveBeenCalledWith(['bar']); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.