|
| 1 | +// |
| 2 | +// Copyright Β© 2024 Stream.io Inc. All rights reserved. |
| 3 | +// |
| 4 | + |
| 5 | +import SnapshotTesting |
| 6 | +@testable import StreamChat |
| 7 | +@testable import StreamChatSwiftUI |
| 8 | +@testable import StreamChatTestTools |
| 9 | +import StreamSwiftTestHelpers |
| 10 | +import XCTest |
| 11 | + |
| 12 | +final class ChatThreadListItemView_Tests: StreamChatTestCase { |
| 13 | + var mockThread: ChatThread! |
| 14 | + |
| 15 | + var mockYoda = ChatUser.mock(id: .unique, name: "Yoda", imageURL: nil) |
| 16 | + var currentUser: ChatUser! |
| 17 | + |
| 18 | + override func setUp() { |
| 19 | + super.setUp() |
| 20 | + let circleImage = UIImage.circleImage |
| 21 | + streamChat?.utils.channelHeaderLoader.placeholder1 = circleImage |
| 22 | + streamChat?.utils.channelHeaderLoader.placeholder2 = circleImage |
| 23 | + streamChat?.utils.channelHeaderLoader.placeholder3 = circleImage |
| 24 | + streamChat?.utils.channelHeaderLoader.placeholder4 = circleImage |
| 25 | + |
| 26 | + currentUser = ChatUser.mock(id: StreamChatTestCase.currentUserId, name: "Vader", imageURL: nil) |
| 27 | + |
| 28 | + mockThread = .mock( |
| 29 | + parentMessage: .mock(text: "Parent Message", author: mockYoda), |
| 30 | + channel: .mock(cid: .unique, name: "Star Wars Channel"), |
| 31 | + createdBy: currentUser, |
| 32 | + replyCount: 3, |
| 33 | + participantCount: 2, |
| 34 | + threadParticipants: [ |
| 35 | + .mock(user: mockYoda), |
| 36 | + .mock(user: currentUser) |
| 37 | + ], |
| 38 | + lastMessageAt: .unique, |
| 39 | + createdAt: .unique, |
| 40 | + updatedAt: .unique, |
| 41 | + title: nil, |
| 42 | + latestReplies: [ |
| 43 | + .mock(text: "First Message", author: mockYoda), |
| 44 | + .mock(text: "Second Message", author: currentUser), |
| 45 | + .mock(text: "Third Message", author: mockYoda) |
| 46 | + ], |
| 47 | + reads: [], |
| 48 | + extraData: [:] |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + func test_threadListItem_default() throws { |
| 53 | + let view = ChatThreadListItem(thread: mockThread) |
| 54 | + .frame(width: defaultScreenSize.width) |
| 55 | + |
| 56 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 57 | + } |
| 58 | + |
| 59 | + func test_threadListItem_withUnreads() throws { |
| 60 | + let thread = mockThread |
| 61 | + .with(reads: [.mock(user: currentUser, lastReadAt: .unique, unreadMessagesCount: 4)]) |
| 62 | + |
| 63 | + let view = ChatThreadListItem(thread: thread) |
| 64 | + .frame(width: defaultScreenSize.width) |
| 65 | + |
| 66 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 67 | + } |
| 68 | + |
| 69 | + func test_threadListItem_withTitle() throws { |
| 70 | + let thread = mockThread |
| 71 | + .with(title: "Thread title") |
| 72 | + |
| 73 | + let view = ChatThreadListItem(thread: thread) |
| 74 | + .frame(width: defaultScreenSize.width) |
| 75 | + |
| 76 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 77 | + } |
| 78 | + |
| 79 | + func test_threadListItem_withParentMessageDeleted() throws { |
| 80 | + let thread = mockThread |
| 81 | + .with(parentMessage: .mock(text: "Parent Message", deletedAt: .unique)) |
| 82 | + |
| 83 | + let view = ChatThreadListItem(thread: thread) |
| 84 | + .frame(width: defaultScreenSize.width) |
| 85 | + |
| 86 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 87 | + } |
| 88 | + |
| 89 | + func test_threadListItem_withLastReplyDeleted() throws { |
| 90 | + let thread = mockThread |
| 91 | + .with(latestReplies: [ |
| 92 | + .mock(text: "First Message", author: mockYoda), |
| 93 | + .mock(text: "Second Message", author: currentUser), |
| 94 | + .mock(text: "Third Message", author: mockYoda, deletedAt: .unique) |
| 95 | + ]) |
| 96 | + |
| 97 | + let view = ChatThreadListItem(thread: thread) |
| 98 | + .frame(width: defaultScreenSize.width) |
| 99 | + |
| 100 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 101 | + } |
| 102 | + |
| 103 | + func test_threadListItem_whenAttachments() throws { |
| 104 | + let thread = mockThread |
| 105 | + .with( |
| 106 | + parentMessage: .mock(text: "", attachments: [.dummy(type: .giphy)]), |
| 107 | + latestReplies: [ |
| 108 | + .mock(text: "", author: mockYoda, attachments: [.dummy(type: .audio)]) |
| 109 | + ] |
| 110 | + ) |
| 111 | + |
| 112 | + let view = ChatThreadListItem(thread: thread) |
| 113 | + .frame(width: defaultScreenSize.width) |
| 114 | + |
| 115 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 116 | + } |
| 117 | + |
| 118 | + func test_threadListItem_whenAttachmentIsPoll() throws { |
| 119 | + let thread = mockThread |
| 120 | + .with( |
| 121 | + parentMessage: .mock(text: "", poll: .mock(name: "Who is better?")), |
| 122 | + latestReplies: [ |
| 123 | + .mock(text: "", author: mockYoda, poll: .mock(name: "Who is worse?")) |
| 124 | + ] |
| 125 | + ) |
| 126 | + |
| 127 | + let view = ChatThreadListItem(thread: thread) |
| 128 | + .frame(width: defaultScreenSize.width) |
| 129 | + |
| 130 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 131 | + } |
| 132 | +} |
0 commit comments