1
0
Fork 0
js-utils/test/utils/TestReflect.ts

44 lines
1.4 KiB
TypeScript
Raw Normal View History

import { expect } from 'chai';
2022-10-08 19:10:17 +00:00
import { InvalidArgumentError } from '../../src/error/InvalidArgumentError.js';
import { constructorName, getConstructor, getMethods } from '../../src/Reflect.js';
class Test {
public foo() { /* noop */ }
public bar() { /* noop */ }
}
describe('reflect utils', () => {
describe('get methods helper', () => {
2022-10-08 19:10:17 +00:00
it('should collect method functions', async () => {
const methods = getMethods(new Test()).values();
/* eslint-disable @typescript-eslint/unbound-method */
expect(methods).to.include(Test.prototype.foo);
expect(methods).to.include(Test.prototype.bar);
});
});
describe('get constructor helper', () => {
2022-10-08 19:10:17 +00:00
it('should get the constructor from an instance', async () => {
const instance = new Test();
expect(getConstructor(instance)).to.equal(Test);
});
});
describe('get constructor name helper', () => {
2022-10-08 19:10:17 +00:00
it('should get the constructor name from an instance', async () => {
const instance = new Test();
expect(constructorName(instance)).to.equal(Test.name);
});
2022-10-08 19:10:17 +00:00
it('should throw when value has no prototype', async () => {
/* eslint-disable-next-line no-null/no-null */
const protoless = Object.create(null, {});
expect(() => constructorName(protoless)).to.throw(InvalidArgumentError);
});
xit('should handle nil values');
});
});