Skip to content

Commit

Permalink
Add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nuno-vieira committed Mar 20, 2024
1 parent 85f63d8 commit 86cc490
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/StreamChatUI/Composer/ComposerVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public enum AttachmentValidationError: Error {
/// The number of attachments reached the limit.
case maxAttachmentsCountPerMessageExceeded(limit: Int)

fileprivate static var fileSizeMaxLimitFallback: Int64 = 100 * 1024 * 1024
internal static var fileSizeMaxLimitFallback: Int64 = 100 * 1024 * 1024
}

public struct LocalAttachmentInfoKey: Hashable, Equatable, RawRepresentable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ final class ChatClient_Mock: ChatClient {
@Atomic var completeTokenWaiters_called = false
@Atomic var completeTokenWaiters_token: Token?

var mockedAppSettings: AppSettings?

override var appSettings: AppSettings? {
mockedAppSettings
}

var mockedEventNotificationCenter: EventNotificationCenter_Mock? = nil

override var eventNotificationCenter: EventNotificationCenter {
Expand Down Expand Up @@ -256,3 +262,39 @@ extension ChatClient {
)
}
}

extension AppSettings {
static func mock(
name: String = "Stream iOS",
fileUploadConfig: UploadConfig? = nil,
imageUploadConfig: UploadConfig? = nil,
autoTranslationEnabled: Bool = false,
asyncUrlEnrichEnabled: Bool = false
) -> AppSettings {
.init(
name: name,
fileUploadConfig: fileUploadConfig ?? .mock(),
imageUploadConfig: imageUploadConfig ?? .mock(),
autoTranslationEnabled: autoTranslationEnabled,
asyncUrlEnrichEnabled: asyncUrlEnrichEnabled
)
}
}

extension AppSettings.UploadConfig {
static func mock(
allowedFileExtensions: [String] = [],
blockedFileExtensions: [String] = [],
allowedMimeTypes: [String] = [],
blockedMimeTypes: [String] = [],
sizeLimitInBytes: Int64? = nil
) -> AppSettings.UploadConfig {
.init(
allowedFileExtensions: allowedFileExtensions,
blockedFileExtensions: blockedFileExtensions,
allowedMimeTypes: allowedMimeTypes,
blockedMimeTypes: blockedMimeTypes,
sizeLimitInBytes: sizeLimitInBytes
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,55 @@ final class ComposerVC_Tests: XCTestCase {
XCTAssertEqual(composerVC.dismissLinkPreviewCallCount, 0)
}

// MARK: - maxAttachmentSize

func test_maxAttachmentSize_whenChannelControllerNotSet_thenReturnsDefaultFallbackLimit() {
composerVC.channelController = nil
XCTAssertEqual(composerVC.maxAttachmentSize(for: .file), AttachmentValidationError.fileSizeMaxLimitFallback)
}

func test_maxAttachmentSize_whenImageType_thenReturnsLimitFromImageUploadConfig() {
let expectedValue: Int64 = 50 * 1024 * 1024
let chatClient = ChatClient_Mock.mock
chatClient.mockedAppSettings = .mock(imageUploadConfig: .mock(
sizeLimitInBytes: expectedValue
))
composerVC.channelController = ChatChannelController_Mock.mock(chatClient: chatClient)

XCTAssertEqual(composerVC.maxAttachmentSize(for: .image), expectedValue)
}

func test_maxAttachmentSize_whenFileType_thenReturnsLimitFromFileUploadConfig() {
let expectedValue: Int64 = 50 * 1024 * 1024
let chatClient = ChatClient_Mock.mock
chatClient.mockedAppSettings = .mock(fileUploadConfig: .mock(
sizeLimitInBytes: expectedValue
))
composerVC.channelController = ChatChannelController_Mock.mock(chatClient: chatClient)

XCTAssertEqual(composerVC.maxAttachmentSize(for: .file), expectedValue)
}

func test_maxAttachmentSize_whenOtherType_thenReturnsLimitFromFileUploadConfig() {
let expectedValue: Int64 = 50 * 1024 * 1024
let chatClient = ChatClient_Mock.mock
chatClient.mockedAppSettings = .mock(fileUploadConfig: .mock(
sizeLimitInBytes: expectedValue
))
composerVC.channelController = ChatChannelController_Mock.mock(chatClient: chatClient)

XCTAssertEqual(composerVC.maxAttachmentSize(for: .video), expectedValue)
}

func test_maxAttachmentSize_whenSizeLimitNotDefined_thenReturnsLimitFromChatClientConfig() {
let expectedValue: Int64 = 50 * 1024 * 1024
var config = ChatClientConfig(apiKeyString: "sadsad")
config.maxAttachmentSize = expectedValue
composerVC.channelController = ChatChannelController_Mock.mock(chatClientConfig: config)

XCTAssertEqual(composerVC.maxAttachmentSize(for: .image), expectedValue)
}

// MARK: - audioPlayer

func test_audioPlayer_voiceRecordingAndAttachmentsVCGetTheSameInstance() {
Expand Down

0 comments on commit 86cc490

Please sign in to comment.