|
| 1 | +// |
| 2 | +// Copyright © 2023 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 XCTest |
| 10 | + |
| 11 | +final class ChatChannelListItemView_Tests: StreamChatTestCase { |
| 12 | + |
| 13 | + func test_channelListItem_audioMessage() throws { |
| 14 | + // Given |
| 15 | + let message = try mockAudioMessage(text: "Audio", isSentByCurrentUser: true) |
| 16 | + let channel = ChatChannel.mock(cid: .unique, latestMessages: [message]) |
| 17 | + |
| 18 | + // When |
| 19 | + let view = ChatChannelListItem( |
| 20 | + channel: channel, |
| 21 | + channelName: "Test", |
| 22 | + avatar: .circleImage, |
| 23 | + onlineIndicatorShown: true, |
| 24 | + onItemTap: { _ in } |
| 25 | + ) |
| 26 | + .frame(width: defaultScreenSize.width) |
| 27 | + |
| 28 | + // Then |
| 29 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: snapshotPrecision)) |
| 30 | + } |
| 31 | + |
| 32 | + func test_channelListItem_imageMessage() throws { |
| 33 | + // Given |
| 34 | + let message = try mockImageMessage(text: "Image", isSentByCurrentUser: true) |
| 35 | + let channel = ChatChannel.mock(cid: .unique, latestMessages: [message]) |
| 36 | + |
| 37 | + // When |
| 38 | + let view = ChatChannelListItem( |
| 39 | + channel: channel, |
| 40 | + channelName: "Test", |
| 41 | + avatar: .circleImage, |
| 42 | + onlineIndicatorShown: true, |
| 43 | + onItemTap: { _ in } |
| 44 | + ) |
| 45 | + .frame(width: defaultScreenSize.width) |
| 46 | + |
| 47 | + // Then |
| 48 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: snapshotPrecision)) |
| 49 | + } |
| 50 | + |
| 51 | + func test_channelListItem_videoMessage() throws { |
| 52 | + // Given |
| 53 | + let message = try mockVideoMessage(text: "Video", isSentByCurrentUser: true) |
| 54 | + let channel = ChatChannel.mock(cid: .unique, latestMessages: [message]) |
| 55 | + |
| 56 | + // When |
| 57 | + let view = ChatChannelListItem( |
| 58 | + channel: channel, |
| 59 | + channelName: "Test", |
| 60 | + avatar: .circleImage, |
| 61 | + onlineIndicatorShown: true, |
| 62 | + onItemTap: { _ in } |
| 63 | + ) |
| 64 | + .frame(width: defaultScreenSize.width) |
| 65 | + |
| 66 | + // Then |
| 67 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: snapshotPrecision)) |
| 68 | + } |
| 69 | + |
| 70 | + func test_channelListItem_fileMessage() throws { |
| 71 | + // Given |
| 72 | + let message = try mockFileMessage(title: "Filename", text: "File", isSentByCurrentUser: true) |
| 73 | + let channel = ChatChannel.mock(cid: .unique, latestMessages: [message]) |
| 74 | + |
| 75 | + // When |
| 76 | + let view = ChatChannelListItem( |
| 77 | + channel: channel, |
| 78 | + channelName: "Test", |
| 79 | + avatar: .circleImage, |
| 80 | + onlineIndicatorShown: true, |
| 81 | + onItemTap: { _ in } |
| 82 | + ) |
| 83 | + .frame(width: defaultScreenSize.width) |
| 84 | + |
| 85 | + // Then |
| 86 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: snapshotPrecision)) |
| 87 | + } |
| 88 | + |
| 89 | + func test_channelListItem_giphyMessage() throws { |
| 90 | + // Given |
| 91 | + let message = try mockGiphyMessage(text: "Giphy", isSentByCurrentUser: true) |
| 92 | + let channel = ChatChannel.mock(cid: .unique, latestMessages: [message]) |
| 93 | + |
| 94 | + // When |
| 95 | + let view = ChatChannelListItem( |
| 96 | + channel: channel, |
| 97 | + channelName: "Test", |
| 98 | + avatar: .circleImage, |
| 99 | + onlineIndicatorShown: true, |
| 100 | + onItemTap: { _ in } |
| 101 | + ) |
| 102 | + .frame(width: defaultScreenSize.width) |
| 103 | + |
| 104 | + // Then |
| 105 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: snapshotPrecision)) |
| 106 | + } |
| 107 | + |
| 108 | + //MARK: - private |
| 109 | + |
| 110 | + private func mockAudioMessage(text: String, isSentByCurrentUser: Bool) throws -> ChatMessage { |
| 111 | + .mock( |
| 112 | + id: .unique, |
| 113 | + cid: .unique, |
| 114 | + text: text, |
| 115 | + type: .regular, |
| 116 | + author: .mock(id: "user", name: "User"), |
| 117 | + createdAt: Date(timeIntervalSince1970: 100), |
| 118 | + attachments: [ |
| 119 | + .dummy( |
| 120 | + type: .audio, |
| 121 | + payload: try JSONEncoder().encode(AudioAttachmentPayload( |
| 122 | + title: "Some Audio", |
| 123 | + audioRemoteURL: URL(string: "url")!, |
| 124 | + file: .init(type: .mp3, size: 123, mimeType: nil), |
| 125 | + extraData: nil |
| 126 | + )) |
| 127 | + ) |
| 128 | + ], |
| 129 | + localState: nil, |
| 130 | + isSentByCurrentUser: isSentByCurrentUser |
| 131 | + ) |
| 132 | + } |
| 133 | + |
| 134 | + private func mockImageMessage(text: String, isSentByCurrentUser: Bool) throws -> ChatMessage { |
| 135 | + .mock( |
| 136 | + id: .unique, |
| 137 | + cid: .unique, |
| 138 | + text: text, |
| 139 | + type: .regular, |
| 140 | + author: .mock(id: "user", name: "User"), |
| 141 | + createdAt: Date(timeIntervalSince1970: 100), |
| 142 | + attachments: [ |
| 143 | + .dummy( |
| 144 | + type: .image, |
| 145 | + payload: try JSONEncoder().encode(ImageAttachmentPayload( |
| 146 | + title: "Test", |
| 147 | + imageRemoteURL: URL(string: "Url")! |
| 148 | + )) |
| 149 | + ) |
| 150 | + ], |
| 151 | + localState: nil, |
| 152 | + isSentByCurrentUser: isSentByCurrentUser |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + private func mockVideoMessage(text: String, isSentByCurrentUser: Bool) throws -> ChatMessage { |
| 157 | + .mock( |
| 158 | + id: .unique, |
| 159 | + cid: .unique, |
| 160 | + text: text, |
| 161 | + type: .regular, |
| 162 | + author: .mock(id: "user", name: "User"), |
| 163 | + createdAt: Date(timeIntervalSince1970: 100), |
| 164 | + attachments: [ |
| 165 | + .dummy( |
| 166 | + type: .video, |
| 167 | + payload: try JSONEncoder().encode(VideoAttachmentPayload( |
| 168 | + title: "Test", |
| 169 | + videoRemoteURL: URL(string: "Url")!, |
| 170 | + file: .init(type: .mp4, size: 123, mimeType: nil), |
| 171 | + extraData: nil |
| 172 | + )) |
| 173 | + ) |
| 174 | + ], |
| 175 | + localState: nil, |
| 176 | + isSentByCurrentUser: isSentByCurrentUser |
| 177 | + ) |
| 178 | + } |
| 179 | + |
| 180 | + private func mockFileMessage(title: String?, text: String, isSentByCurrentUser: Bool) throws -> ChatMessage { |
| 181 | + .mock( |
| 182 | + id: .unique, |
| 183 | + cid: .unique, |
| 184 | + text: text, |
| 185 | + type: .regular, |
| 186 | + author: .mock(id: "user", name: "User"), |
| 187 | + createdAt: Date(timeIntervalSince1970: 100), |
| 188 | + attachments: [ |
| 189 | + .dummy( |
| 190 | + type: .file, |
| 191 | + payload: try JSONEncoder().encode(FileAttachmentPayload( |
| 192 | + title: title, |
| 193 | + assetRemoteURL: URL(string: "Url")!, |
| 194 | + file: .init(type: .pdf, size: 123, mimeType: nil), |
| 195 | + extraData: nil |
| 196 | + )) |
| 197 | + ) |
| 198 | + ], |
| 199 | + localState: nil, |
| 200 | + isSentByCurrentUser: isSentByCurrentUser |
| 201 | + ) |
| 202 | + } |
| 203 | + |
| 204 | + private func mockGiphyMessage(text: String, isSentByCurrentUser: Bool) throws -> ChatMessage { |
| 205 | + .mock( |
| 206 | + id: .unique, |
| 207 | + cid: .unique, |
| 208 | + text: text, |
| 209 | + type: .regular, |
| 210 | + author: .mock(id: "user", name: "User"), |
| 211 | + createdAt: Date(timeIntervalSince1970: 100), |
| 212 | + attachments: [ |
| 213 | + .dummy( |
| 214 | + type: .giphy, |
| 215 | + payload: try JSONEncoder().encode(GiphyAttachmentPayload( |
| 216 | + title: "Test", |
| 217 | + previewURL: URL(string: "Url")! |
| 218 | + )) |
| 219 | + ) |
| 220 | + ], |
| 221 | + localState: nil, |
| 222 | + isSentByCurrentUser: isSentByCurrentUser |
| 223 | + ) |
| 224 | + } |
| 225 | +} |
0 commit comments