Skip to content

Commit e3a800d

Browse files
committed
Add test coverage to show original text feature
1 parent aaa1274 commit e3a800d

5 files changed

+123
-6
lines changed

β€ŽStreamChatSwiftUITests/Tests/ChatChannel/MessageContainerView_Tests.swiftβ€Ž

Lines changed: 123 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import SnapshotTesting
66
@testable import StreamChat
77
@testable import StreamChatSwiftUI
8+
@testable import StreamChatTestTools
89
import StreamSwiftTestHelpers
910
import SwiftUI
1011
import XCTest
@@ -318,7 +319,7 @@ class MessageContainerView_Tests: StreamChatTestCase {
318319
.environment(\.channelTranslationLanguage, .spanish)
319320

320321
// Then
321-
AssertSnapshot(view, size: CGSize(width: 375, height: 200))
322+
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
322323
}
323324

324325
func test_translatedText_myMessageIsNotTranslated_snapshot() {
@@ -342,13 +343,120 @@ class MessageContainerView_Tests: StreamChatTestCase {
342343
AssertSnapshot(view, size: CGSize(width: 375, height: 200))
343344
}
344345

346+
func test_translatedText_showOriginalTranslatedButtonEnabled_originalTextShown_snapshot() {
347+
// Given
348+
let message = ChatMessage.mock(
349+
id: .unique,
350+
cid: .unique,
351+
text: "Hello",
352+
author: .mock(id: .unique),
353+
translations: [
354+
.spanish: "Hola"
355+
]
356+
)
357+
let utils = Utils(
358+
dateFormatter: EmptyDateFormatter(),
359+
messageListConfig: .init(
360+
messageDisplayOptions: MessageDisplayOptions(showOriginalTranslatedButton: true)
361+
)
362+
)
363+
streamChat = StreamChat(chatClient: chatClient, utils: utils)
364+
365+
// When
366+
let messageViewModel = MessageViewModel_Mock(
367+
message: message,
368+
channel: .mock(
369+
cid: .unique,
370+
membership: .mock(id: .unique, language: .spanish)
371+
)
372+
)
373+
messageViewModel.mockOriginalTextShown = true
374+
let view = testMessageViewContainer(message: message, messageViewModel: messageViewModel)
375+
.environment(\.channelTranslationLanguage, .spanish)
376+
377+
// Then
378+
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
379+
}
380+
381+
func test_translatedText_showOriginalTranslatedButtonEnabled_translatedTextShown_snapshot() {
382+
// Given
383+
let message = ChatMessage.mock(
384+
id: .unique,
385+
cid: .unique,
386+
text: "Hello",
387+
author: .mock(id: .unique),
388+
translations: [
389+
.spanish: "Hola"
390+
]
391+
)
392+
let utils = Utils(
393+
dateFormatter: EmptyDateFormatter(),
394+
messageListConfig: .init(
395+
messageDisplayOptions: MessageDisplayOptions(showOriginalTranslatedButton: true)
396+
)
397+
)
398+
streamChat = StreamChat(chatClient: chatClient, utils: utils)
399+
400+
// When
401+
let messageViewModel = MessageViewModel_Mock(
402+
message: message,
403+
channel: .mock(
404+
cid: .unique,
405+
membership: .mock(id: .unique, language: .spanish)
406+
)
407+
)
408+
messageViewModel.mockOriginalTextShown = false
409+
let view = testMessageViewContainer(message: message, messageViewModel: messageViewModel)
410+
.environment(\.channelTranslationLanguage, .spanish)
411+
412+
// Then
413+
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
414+
}
415+
416+
func test_translatedText_showOriginalTranslatedButtonDisabled_translatedTextShown_snapshot() {
417+
// Given
418+
let message = ChatMessage.mock(
419+
id: .unique,
420+
cid: .unique,
421+
text: "Hello",
422+
author: .mock(id: .unique),
423+
translations: [
424+
.spanish: "Hola"
425+
]
426+
)
427+
let utils = Utils(
428+
dateFormatter: EmptyDateFormatter(),
429+
messageListConfig: .init(
430+
messageDisplayOptions: MessageDisplayOptions(showOriginalTranslatedButton: false)
431+
)
432+
)
433+
streamChat = StreamChat(chatClient: chatClient, utils: utils)
434+
435+
// When
436+
let messageViewModel = MessageViewModel_Mock(
437+
message: message,
438+
channel: .mock(
439+
cid: .unique,
440+
membership: .mock(id: .unique, language: .spanish)
441+
)
442+
)
443+
let view = testMessageViewContainer(message: message, messageViewModel: messageViewModel)
444+
.environment(\.channelTranslationLanguage, .spanish)
445+
446+
// Then
447+
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
448+
}
449+
345450
// MARK: - private
346451

347-
func testMessageViewContainer(message: ChatMessage) -> some View {
348-
let channel = ChatChannel.mockDMChannel()
349-
return MessageContainerView(
452+
func testMessageViewContainer(
453+
message: ChatMessage,
454+
channel: ChatChannel? = nil,
455+
messageViewModel: MessageViewModel? = nil
456+
) -> some View {
457+
MessageContainerView(
350458
factory: DefaultViewFactory.shared,
351-
channel: .mockDMChannel(),
459+
channel: channel ?? .mockDMChannel(),
352460
message: message,
353461
width: defaultScreenSize.width,
354462
showsAllInfo: true,
@@ -358,7 +466,16 @@ class MessageContainerView_Tests: StreamChatTestCase {
358466
quotedMessage: .constant(nil),
359467
onLongPress: { _ in }
360468
)
361-
.environmentObject(MessageViewModel(message: message, channel: channel))
469+
.environmentObject(ChatChannelViewModel(channelController: ChatChannelController_Mock.mock()))
470+
.environmentObject(messageViewModel ?? MessageViewModel(message: message, channel: channel))
362471
.frame(width: 375, height: 200)
363472
}
364473
}
474+
475+
class MessageViewModel_Mock: MessageViewModel {
476+
var mockOriginalTextShown: Bool = false
477+
478+
override var originalTextShown: Bool {
479+
mockOriginalTextShown
480+
}
481+
}
Loading

0 commit comments

Comments
Β (0)