Skip to content

Commit

Permalink
fix(spectator): make mock template type safe (#607)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: introducing type safety for the template values in
createSpyObject and mockProvider. This might cause breaking builds
because the compiler will find issues hidden until now.
  • Loading branch information
szabyg committed May 16, 2023
1 parent 12b7ebe commit 4382e37
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
14 changes: 7 additions & 7 deletions projects/spectator/src/lib/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ export function installProtoMethods<T>(mock: any, proto: any, createSpyFn: Funct
mock[key] = createSpyFn(key);
} else if (descriptor.get && !mock.hasOwnProperty(key)) {
Object.defineProperty(mock, key, {
set: value => (mock[`_${key}`] = value),
set: (value) => (mock[`_${key}`] = value),
get: () => mock[`_${key}`],
configurable: true
configurable: true,
});
}
}
Expand All @@ -70,13 +70,13 @@ export function installProtoMethods<T>(mock: any, proto: any, createSpyFn: Funct
/**
* @publicApi
*/
export function createSpyObject<T>(type: Type<T> | AbstractType<T>, template?: Partial<Record<keyof T, any>>): SpyObject<T> {
export function createSpyObject<T>(type: Type<T> | AbstractType<T>, template?: Partial<T>): SpyObject<T> {
const mock: any = { ...template } || {};

installProtoMethods<T>(mock, type.prototype, name => {
installProtoMethods<T>(mock, type.prototype, (name) => {
const newSpy: jasmine.Spy & Partial<CompatibleSpy> = jasmine.createSpy(name);
newSpy.andCallFake = (fn: (...args: any[]) => any) => <any>newSpy.and.callFake(fn);
newSpy.andReturn = val => newSpy.and.returnValue(val);
newSpy.andReturn = (val) => newSpy.and.returnValue(val);
newSpy.reset = () => newSpy.calls.reset();
// revisit return null here (previously needed for rtts_assert).
newSpy.and.returnValue(null);
Expand All @@ -90,10 +90,10 @@ export function createSpyObject<T>(type: Type<T> | AbstractType<T>, template?: P
/**
* @publicApi
*/
export function mockProvider<T>(type: Type<T> | AbstractType<T>, properties?: Partial<Record<keyof T, any>>): FactoryProvider {
export function mockProvider<T>(type: Type<T> | AbstractType<T>, properties?: Partial<T>): FactoryProvider {
return {
provide: type,
useFactory: () => createSpyObject(type, properties)
useFactory: () => createSpyObject(type, properties),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import { RouterStub } from './router-stub';
*/
export function initialRoutingModule<S>(options: Required<SpectatorRoutingOptions<S>>): ModuleMetadata {
const moduleMetadata = initialSpectatorModule(options);
const eventsSubject = new Subject<Event>();

if (options.stubsEnabled) {
moduleMetadata.imports.push(RouterTestingModule);
moduleMetadata.providers.push(
options.mockProvider(RouterStub, {
events: new Subject<Event>(),
events: eventsSubject.asObservable(),
emitRouterEvent(event: Event): void {
this.events.next(event);
eventsSubject.next(event);
},
serializeUrl(): string {
return '/';
Expand Down

0 comments on commit 4382e37

Please sign in to comment.