diff --git a/EssentialFeed/Image Comments Presentation/ImageCommentsPresenter.swift b/EssentialFeed/Image Comments Presentation/ImageCommentsPresenter.swift index 559358e9..02f0eda1 100644 --- a/EssentialFeed/Image Comments Presentation/ImageCommentsPresenter.swift +++ b/EssentialFeed/Image Comments Presentation/ImageCommentsPresenter.swift @@ -31,13 +31,20 @@ public final class ImageCommentsPresenter { comment: "Title for the image comments view") } - public static func map(_ comments: [ImageComment]) -> ImageCommentsViewModel { + public static func map( + _ comments: [ImageComment], + currentDate: Date = Date(), + calendar: Calendar = .current, + locale: Locale = .current + ) -> ImageCommentsViewModel { let formatter = RelativeDateTimeFormatter() + formatter.calendar = calendar + formatter.locale = locale return ImageCommentsViewModel(comments: comments.map { comment in ImageCommentViewModel( message: comment.message, - date: formatter.localizedString(for: comment.createdAt, relativeTo: Date()), + date: formatter.localizedString(for: comment.createdAt, relativeTo: currentDate), username: comment.username ) }) diff --git a/EssentialFeedTests/Image Comments Presentation/ImageCommentsPresenterTests.swift b/EssentialFeedTests/Image Comments Presentation/ImageCommentsPresenterTests.swift index d9a91c81..0aa07d36 100644 --- a/EssentialFeedTests/Image Comments Presentation/ImageCommentsPresenterTests.swift +++ b/EssentialFeedTests/Image Comments Presentation/ImageCommentsPresenterTests.swift @@ -15,6 +15,8 @@ class ImageCommentsPresenterTests: XCTestCase { func test_map_createsViewModels() { let now = Date() + let calendar = Calendar(identifier: .gregorian) + let locale = Locale(identifier: "en_US_POSIX") let comments = [ ImageComment( @@ -31,7 +33,12 @@ class ImageCommentsPresenterTests: XCTestCase { ), ] - let viewModel = ImageCommentsPresenter.map(comments) + let viewModel = ImageCommentsPresenter.map( + comments, + currentDate: now, + calendar: calendar, + locale: locale + ) XCTAssertEqual(viewModel.comments, [ ImageCommentViewModel(