oidc-client-rx/test/auto-mock.ts
lonelyhentxi 983254164b
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run
feat: init and fork some code from angular-auth-oidc-client
2025-01-17 04:24:12 +08:00

50 lines
1.1 KiB
TypeScript

import { Provider } from 'injection-js';
export function mockClass<T>(obj: new (...args: any[]) => T): any {
const keys = Object.getOwnPropertyNames(obj.prototype);
const allMethods = keys.filter((key) => {
try {
return typeof obj.prototype[key] === 'function';
} catch (error) {
return false;
}
});
const allProperties = keys.filter((x) => !allMethods.includes(x));
const mockedClass = class T {};
allMethods.forEach(
(method: string) =>
((mockedClass.prototype as any)[method] = (): void => {
return;
})
);
allProperties.forEach((method) => {
Object.defineProperty(mockedClass.prototype, method, {
get() {
return '';
},
configurable: true,
});
});
return mockedClass;
}
export function mockProvider<T>(obj: new (...args: any[]) => T): Provider {
return {
provide: obj,
useClass: mockClass(obj),
};
}
export function mockAbstractProvider<T, M extends T>(
type: abstract new (...args: any[]) => T,
mockType: new (...args: any[]) => M
): Provider {
const mock = mockClass(mockType);
return { provide: type, useClass: mock };
}