Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
]
  • Loading branch information
Velin92 committed Jun 11, 2024
1 parent 725e24f commit 2ba5cba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
4 changes: 2 additions & 2 deletions crates/matrix-sdk-base/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ pub use self::integration_tests::StateStoreIntegrationTests;
pub use self::{
memory_store::MemoryStore,
traits::{
ComposerDraft, DynStateStore, IntoStateStore, StateStore, StateStoreDataKey,
StateStoreDataValue, StateStoreExt,
ComposerDraft, ComposerDraftType, DynStateStore, IntoStateStore, StateStore,
StateStoreDataKey, StateStoreDataValue, StateStoreExt,
},
};

Expand Down
22 changes: 14 additions & 8 deletions crates/matrix-sdk-base/src/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,28 +823,34 @@ pub enum StateStoreDataValue {
}

/// Current draft of the composer for the room.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct ComposerDraft {
/// The draft content in plain text.
plain_text: String,
pub plain_text: String,
/// If the message is formatted in HTML, the HTML representation of the
/// message.
html_text: Option<String>,
pub html_text: Option<String>,
/// The type of draft.
draft_type: ComposerDraftType,
pub draft_type: ComposerDraftType,
}

/// The type of draft of the composer.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
pub enum ComposerDraftType {
/// The draft is a new message.
NewMessage,
/// The draft is a rseply to an event.
Reply { event_id: String },
/// The draft is a reply to an event.
Reply {
/// The ID of the event being replied to.
event_id: String,
},
/// The draft is an edit of an event.
Edit { event_id: String },
Edit {
/// The ID of the event being edited.
event_id: String,
},
}

impl StateStoreDataValue {
Expand Down
29 changes: 28 additions & 1 deletion crates/matrix-sdk/src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2969,7 +2969,7 @@ pub struct TryFromReportedContentScoreError(());

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
use matrix_sdk_base::SessionMeta;
use matrix_sdk_base::{store::ComposerDraftType, ComposerDraft, SessionMeta};
use matrix_sdk_test::{
async_test, test_json, JoinedRoomBuilder, StateTestEvent, SyncResponseBuilder,
};
Expand All @@ -2983,6 +2983,7 @@ mod tests {
use crate::{
config::RequestConfig,
matrix_auth::{MatrixSession, MatrixSessionTokens},
test_utils::logged_in_client,
Client,
};

Expand Down Expand Up @@ -3131,4 +3132,30 @@ mod tests {
ReportedContentScore::try_from(int!(10)).unwrap_err();
ReportedContentScore::try_from(int!(-110)).unwrap_err();
}

#[async_test]
async fn test_composer_draft() {
use matrix_sdk_test::DEFAULT_TEST_ROOM_ID;

let client = logged_in_client(None).await;

let response = SyncResponseBuilder::default()
.add_joined_room(JoinedRoomBuilder::default())
.build_sync_response();
client.base_client().receive_sync_response(response).await.unwrap();
let room = client.get_room(&DEFAULT_TEST_ROOM_ID).expect("Room should exist");

assert_eq!(room.load_composer_draft().await.unwrap(), None);

let draft = ComposerDraft {
plain_text: "Hello, world!".to_owned(),
html_text: Some("<strong>Hello</strong>, world!".to_owned()),
draft_type: ComposerDraftType::NewMessage,
};
room.save_composer_draft(draft.clone()).await.unwrap();
assert_eq!(room.load_composer_draft().await.unwrap(), Some(draft));

room.clear_composer_draft().await.unwrap();
assert_eq!(room.load_composer_draft().await.unwrap(), None);
}
}

0 comments on commit 2ba5cba

Please sign in to comment.