Permalink
Browse files

Show most recent message first.

  • Loading branch information...
1 parent 04c818b commit d4a664b6e138b11b53ff0879c5a728b4d36109fa @lazyatom lazyatom committed with chrisroos May 18, 2012
@@ -42,7 +42,7 @@ def merge(other_conversation)
end
def messages
- message_ids.map { |id| @message_repository.find_by_message_id(id) }
+ message_ids.map { |id| @message_repository.find_by_message_id(id) }.sort_by(&:date).reverse
end
def ==(other_conversation)
@@ -201,7 +201,7 @@ class ConversationRepository::ConversationIndexTest < ActiveSupport::TestCase
@message_repository.expects(:find_by_message_id).with('message-2').returns(messages[1])
@message_repository.expects(:find_by_message_id).with('message-3').returns(messages[2])
- assert_equal messages, conversation.messages
+ assert_equal messages.reverse, conversation.messages
end
test "returns nil if the conversation identifier doesn't exist" do
@@ -4,8 +4,23 @@ class ConversationRepository
class ConversationTest < ActiveSupport::TestCase
test "uses the identifier when involved in generating URLs" do
identifier = "abc123"
- c = Conversation.new(conversation_record_stub('record', identifier: identifier))
- assert_equal identifier, c.to_param
+ conversation = Conversation.new(conversation_record_stub('record', identifier: identifier))
+ assert_equal identifier, conversation.to_param
+ end
+
+ test "returns messages ordered by most recent first" do
+ newest_message = message_stub('newest message', message_id: '1', date: 1.minute.ago)
+ oldest_message = message_stub('oldest message', message_id: '2', date: 10.days.ago)
+ oldish_message = message_stub('oldish message', message_id: '3', date: 3.days.ago)
+ messages = [newest_message, oldest_message, oldish_message]
+ message_repository = stub('message repository')
+ message_repository.stubs(:find_by_message_id).returns(*messages)
+
+ record = conversation_record_stub('record', message_ids: ['1', '2', '3'])
+
+ conversation = Conversation.new(record, message_repository)
+
+ assert_equal [newest_message, oldish_message, oldest_message], conversation.messages
end
end
end

0 comments on commit d4a664b

Please sign in to comment.