Skip to content

Commit 53b2cf6

Browse files
committed
chore: add tests for messaging sending flow
1 parent 2305576 commit 53b2cf6

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

test/unit/channel.test.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,3 +1723,122 @@ describe('delete reaction flow', () => {
17231723
});
17241724
});
17251725
});
1726+
1727+
describe('message sending flow', () => {
1728+
let client;
1729+
let channel;
1730+
let loggerSpy;
1731+
let queueTaskSpy;
1732+
let postSpy;
1733+
1734+
const message = {
1735+
id: 'msg-123',
1736+
text: 'Hello world',
1737+
user: { id: 'user-abc' },
1738+
};
1739+
1740+
const options = {
1741+
pending: true,
1742+
skip_push: true,
1743+
pending_message_metadata: { source: 'local' },
1744+
};
1745+
1746+
beforeEach(async () => {
1747+
client = await getClientWithUser({ id: 'user-abc' });
1748+
const offlineDb = new MockOfflineDB({ client });
1749+
1750+
client.setOfflineDBApi(offlineDb);
1751+
await client.offlineDb.init(client.userID);
1752+
1753+
channel = client.channel('messaging', 'test');
1754+
1755+
loggerSpy = vi.spyOn(client, 'logger').mockImplementation(vi.fn());
1756+
queueTaskSpy = vi.spyOn(client.offlineDb, 'queueTask').mockResolvedValue({});
1757+
postSpy = vi.spyOn(client, 'post').mockResolvedValue({});
1758+
});
1759+
1760+
afterEach(() => {
1761+
vi.resetAllMocks();
1762+
});
1763+
1764+
describe('sendMessage', () => {
1765+
beforeEach(() => {
1766+
vi.spyOn(channel, '_sendMessage').mockResolvedValue({});
1767+
});
1768+
1769+
afterEach(() => {
1770+
vi.resetAllMocks();
1771+
});
1772+
1773+
it('queues task if offlineDb exists and message has ID', async () => {
1774+
const result = await channel.sendMessage(message, options);
1775+
1776+
expect(queueTaskSpy).toHaveBeenCalledTimes(1);
1777+
expect(queueTaskSpy).toHaveBeenCalledWith({
1778+
task: {
1779+
channelId: 'test',
1780+
channelType: 'messaging',
1781+
messageId: 'msg-123',
1782+
payload: [message, options],
1783+
type: 'send-message',
1784+
},
1785+
});
1786+
1787+
expect(result).toEqual({});
1788+
expect(channel._sendMessage).not.toHaveBeenCalled();
1789+
});
1790+
1791+
it('falls back to _sendMessage if offlineDb is missing', async () => {
1792+
client.offlineDb = undefined;
1793+
1794+
const result = await channel.sendMessage(message, options);
1795+
1796+
expect(channel._sendMessage).toHaveBeenCalledTimes(1);
1797+
expect(channel._sendMessage).toHaveBeenCalledWith(message, options);
1798+
expect(result).toEqual({});
1799+
});
1800+
1801+
it('falls back to _sendMessage if message.id is missing', async () => {
1802+
const msg = { ...message, id: undefined };
1803+
1804+
await channel.sendMessage(msg, options);
1805+
1806+
expect(channel._sendMessage).toHaveBeenCalledWith(msg, options);
1807+
});
1808+
1809+
it('falls back to _sendMessage if offlineDb throws', async () => {
1810+
queueTaskSpy.mockRejectedValue(new Error('Queue failed'));
1811+
1812+
const result = await channel.sendMessage(message, options);
1813+
1814+
expect(loggerSpy).toHaveBeenCalledTimes(1);
1815+
expect(channel._sendMessage).toHaveBeenCalledWith(message, options);
1816+
expect(result).toEqual({});
1817+
});
1818+
});
1819+
1820+
describe('_sendMessage', () => {
1821+
it('posts the message to the correct endpoint with options', async () => {
1822+
const expectedUrl = `${client.baseURL}/channels/messaging/test/message`;
1823+
1824+
const result = await channel._sendMessage(message, options);
1825+
1826+
expect(postSpy).toHaveBeenCalledTimes(1);
1827+
expect(postSpy).toHaveBeenCalledWith(expectedUrl, {
1828+
message,
1829+
...options,
1830+
});
1831+
1832+
expect(result).toEqual({});
1833+
});
1834+
1835+
it('works without options', async () => {
1836+
await channel._sendMessage(message);
1837+
1838+
expect(postSpy).toHaveBeenCalledWith(
1839+
`${client.baseURL}/channels/messaging/test/message`,
1840+
{ message },
1841+
);
1842+
});
1843+
});
1844+
});

0 commit comments

Comments
 (0)