Skip to content

Commit 69e2ea6

Browse files
committed
Simulate key press in snapshot test
1 parent b0a7659 commit 69e2ea6

File tree

3 files changed

+131
-7
lines changed

3 files changed

+131
-7
lines changed

src/main.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,20 +424,22 @@ fn set_panic_handlers() -> Result<()> {
424424

425425
#[cfg(test)]
426426
mod tests {
427-
use std::{cell::RefCell, path::PathBuf};
427+
use std::{
428+
cell::RefCell, path::PathBuf, thread::sleep, time::Duration,
429+
};
428430

429-
use asyncgit::sync::RepoPath;
431+
use asyncgit::{sync::RepoPath, AsyncGitNotification};
430432
use crossbeam_channel::unbounded;
431433
use git2_testing::repo_init;
432434
use insta::assert_snapshot;
433435
use ratatui::{backend::TestBackend, Terminal};
434436

435437
use crate::{
436438
app::App, draw, input::Input, keys::KeyConfig,
437-
ui::style::Theme,
439+
ui::style::Theme, AsyncNotification,
438440
};
439441

440-
// Macro recommended by: https://insta.rs/docs/cmd/
442+
// Macro adapted from: https://insta.rs/docs/cmd/
441443
macro_rules! apply_common_filters {
442444
{} => {
443445
let mut settings = insta::Settings::clone_current();
@@ -447,6 +449,8 @@ mod tests {
447449
settings.add_filter(r" */tmp/\.tmp\S+", "[TEMP_FILE]");
448450
// Windows Temp folder
449451
settings.add_filter(r" *\[…\].*/Local/Temp/\S+", "[TEMP_FILE]");
452+
// Commit ids that follow a vertical bar
453+
settings.add_filter(r"│[a-z0-9]{7} ", "│[AAAAA] ");
450454
let _bound = settings.bind_to_scope();
451455
}
452456
}
@@ -468,21 +472,51 @@ mod tests {
468472
.map_err(|e| eprintln!("KeyConfig loading error: {e}"))
469473
.unwrap_or_default();
470474

471-
let app = App::new(
475+
let mut app = App::new(
472476
RefCell::new(path),
473477
tx_git,
474478
tx_app,
475479
input.clone(),
476480
theme,
477-
key_config,
481+
key_config.clone(),
478482
)
479483
.unwrap();
480484

481485
let mut terminal =
482486
Terminal::new(TestBackend::new(120, 40)).unwrap();
483487

484-
let _ = draw(&mut terminal, &app);
488+
draw(&mut terminal, &app).unwrap();
485489

486490
assert_snapshot!(terminal.backend());
491+
492+
let event =
493+
AsyncNotification::Git(AsyncGitNotification::Status);
494+
app.update_async(event).unwrap();
495+
496+
sleep(Duration::from_millis(5));
497+
498+
draw(&mut terminal, &app).unwrap();
499+
500+
assert_snapshot!("app_loading_finished", terminal.backend());
501+
502+
let event = crossterm::event::KeyEvent::new(
503+
key_config.keys.tab_log.code,
504+
key_config.keys.tab_log.modifiers,
505+
);
506+
app.event(crate::input::InputEvent::Input(
507+
crossterm::event::Event::Key(event),
508+
))
509+
.unwrap();
510+
511+
sleep(Duration::from_millis(5));
512+
513+
app.update().unwrap();
514+
515+
draw(&mut terminal, &app).unwrap();
516+
517+
assert_snapshot!(
518+
"app_log_tab_showing_one_commit",
519+
terminal.backend()
520+
);
487521
}
488522
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
source: src/main.rs
3+
expression: terminal.backend()
4+
snapshot_kind: text
5+
---
6+
" Status [1] | Log [2] | Files [3] | Stashing [4] | Stashes [5][TEMP_FILE] "
7+
" ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── "
8+
"┌Unstaged Changes──────────────────────────────────────────┐┌Diff: ────────────────────────────────────────────────────┐"
9+
"│ ││ │"
10+
"│ ││ │"
11+
"│ ││ │"
12+
"│ ││ │"
13+
"│ ││ │"
14+
"│ ││ │"
15+
"│ ││ │"
16+
"│ ││ │"
17+
"│ ││ │"
18+
"│ ││ │"
19+
"│ ││ │"
20+
"│ ││ │"
21+
"│ ││ │"
22+
"│ ││ │"
23+
"│ ││ │"
24+
"│ ││ │"
25+
"│ ││ │"
26+
"│ ││ │"
27+
"│ ││ │"
28+
"│ ││ │"
29+
"└──────────────────────────────────────────────────{master}┘│ │"
30+
"┌Staged Changes────────────────────────────────────────────┐│ │"
31+
"│ ││ │"
32+
"│ ││ │"
33+
"│ ││ │"
34+
"│ ││ │"
35+
"│ ││ │"
36+
"│ ││ │"
37+
"│ ││ │"
38+
"│ ││ │"
39+
"│ ││ │"
40+
"│ ││ │"
41+
"│ ││ │"
42+
"│ ││ │"
43+
"│ ││ │"
44+
"└──────────────────────────────────────────────────────────┘└──────────────────────────────────────────────────────────┘"
45+
"Branches [b] Push [p] Fetch [⇧F] Pull [f] Undo Commit [⇧U] Submodules [⇧S] Nav [kjlh] Diff [l] To stage [w] more [.]"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
source: src/main.rs
3+
expression: terminal.backend()
4+
snapshot_kind: text
5+
---
6+
" Status [1] | Log [2] | Files [3] | Stashing [4] | Stashes [5][TEMP_FILE] "
7+
" ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── "
8+
"┌Commit 1/1────────────────────────────────────────────────────────────────────────────────────────────────────────────┐"
9+
"│[AAAAA] <1m ago name initial █"
10+
"│ ║"
11+
"│ ║"
12+
"│ ║"
13+
"│ ║"
14+
"│ ║"
15+
"│ ║"
16+
"│ ║"
17+
"│ ║"
18+
"│ ║"
19+
"│ ║"
20+
"│ ║"
21+
"│ ║"
22+
"│ ║"
23+
"│ ║"
24+
"│ ║"
25+
"│ ║"
26+
"│ ║"
27+
"│ ║"
28+
"│ ║"
29+
"│ ║"
30+
"│ ║"
31+
"│ ║"
32+
"│ ║"
33+
"│ ║"
34+
"│ ║"
35+
"│ ║"
36+
"│ ║"
37+
"│ ║"
38+
"│ ║"
39+
"│ ║"
40+
"│ ║"
41+
"│ ║"
42+
"│ ║"
43+
"│ ║"
44+
"└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘"
45+
"Scroll [kj] Mark [˽] Details [⏎] Branches [b] Compare [⇧C] Copy Hash [y] Tag [t] Checkout [⇧S] Tags [⇧T] more [.]"

0 commit comments

Comments
 (0)