Skip to content

Commit

Permalink
Inject currentDate, locale, and calendar to make tests reliable in an…
Browse files Browse the repository at this point in the history
…y locale
  • Loading branch information
ullas-jain committed Nov 21, 2023
1 parent fd84288 commit bd099f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit bd099f5

Please sign in to comment.