|
| 1 | +import { expect } from 'chai'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import { StreamChat } from '../../src'; |
| 4 | +import { generateChannel } from './test-utils/generateChannel'; |
| 5 | +import { v4 as uuidv4 } from 'uuid'; |
| 6 | + |
| 7 | +describe('Draft Messages', () => { |
| 8 | + let client; |
| 9 | + let channel; |
| 10 | + const apiKey = 'test-api-key'; |
| 11 | + const channelType = 'messaging'; |
| 12 | + const channelID = 'test-channel'; |
| 13 | + const userID = 'test-user'; |
| 14 | + const parentID = 'parent-message-id'; |
| 15 | + |
| 16 | + const draftMessage = { |
| 17 | + text: 'Draft message text', |
| 18 | + attachments: [{ type: 'image', url: 'https://example.com/image.jpg' }], |
| 19 | + mentioned_users: ['user1', 'user2'], |
| 20 | + }; |
| 21 | + |
| 22 | + const draftWithParent = { |
| 23 | + text: 'Draft message text', |
| 24 | + attachments: [{ type: 'image', url: 'https://example.com/image.jpg' }], |
| 25 | + mentioned_users: ['user1', 'user2'], |
| 26 | + parent_id: parentID, |
| 27 | + }; |
| 28 | + |
| 29 | + const draftResponse = { |
| 30 | + draft: { |
| 31 | + channel_cid: `${channelType}:${channelID}`, |
| 32 | + created_at: '2023-01-01T00:00:00Z', |
| 33 | + message: { |
| 34 | + id: 'draft-id', |
| 35 | + ...draftMessage, |
| 36 | + }, |
| 37 | + parent_id: parentID, |
| 38 | + }, |
| 39 | + }; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + client = new StreamChat(apiKey); |
| 43 | + client.userID = userID; |
| 44 | + let channelResponse = generateChannel({ |
| 45 | + channel: { id: channelID, name: 'Test channel', members: [] }, |
| 46 | + }).channel; |
| 47 | + channel = client.channel(channelResponse.type, channelResponse.id); |
| 48 | + |
| 49 | + // Mock the methods |
| 50 | + sinon.stub(client, 'queryDrafts').resolves(draftResponse); |
| 51 | + sinon.stub(channel, 'createDraft').resolves(draftResponse); |
| 52 | + sinon.stub(channel, 'getDraft').resolves(draftResponse); |
| 53 | + sinon.stub(channel, 'deleteDraft').resolves({ duration: '0.01ms' }); |
| 54 | + }); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + sinon.restore(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should create a draft message', async () => { |
| 61 | + const response = await channel.createDraft(draftMessage); |
| 62 | + |
| 63 | + expect(channel.createDraft.calledOnce).to.be.true; |
| 64 | + expect(channel.createDraft.firstCall.args[0]).to.deep.equal(draftMessage); |
| 65 | + expect(response).to.deep.equal(draftResponse); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should create a draft message with parent ID', async () => { |
| 69 | + const response = await channel.createDraft(draftWithParent); |
| 70 | + |
| 71 | + expect(channel.createDraft.calledOnce).to.be.true; |
| 72 | + expect(channel.createDraft.firstCall.args[0]).to.deep.equal(draftWithParent); |
| 73 | + expect(response).to.deep.equal(draftResponse); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should get a draft message', async () => { |
| 77 | + const response = await channel.getDraft(parentID); |
| 78 | + |
| 79 | + expect(channel.getDraft.calledOnce).to.be.true; |
| 80 | + expect(channel.getDraft.firstCall.args[0]).to.deep.equal(parentID); |
| 81 | + expect(response).to.deep.equal(draftResponse); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should get a draft message with parent ID', async () => { |
| 85 | + const response = await channel.getDraft(parentID); |
| 86 | + |
| 87 | + expect(channel.getDraft.calledOnce).to.be.true; |
| 88 | + expect(channel.getDraft.firstCall.args[0]).to.deep.equal(parentID); |
| 89 | + expect(response).to.deep.equal(draftResponse); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should delete a draft message', async () => { |
| 93 | + await channel.deleteDraft(); |
| 94 | + |
| 95 | + expect(channel.deleteDraft.calledOnce).to.be.true; |
| 96 | + expect(channel.deleteDraft.firstCall.args[0]).to.be.undefined; |
| 97 | + }); |
| 98 | + |
| 99 | + it('should delete a draft message with parent ID', async () => { |
| 100 | + await channel.deleteDraft(parentID); |
| 101 | + |
| 102 | + expect(channel.deleteDraft.calledOnce).to.be.true; |
| 103 | + expect(channel.deleteDraft.firstCall.args[0]).to.deep.equal(parentID); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should query drafts', async () => { |
| 107 | + const queryOptions = { |
| 108 | + filter: { created_at: { $gt: '2023-01-01T00:00:00Z' } }, |
| 109 | + limit: 10, |
| 110 | + }; |
| 111 | + |
| 112 | + const queryResponse = { |
| 113 | + drafts: [draftResponse.draft, { ...draftResponse.draft, channel_cid: 'messaging:other-channel' }], |
| 114 | + next: 'next-page-token', |
| 115 | + }; |
| 116 | + client.queryDrafts.resolves(queryResponse); |
| 117 | + |
| 118 | + const response = await client.queryDrafts(queryOptions); |
| 119 | + |
| 120 | + expect(client.queryDrafts.calledOnce).to.be.true; |
| 121 | + expect(client.queryDrafts.firstCall.args[0]).to.deep.equal(queryOptions); |
| 122 | + expect(response).to.deep.equal(queryResponse); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should query drafts with default options', async () => { |
| 126 | + const queryResponse = { |
| 127 | + drafts: [draftResponse.draft], |
| 128 | + }; |
| 129 | + client.queryDrafts.resolves(queryResponse); |
| 130 | + |
| 131 | + const response = await client.queryDrafts(); |
| 132 | + expect(client.queryDrafts.calledOnce).to.be.true; |
| 133 | + expect(client.queryDrafts.firstCall.args[0]).to.be.undefined; |
| 134 | + expect(response).to.deep.equal(queryResponse); |
| 135 | + }); |
| 136 | +}); |
0 commit comments