wechaty-puppet
Version:
Abstract Puppet for Wechaty
58 lines • 2.15 kB
JavaScript
import { test, sinon, } from 'tstest';
import { GError } from 'gerror';
import { PuppetSkeleton, } from './puppet-skeleton.js';
test('ProtectedPropertySkeleton', async (t) => {
const noOneLeft = true;
t.ok(noOneLeft, 'should match Mixin properties for every protected property');
});
test('emit(error, ...) with GError', async (t) => {
class PuppetSkeletonImpl extends PuppetSkeleton {
}
const puppet = new PuppetSkeletonImpl();
const FIXTURES = [
undefined,
null,
true,
false,
0,
1,
'',
'foo',
[],
[1],
{},
{ foo: 'bar' },
new Error(),
new Error('foo'),
GError.from(new Error('test')),
];
let payload;
puppet.on('error', (...args) => {
payload = args[0];
});
for (const data of FIXTURES) {
puppet.emit('error', data);
await Promise.resolve();
t.equal(typeof payload.gerror, 'string', `should be an error payload for ${typeof data} "${JSON.stringify(data)}"`);
t.doesNotThrow(() => GError.fromJSON(payload.gerror), 'should be successfully deserialized to GError');
}
});
test('wrapAsync() promise', async (t) => {
class PuppetSkeletonImpl extends PuppetSkeleton {
}
const puppet = new PuppetSkeletonImpl();
const spy = sinon.spy();
puppet.on('error', spy);
const DATA = 'test';
const promise = Promise.resolve(DATA);
const wrappedPromise = puppet.wrapAsync(promise);
t.equal(await wrappedPromise, undefined, 'should resolve Promise<any> to void');
const rejection = Promise.reject(new Error('test'));
const wrappedRejection = puppet.wrapAsync(rejection);
t.equal(wrappedRejection, undefined, 'should be void and not to reject');
t.equal(spy.callCount, 0, 'should have no error before sleep');
await new Promise(resolve => setImmediate(resolve)); // wait async event loop task to be executed
t.equal(spy.callCount, 1, 'should emit error when promise reject with error');
});
//# sourceMappingURL=puppet-skeleton.spec.js.map