Skip to content
This repository has been archived by the owner on Aug 18, 2019. It is now read-only.

Commit

Permalink
add conversation preview specs
Browse files Browse the repository at this point in the history
  • Loading branch information
aguacongas committed Oct 23, 2016
1 parent c199202 commit be1f35b
Showing 1 changed file with 97 additions and 4 deletions.
101 changes: 97 additions & 4 deletions test/unit/components/conversation-preview.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ 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 = {
subscribe: (e, d) => {
if (e === ConversationSelected) {
conversationSelectedSubscriptionCallback = d;
conversationSelectedSubscription = {
dispose: () => {}
dispose: () => { }
};
return conversationSelectedSubscription;
}
if (e === MessageReceived) {
messageReceivedSubscriptionCallback = d;
messageReceivedSubscription = {
dispose: () => {}
dispose: () => { }
};
return messageReceivedSubscription;
}
Expand All @@ -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>();
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);
});
});
});
});

0 comments on commit be1f35b

Please sign in to comment.