Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add listen callback multiple test #23

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"main": "lib/remoteAsync.cjs.js",
"module": "lib/remoteAsync.esm.js",
"license": "MIT",
"engines": {
"node": ">=10",
"yarn": ">=1.22 <2"
},
"scripts": {
"test": "jest --verbose",
"test:coverage": "jest --coverage",
Expand Down
21 changes: 21 additions & 0 deletions tests/inner.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Server } from '../src';
import { RemoteCallBack } from '../src/Server';
import { PROMISE_TYPE } from '../src/promiseType';

describe('remoteAsync:inner test', () => {
Expand Down Expand Up @@ -61,6 +62,26 @@ describe('remoteAsync:inner test', () => {
const retData = await listener.registerPromise('dataSend', object).catch((d) => Promise.resolve({ v: 1, d }));
expect(retData).toEqual({ v: 1, d: retObject });
});
test('listen callback multiple times', async () => {
const listener = new Server();
listener.registerSender((data) => listener.receiveData(data));
const cb1 = jest.fn(((data, resolve) => {
// only first resolve or reject will be accepted when multiple callbacks
console.log('cb1', data);
resolve?.({ a: 1 });
expect(resolve).not.toEqual(undefined);
}) as RemoteCallBack);
const cb2 = jest.fn(((data, resolve ,reject) => {
// no effect
reject?.({ a: 1 });
expect(reject).not.toEqual(undefined);
}) as RemoteCallBack);
listener.listen('k', cb1);
listener.listen('k', cb2);
await listener.registerPromise('k', { a: 1 });
expect(cb1).toBeCalledTimes(1);
expect(cb2).toBeCalledTimes(1);
})
test('off cb', async () => {
const listener = new Server();
const cb1 = () => 'k';
Expand Down
Loading