Skip to content

Commit

Permalink
fix(mock): make SpyObject properties writable by default
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkluijk committed Sep 24, 2019
1 parent 5dac95f commit 3d0931f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
7 changes: 6 additions & 1 deletion projects/spectator/jest/src/lib/mock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { FactoryProvider } from '@angular/core';
import { installProtoMethods, CompatibleSpy, SpyObject as BaseSpyObject, InjectableType } from '@ngneat/spectator';

export type SpyObject<T> = BaseSpyObject<T> & { [P in keyof T]: T[P] & (T[P] extends (...args: any[]) => infer R ? jest.Mock<R> : T[P]) };
type Writable<T> = { -readonly [P in keyof T]: T[P] };

export type SpyObject<T> = BaseSpyObject<T> &
{
[P in keyof Writable<T>]: T[P] & (T[P] extends (...args: any[]) => infer R ? jest.Mock<R> : T[P]);
};

/**
* @internal
Expand Down
2 changes: 2 additions & 0 deletions projects/spectator/jest/test/spy-object/spy-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ describe('SpyObject', () => {

person.castToWritable().name = 'Other name'; // should compile

person.name = 'Other name'; // should also compile since 4.3.1

expect(person.name).toBe('Other name');
});
});
6 changes: 4 additions & 2 deletions projects/spectator/src/lib/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ export interface CompatibleSpy extends jasmine.Spy {
/**
* @publicApi
*/
export type SpyObject<T> = T &
{ [P in keyof T]: T[P] extends Function ? T[P] & CompatibleSpy : T[P] } & {
export type SpyObject<T> = Writable<T> &
{ [P in keyof Writable<T>]: T[P] extends Function ? T[P] & CompatibleSpy : T[P] } & {
/**
* Casts to type without readonly properties
*
* @deprecated Not needed anymore as since 4.3.1 all properties of SpyObject are already writable
*/
castToWritable(): Writable<T>;
};
Expand Down
2 changes: 2 additions & 0 deletions projects/spectator/test/spy-object/spy-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ describe('SpyObject', () => {

person.castToWritable().name = 'Other name'; // should compile

person.name = 'Other name'; // should also compile since 4.3.1

expect(person.name).toBe('Other name');
});
});

0 comments on commit 3d0931f

Please sign in to comment.