|
| 1 | +import { afterEach, beforeEach, describe } from 'mocha'; |
| 2 | +import Sinon from 'sinon'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import { Data } from '../../data/data'; |
| 5 | +import { ConversationModel } from '../../models/conversation'; |
| 6 | +import { channels } from '../../data/channels'; |
| 7 | + |
| 8 | +describe('data', () => { |
| 9 | + afterEach(() => { |
| 10 | + Sinon.restore(); |
| 11 | + }); |
| 12 | + |
| 13 | + describe('getAllConversations', () => { |
| 14 | + let getAllConversationsStub: Record<string, any>; |
| 15 | + beforeEach(() => { |
| 16 | + channels.getAllConversations = () => {}; |
| 17 | + |
| 18 | + getAllConversationsStub = Sinon.stub(channels, 'getAllConversations'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns empty array when channels.getAllConversations yields invalid conversations', async () => { |
| 22 | + getAllConversationsStub.resolves([{} as any, { id: 123 } as any]); |
| 23 | + |
| 24 | + const conversations = await Data.getAllConversations(); |
| 25 | + |
| 26 | + expect(conversations).to.be.an('array'); |
| 27 | + expect(conversations).to.deep.equal([]); |
| 28 | + expect(getAllConversationsStub.calledOnce).to.eq(true); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns array of ConversationModel for valid conversations', async () => { |
| 32 | + const attrs = [{ id: 'abc' } as any, { id: 'def' } as any]; |
| 33 | + const stub = getAllConversationsStub.resolves(attrs); |
| 34 | + |
| 35 | + const conversations = await Data.getAllConversations(); |
| 36 | + |
| 37 | + expect(conversations).to.be.an('array'); |
| 38 | + expect(conversations).to.have.length(2); |
| 39 | + expect(conversations[0]).to.be.instanceOf(ConversationModel); |
| 40 | + expect(conversations[1]).to.be.instanceOf(ConversationModel); |
| 41 | + expect(stub.calledOnce).to.eq(true); |
| 42 | + }); |
| 43 | + |
| 44 | + it('filters out invalid and keeps valid when mixed', async () => { |
| 45 | + const stub = getAllConversationsStub.resolves([ |
| 46 | + { id: 'ok' } as any, |
| 47 | + {} as any, |
| 48 | + { id: null } as any, |
| 49 | + { id: 'ok2' } as any, |
| 50 | + ]); |
| 51 | + |
| 52 | + const conversations = await Data.getAllConversations(); |
| 53 | + |
| 54 | + expect(conversations).to.be.an('array'); |
| 55 | + expect(conversations).to.have.length(2); |
| 56 | + expect(conversations.every(c => c instanceof ConversationModel)).to.eq(true); |
| 57 | + expect(stub.calledOnce).to.eq(true); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
0 commit comments