Skip to content

Commit

Permalink
fix(mock): allow assigning read-only properties
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkluijk committed Aug 26, 2019
1 parent 5608150 commit 1542c24
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
10 changes: 9 additions & 1 deletion projects/spectator/jest/test/spy-object/spy-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ describe('SpyObject', () => {
person.sayHi.andReturn('Bye!');
});

it('should keep getters/setters', () => {
it('should enable spying on properties', () => {
const person = createSpyObject(Person);
person.birthYear = 1990;
jest.spyOn(person, 'age', 'get').mockReturnValue(29);

expect(person.age).toBe(29);
});

it('should enable setting properties by just assigning', () => {
const person = createSpyObject(Person);
person.birthYear = 1990;
(person as any).age = 29;

expect(person.age).toBe(29);
});

it('should allow setting properties', () => {
const person = createSpyObject(Person);

Expand Down
3 changes: 2 additions & 1 deletion projects/spectator/src/lib/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ 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, {
get: () => null
set: value => (mock[`_${key}`] = value),
get: () => mock[`_${key}`]
});
}
}
Expand Down
8 changes: 8 additions & 0 deletions projects/spectator/test/spy-object/spy-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe('SpyObject', () => {
expect(person.age).toBe(29);
});

it('should enable setting properties by just assigning', () => {
const person = createSpyObject(Person);
person.birthYear = 1990;
(person as any).age = 29;

expect(person.age).toBe(29);
});

it('should allow setting properties', () => {
const person = createSpyObject(Person);

Expand Down

0 comments on commit 1542c24

Please sign in to comment.