-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: overrides as functions are properly cloned #455
- Loading branch information
Showing
13 changed files
with
490 additions
and
39 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 |
---|---|---|
@@ -1,13 +1,10 @@ | ||
import helperMockService from '../mock-service/helper.mock-service'; | ||
|
||
export default (instance: any, property: keyof any, value: any, enumerable = false) => { | ||
// istanbul ignore else | ||
if (Object.defineProperty) { | ||
Object.defineProperty(instance, property, { | ||
configurable: true, | ||
enumerable, | ||
value, | ||
writable: true, | ||
}); | ||
} else { | ||
instance[property] = value; | ||
} | ||
helperMockService.definePropertyDescriptor(instance, property, { | ||
configurable: true, | ||
enumerable, | ||
value, | ||
writable: true, | ||
}); | ||
}; |
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,22 @@ | ||
import helperDefinePropertyDescriptor from './helper.define-property-descriptor'; | ||
import helperExtractMethodsFromPrototype from './helper.extract-methods-from-prototype'; | ||
import helperExtractPropertiesFromPrototype from './helper.extract-properties-from-prototype'; | ||
import helperExtractPropertyDescriptor from './helper.extract-property-descriptor'; | ||
|
||
export default (service: any): any => { | ||
const instance = function () { | ||
// tslint:disable-next-line:ban-ts-ignore | ||
// @ts-ignore | ||
return service.apply(this, arguments); | ||
}; | ||
|
||
for (const prop of [ | ||
...helperExtractMethodsFromPrototype(service), | ||
...helperExtractPropertiesFromPrototype(service), | ||
]) { | ||
const desc = helperExtractPropertyDescriptor(service, prop); | ||
helperDefinePropertyDescriptor(instance, prop, desc); | ||
} | ||
|
||
return instance; | ||
}; |
26 changes: 26 additions & 0 deletions
26
libs/ng-mocks/src/lib/mock-service/helper.define-property-descriptor.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,26 @@ | ||
import helperExtractPropertyDescriptor from './helper.extract-property-descriptor'; | ||
|
||
export default (instance: any, prop: keyof any, desc?: PropertyDescriptor): boolean => { | ||
// istanbul ignore next | ||
if (!desc) { | ||
return false; | ||
} | ||
|
||
// istanbul ignore else | ||
if (Object.defineProperty) { | ||
const sourceDesc = helperExtractPropertyDescriptor(instance, prop); | ||
if (sourceDesc?.configurable === false) { | ||
return false; | ||
} | ||
|
||
Object.defineProperty(instance, prop, { | ||
...desc, | ||
configurable: true, | ||
...(desc.writable === false ? { writable: true } : {}), | ||
}); | ||
} else { | ||
instance[prop] = desc.value; | ||
} | ||
|
||
return true; | ||
}; |
2 changes: 1 addition & 1 deletion
2
libs/ng-mocks/src/lib/mock-service/helper.extract-property-descriptor.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
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
Oops, something went wrong.