Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/app/screens/bookmarked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,109 @@ impl BookmarkedPatchsets {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json;

fn make_patch(title: &str) -> Patch {
let json = format!(
r#"{{
"title": "{title}",
"author": {{ "name": "name", "email": "teste@teste.com" }},
"link": {{ "@href": "http://lore.kernel.org/some-list/1234-1-teste@teste.com" }},
"thr:in-reply-to": null,
"updated": "2024-07-06T19:15:48Z"
}}"#
);
serde_json::from_str::<Patch>(&json).unwrap()
}

fn mock_bookmarked_patchsets() -> BookmarkedPatchsets {
BookmarkedPatchsets {
bookmarked_patchsets: vec![
make_patch("Patch 1"),
make_patch("Patch 2"),
make_patch("Patch 3"),
],
patchset_index: 0,
}
}

#[test]
fn test_select_below_patchset_increments_index() {
let mut s = mock_bookmarked_patchsets();
let patchset_index_before = s.patchset_index;
s.select_below_patchset();
assert_eq!(s.patchset_index, patchset_index_before + 1);
}

#[test]
fn test_select_below_patchset_stops_at_end() {
let mut s = mock_bookmarked_patchsets();
s.patchset_index = s.bookmarked_patchsets.len() - 1;
s.select_below_patchset();
assert_eq!(s.patchset_index, s.bookmarked_patchsets.len() - 1);
}

#[test]
fn test_select_above_patchset_decrements_index() {
let mut s = mock_bookmarked_patchsets();
s.patchset_index = 2;
s.select_above_patchset();
assert_eq!(s.patchset_index, 1);
}

#[test]
fn test_select_above_patchset_stops_at_start() {
let mut s = mock_bookmarked_patchsets();
s.patchset_index = 0;
s.select_above_patchset();
assert_eq!(s.patchset_index, 0);
Comment on lines +100 to +101
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing here. We shouldn't need to see mock_bookmarked_patchsets implementation to understand what's the initial patchset_index. You can assert or set the patchset_index

}

#[test]
fn test_get_selected_patchset_returns_correct_patch() {
let s = mock_bookmarked_patchsets();
let patch = s.get_selected_patchset();
assert_eq!(patch.title(), "Patch 1");
}

#[test]
fn test_bookmark_selected_patch_adds_new_patch() {
let mut s = mock_bookmarked_patchsets();
let new_patch = make_patch("New Patch");
s.bookmark_selected_patch(&new_patch);
assert!(s.bookmarked_patchsets.contains(&new_patch));
}

#[test]
fn test_bookmark_selected_patch_does_not_duplicate() {
let mut s = mock_bookmarked_patchsets();
let existing_patch = make_patch("Patch 1");
s.bookmark_selected_patch(&existing_patch);
let count = s
.bookmarked_patchsets
.iter()
.filter(|p| **p == existing_patch)
.count();
assert_eq!(count, 1);
}

#[test]
fn test_unbookmark_selected_patch_removes_patch() {
let mut s = mock_bookmarked_patchsets();
let to_remove = s.bookmarked_patchsets[1].clone();
s.unbookmark_selected_patch(&to_remove);
assert!(!s.bookmarked_patchsets.contains(&to_remove));
}

#[test]
fn test_unbookmark_selected_patch_ignores_missing_patch() {
let mut s = mock_bookmarked_patchsets();
let missing = make_patch("Inexistent");
s.unbookmark_selected_patch(&missing);
assert_eq!(s.bookmarked_patchsets.len(), 3);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, you should be asserting the len before the unbookmark_selected_patch operation so the test behavior is explicit

}
}