Files
upstream-rocketchat/packages/ddp-client/__tests__/MinimalDDPClient.spec.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

375 lines
7.6 KiB
TypeScript

import { MinimalDDPClient } from '../src/MinimalDDPClient';
describe('ping/pong mechanism', () => {
it('should reply with pong if no id is provided', () => {
const handlerCallback = jest.fn();
const senderCallback = jest.fn();
const client = new MinimalDDPClient(() => undefined);
client.once('message', handlerCallback);
client.once('send', senderCallback);
client.handleMessage(
JSON.stringify({
msg: 'ping',
}),
);
expect(handlerCallback).toHaveBeenCalledTimes(1);
expect(handlerCallback).toHaveBeenCalledWith({
msg: 'ping',
});
expect(senderCallback).toHaveBeenCalledTimes(1);
expect(senderCallback).toHaveBeenCalledWith({
msg: 'pong',
});
});
it('should reply with pong if id is provided', () => {
const handlerCallback = jest.fn();
const senderCallback = jest.fn();
const client = new MinimalDDPClient(() => undefined);
client.on('message', handlerCallback);
client.on('send', senderCallback);
client.handleMessage(
JSON.stringify({
msg: 'ping',
id: '123',
}),
);
expect(handlerCallback).toHaveBeenCalledTimes(1);
expect(handlerCallback).toHaveBeenCalledWith({
msg: 'ping',
id: '123',
});
expect(senderCallback).toHaveBeenCalledTimes(1);
expect(senderCallback).toHaveBeenCalledWith({
msg: 'pong',
id: '123',
});
});
it('should emit "pong" event if pong is received', () => {
const handlerCallback = jest.fn();
const client = new MinimalDDPClient(() => undefined);
client.once('pong', handlerCallback);
client.handleMessage(
JSON.stringify({
msg: 'pong',
}),
);
expect(handlerCallback).toHaveBeenCalledTimes(1);
expect(handlerCallback).toHaveBeenCalledWith({
msg: 'pong',
});
});
});
describe('subscription mechanism', () => {
it('should emit "ready" event after subscribe', () => {
const handlerCallback = jest.fn();
const client = new MinimalDDPClient(() => undefined);
const id = client.subscribe('test');
client.onPublish(id, handlerCallback);
client.handleMessage(
JSON.stringify({
msg: 'ready',
subs: [id],
}),
);
expect(handlerCallback).toHaveBeenCalledTimes(1);
expect(handlerCallback).toHaveBeenCalledWith({
msg: 'ready',
subs: [id],
});
});
it('should emit "nosub" event if something goes wrong', () => {
const handlerCallback = jest.fn();
const client = new MinimalDDPClient(() => undefined);
const id = client.subscribe('test');
client.onPublish(id, handlerCallback);
client.handleMessage(
JSON.stringify({
msg: 'nosub',
id,
}),
);
expect(handlerCallback).toHaveBeenCalledTimes(1);
expect(handlerCallback).toHaveBeenCalledWith({
msg: 'nosub',
id,
});
});
it('should emit "nosub" event after unsubscribe', () => {
const handlerCallback = jest.fn();
const nosubCallback = jest.fn();
const client = new MinimalDDPClient();
const id = client.subscribe('test');
client.onPublish(id, handlerCallback);
client.onNoSub(id, nosubCallback);
client.handleMessage(
JSON.stringify({
msg: 'ready',
subs: [id],
}),
);
client.unsubscribe(id);
client.handleMessage(
JSON.stringify({
msg: 'nosub',
id,
}),
);
expect(handlerCallback).toHaveBeenCalledTimes(1);
expect(handlerCallback).toHaveBeenCalledWith({
msg: 'ready',
subs: [id],
});
expect(nosubCallback).toHaveBeenCalledTimes(1);
expect(nosubCallback).toHaveBeenCalledWith({
msg: 'nosub',
id,
});
});
});
describe('subscription added/changed/removed mechanism', () => {
it('should receive "added" events after subscribe', () => {
const handlerCallback = jest.fn();
const readyCallback = jest.fn();
const client = new MinimalDDPClient(() => undefined);
const id = client.subscribe('test');
client.onCollection('test', handlerCallback);
client.onPublish(id, readyCallback);
client.handleMessage(
JSON.stringify({
msg: 'ready',
subs: [id],
}),
);
client.handleMessage(
JSON.stringify({
msg: 'added',
collection: 'test',
id,
fields: {
test: 'test',
},
}),
);
expect(readyCallback).toHaveBeenCalledTimes(1);
expect(readyCallback).toHaveBeenCalledWith({
msg: 'ready',
subs: [id],
});
expect(handlerCallback).toHaveBeenCalledTimes(1);
expect(handlerCallback).toHaveBeenCalledWith({
msg: 'added',
collection: 'test',
id,
fields: {
test: 'test',
},
});
});
it('should receive "changed" events after subscribe', () => {
const handlerCallback = jest.fn();
const readyCallback = jest.fn();
const client = new MinimalDDPClient(() => undefined);
const id = client.subscribe('test');
client.onCollection('test', handlerCallback);
client.onPublish(id, readyCallback);
client.handleMessage(
JSON.stringify({
msg: 'ready',
subs: [id],
}),
);
client.handleMessage(
JSON.stringify({
msg: 'changed',
collection: 'test',
id,
fields: {
test: 'test',
},
}),
);
expect(readyCallback).toHaveBeenCalledTimes(1);
expect(readyCallback).toHaveBeenCalledWith({
msg: 'ready',
subs: [id],
});
expect(handlerCallback).toHaveBeenCalledTimes(1);
expect(handlerCallback).toHaveBeenCalledWith({
msg: 'changed',
collection: 'test',
id,
fields: {
test: 'test',
},
});
});
it('should receive "removed" events after subscribe', () => {
const handlerCallback = jest.fn();
const readyCallback = jest.fn();
const client = new MinimalDDPClient(() => undefined);
const id = client.subscribe('test');
client.onCollection('test', handlerCallback);
client.onPublish(id, readyCallback);
client.handleMessage(
JSON.stringify({
msg: 'ready',
subs: [id],
}),
);
client.handleMessage(
JSON.stringify({
msg: 'removed',
collection: 'test',
id,
}),
);
expect(readyCallback).toHaveBeenCalledTimes(1);
expect(readyCallback).toHaveBeenCalledWith({
msg: 'ready',
subs: [id],
});
expect(handlerCallback).toHaveBeenCalledTimes(1);
expect(handlerCallback).toHaveBeenCalledWith({
msg: 'removed',
collection: 'test',
id,
});
});
});
describe('subscription updated mechanism', () => {
it('should receive "updated" events after subscribe and before ready', () => {
const handlerCallback = jest.fn();
const readyCallback = jest.fn();
const updatedCallback = jest.fn();
const client = new MinimalDDPClient(() => undefined);
const id = client.subscribe('test');
client.onCollection(id, handlerCallback);
client.onPublish(id, readyCallback);
client.onUpdate(id, updatedCallback);
client.handleMessage(
JSON.stringify({
msg: 'ready',
subs: [id],
}),
);
client.handleMessage(
JSON.stringify({
msg: 'updated',
methods: [id],
}),
);
expect(readyCallback).toHaveBeenCalledTimes(1);
expect(handlerCallback).toHaveBeenCalledTimes(0);
expect(updatedCallback).toHaveBeenCalledTimes(1);
expect(updatedCallback).toHaveBeenCalledWith({
msg: 'updated',
methods: [id],
});
});
});
describe('method mechanism', () => {
it('should emit "result" event after method call', () => {
const handlerCallback = jest.fn();
const client = new MinimalDDPClient(() => undefined);
const { id } = client.call('test');
client.onResult(id, handlerCallback);
client.handleMessage(
JSON.stringify({
msg: 'result',
params: [],
id,
}),
);
expect(handlerCallback).toHaveBeenCalledTimes(1);
expect(handlerCallback).toHaveBeenCalledWith({
msg: 'result',
params: [],
id,
});
});
});
describe('error mechanism', () => {
it('should throw error if the payload is not valid', () => {
const client = new MinimalDDPClient(() => undefined);
expect(() => {
client.handleMessage('test');
}).toThrow();
});
});