diff --git a/test/unit/components/conversation-preview.spec.ts b/test/unit/components/conversation-preview.spec.ts index 2f54009..554dce2 100644 --- a/test/unit/components/conversation-preview.spec.ts +++ b/test/unit/components/conversation-preview.spec.ts @@ -18,12 +18,12 @@ describe('conversation preview component specs', () => { let conversationSelectedSubscription: Subscription; let messageReceivedSubscription: Subscription; - let conversationSelectedSubscriptionCallback; + let conversationSelectedSubscriptionCallback; let messageReceivedSubscriptionCallback; beforeEach(() => { service = { - showConversation: (c, r) => {} + showConversation: (c, r) => { } } as ConversationService; ea = { @@ -31,14 +31,14 @@ describe('conversation preview component specs', () => { if (e === ConversationSelected) { conversationSelectedSubscriptionCallback = d; conversationSelectedSubscription = { - dispose: () => {} + dispose: () => { } }; return conversationSelectedSubscription; } if (e === MessageReceived) { messageReceivedSubscriptionCallback = d; messageReceivedSubscription = { - dispose: () => {} + dispose: () => { } }; return messageReceivedSubscription; } @@ -60,4 +60,97 @@ describe('conversation preview component specs', () => { // verify expect(service.showConversation).toHaveBeenCalledWith(component.conversation, router); }); + + describe('attached specs', () => { + let message: Message; + let conversation: Conversation; + + beforeEach(() => { + conversation = new Conversation(); + component.conversation = conversation; + conversation.messages = new Array(); + message = new Message(); + conversation.messages.push(message); + }); + + it('attached should subscribe to ConversationSelected and MessageReceived events', () => { + // act + component.attached(); + + // verify + expect(conversationSelectedSubscription).toBeDefined(); + expect(conversationSelectedSubscriptionCallback).toBeDefined(); + expect(messageReceivedSubscription).toBeDefined(); + expect(messageReceivedSubscriptionCallback).toBeDefined(); + }); + + it('attached should set last message', () => { + // prepare + message.text = 'test'; + + // act + component.attached(); + + // verify + expect(component.lastMessage).toBe(message.text); + }); + + describe('subscriptions specs', () => { + beforeEach(() => { + component.attached(); + }); + + it('conversation selected should set isSelect to true when conversation ids match', () => { + // prepare + conversation.id = 'test'; + component.isSelected = false; + + // act + conversationSelectedSubscriptionCallback(new ConversationSelected(conversation)); + + // verify + expect(component.isSelected).toBe(true); + }); + + it("conversation selected should set isSelect to false when conversation ids doesn't match", () => { + // prepare + conversation.id = 'test'; + component.isSelected = true; + + // act + conversationSelectedSubscriptionCallback(new ConversationSelected(new Conversation())); + + // verify@ + expect(component.isSelected).toBe(false); + }); + + it("message received should add message to the conversation and set lastMessage", () => { + // prepare + conversation.id = 'test'; + let m = new Message(); + m.conversationId = conversation.id; + m.text = 'message received'; + + // act + messageReceivedSubscriptionCallback(new MessageReceived(m)); + + // verify@ + expect(component.conversation.messages[0]).toBe(m); + expect(component.lastMessage).toBe(m.text); + }); + + it("detached should dispose subscriptions", () => { + // prepare + spyOn(messageReceivedSubscription, 'dispose'); + spyOn(conversationSelectedSubscription, 'dispose'); + + // act + component.detached(); + + // verify@ + expect(messageReceivedSubscription.dispose).toHaveBeenCalledTimes(1); + expect(conversationSelectedSubscription.dispose).toHaveBeenCalledTimes(1); + }); + }); + }); }); \ No newline at end of file