Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: createMock, for easier SpyObj creations
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { createMock } from './mock'; | ||
|
||
class Simple { | ||
foo(): string { | ||
return ''; | ||
} | ||
bar(): string { | ||
return ''; | ||
} | ||
baz(): string { | ||
return ''; | ||
} | ||
} | ||
|
||
class GrandParent { | ||
foo(): string { | ||
return ''; | ||
} | ||
} | ||
class Parent extends GrandParent { | ||
bar(): string { | ||
return ''; | ||
} | ||
} | ||
class Child extends Parent { | ||
baz(): string { | ||
return ''; | ||
} | ||
} | ||
|
||
abstract class AbstractBase { | ||
abstract foo(): string; | ||
bar(): string { | ||
return ''; | ||
} | ||
} | ||
class AbstractChild extends AbstractBase { | ||
foo(): string { | ||
return ''; | ||
} | ||
|
||
baz(): string { | ||
return ''; | ||
} | ||
} | ||
|
||
describe('mock', () => { | ||
it('should create a mock for a simple class', () => { | ||
const mock = createMock(Simple); | ||
mock.foo.and.returnValue('foo'); | ||
mock.bar.and.returnValue('bar'); | ||
mock.baz.and.returnValue('baz'); | ||
|
||
expect(mock.foo()).toBe('foo'); | ||
expect(mock.bar()).toBe('bar'); | ||
expect(mock.baz()).toBe('baz'); | ||
}); | ||
|
||
it('should create a mock for a child class class', () => { | ||
const mock = createMock(Child); | ||
mock.foo.and.returnValue('foo'); | ||
mock.bar.and.returnValue('bar'); | ||
mock.baz.and.returnValue('baz'); | ||
|
||
expect(mock.foo()).toBe('foo'); | ||
expect(mock.bar()).toBe('bar'); | ||
expect(mock.baz()).toBe('baz'); | ||
}); | ||
|
||
it('should create a mock for a child class of an abstract class', () => { | ||
const mock = createMock(AbstractChild); | ||
mock.foo.and.returnValue('foo'); | ||
mock.bar.and.returnValue('bar'); | ||
mock.baz.and.returnValue('baz'); | ||
|
||
expect(mock.foo()).toBe('foo'); | ||
expect(mock.bar()).toBe('bar'); | ||
expect(mock.baz()).toBe('baz'); | ||
}); | ||
}); |
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,27 @@ | ||
import { Type } from '@angular/core'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function collectMethodNames(proto: unknown): Array<string> { | ||
if (!proto || proto === Object.prototype) { | ||
return []; | ||
} | ||
const methodNames: Array<string> = []; | ||
for (const key of Object.getOwnPropertyNames(proto)) { | ||
const descriptor = Object.getOwnPropertyDescriptor(proto, key); | ||
if (descriptor && typeof descriptor.value === 'function' && key !== 'constructor') { | ||
methodNames.push(key); | ||
} | ||
} | ||
return [...methodNames, ...collectMethodNames(Object.getPrototypeOf(proto))]; | ||
} | ||
|
||
/** | ||
* Creates a spy object for a class where all the methods of the class (and of its superclasses) are spies. | ||
* I.e., for a class `UserService` with methods `get()`, `create()`, `update()` and `delete()`, calling | ||
* `createMock(UserService)` is equivalent to calling | ||
* `jasmine.createSpyObj<UserService>('UserService', ['get', 'create', 'update', 'delete']). | ||
* @param type the type to mock (usually a service class) | ||
*/ | ||
export function createMock<T>(type: Type<T>): jasmine.SpyObj<T> { | ||
return jasmine.createSpyObj<T>(type.name, collectMethodNames(type.prototype) as unknown as jasmine.SpyObjMethodNames<T>); | ||
} |
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