Skip to content

Commit

Permalink
feat(kernel): add restore() fn to PLATFORM
Browse files Browse the repository at this point in the history
  • Loading branch information
fkleuver committed Jun 16, 2019
1 parent 143691a commit 2ced7dd
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions packages/kernel/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,21 @@ const hasOwnProperty = Object.prototype.hasOwnProperty as unknown as {
const emptyArray = Object.freeze([]) as unknown as any[];
const emptyObject = Object.freeze({}) as any;

export const PLATFORM = {
function getRestoreFn(): () => void {
const platformKeys = Object.freeze(Object.keys($PLATFORM) as (keyof typeof $PLATFORM)[]);

function restore(): void {
for (const key of platformKeys) {
if (PLATFORM[key] !== $PLATFORM[key]) {
PLATFORM[key] = $PLATFORM[key];
}
}
}

return restore;
}

const $PLATFORM = Object.freeze({
global: $global,
emptyArray,
emptyObject,
Expand Down Expand Up @@ -319,5 +333,15 @@ export const PLATFORM = {
// tslint:disable-next-line:no-any
setTimeout(handler: ITimerHandler, timeout?: number, ...args: any[]): number {
return $global.setTimeout(handler, timeout, ...args);
}
};
},

/**
* Restore the global `PLATFORM` object to its original state as it was immediately after module initialization.
* Useful for when you need to stub out one or more of its methods in a unit test.
*
* Extraneous properties are NOT removed.
*/
restore: getRestoreFn(),
});

export const PLATFORM = { ...$PLATFORM };

0 comments on commit 2ced7dd

Please sign in to comment.