-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sdk differentiate custom card and usual response card based on metadata. If the type is single choice card and metadata is nil, SDK should draw a message as a response card. If the type is single choice card and metadata is not nil, SDK should pass metadata to renderer and render custom card. MOB-2342
- Loading branch information
1 parent
3d9eba0
commit bcd6e44
Showing
4 changed files
with
103 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import GliaCoreSDK | ||
import XCTest | ||
|
||
@testable import GliaWidgets | ||
|
||
final class ChatMessageTests: XCTestCase { | ||
|
||
func testCardType__singleChoiceWithoutMetadata() throws { | ||
let msg = ChatMessage.mock( | ||
attachment: .mock(type: .singleChoice, files: nil, imageUrl: nil, options: nil), | ||
metadata: nil | ||
) | ||
XCTAssertEqual(msg.cardType, .choiceCard) | ||
} | ||
|
||
func testCardType__singleChoiceWithMetadata() throws { | ||
let metadataDecodingContainer = try CoreSdkMessageMetadataContainer( | ||
jsonData: "{\"html\": \"Hello\"}".data(using: .utf8)! | ||
).container | ||
let msg = ChatMessage.mock( | ||
attachment: .mock(type: .singleChoice, files: nil, imageUrl: nil, options: nil), | ||
metadata: MessageMetadata(container: metadataDecodingContainer) | ||
) | ||
XCTAssertEqual(msg.cardType, .customCard) | ||
} | ||
} | ||
|
||
extension ChatMessage { | ||
static func mock( | ||
id: String = "mocked-message-id", | ||
queueId: String? = "queue-id", | ||
operator: ChatOperator? = .init(name: "XCTest Operator", pictureUrl: nil), | ||
sender: ChatMessageSender = .`operator`, | ||
content: String = "Hello unit test!", | ||
attachment: ChatAttachment? = nil, | ||
downloads: [FileDownload] = [], | ||
metadata: MessageMetadata? = nil | ||
) -> ChatMessage { | ||
ChatMessage( | ||
id: id, | ||
queueID: queueId, | ||
operator: `operator`, | ||
sender: sender, | ||
content: content, | ||
attachment: attachment, | ||
downloads: downloads, | ||
metadata: metadata | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Foundation | ||
import GliaCoreSDK | ||
|
||
/// Defines wrapper structure for getting decoding container. | ||
/// This container can be used when message metadata is needed in | ||
/// tests. | ||
struct CoreSdkMessageMetadataContainer: Decodable { | ||
let container: KeyedDecodingContainer<GliaCoreSDK.Message.Metadata.CodingKeys> | ||
|
||
init(from decoder: Decoder) throws { | ||
self.container = try decoder.container(keyedBy: GliaCoreSDK.Message.Metadata.CodingKeys.self) | ||
} | ||
|
||
/// Creates instance with decoding container inside. | ||
/// This initializer can be used for created mocked Metadata with passing | ||
/// json-data inside of this initializer. | ||
/// NB! Empty 'jsonData' will lead to decoding error. | ||
init( | ||
jsonData: Data, | ||
jsonDecoder: JSONDecoder = .init() | ||
) throws { | ||
self = try jsonDecoder.decode(CoreSdkMessageMetadataContainer.self, from: jsonData) | ||
} | ||
} |