Skip to content

Commit 4a33291

Browse files
committed
Simulate key press in snapshot test
1 parent 1a1aaf5 commit 4a33291

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
@@ -414,20 +414,22 @@ fn set_panic_handler() -> Result<()> {
414414

415415
#[cfg(test)]
416416
mod tests {
417-
use std::{cell::RefCell, path::PathBuf};
417+
use std::{
418+
cell::RefCell, path::PathBuf, thread::sleep, time::Duration,
419+
};
418420

419-
use asyncgit::sync::RepoPath;
421+
use asyncgit::{sync::RepoPath, AsyncGitNotification};
420422
use crossbeam_channel::unbounded;
421423
use git2_testing::repo_init;
422424
use insta::assert_snapshot;
423425
use ratatui::{backend::TestBackend, Terminal};
424426

425427
use crate::{
426428
app::App, draw, input::Input, keys::KeyConfig,
427-
ui::style::Theme,
429+
ui::style::Theme, AsyncNotification,
428430
};
429431

430-
// Macro recommended by: https://insta.rs/docs/cmd/
432+
// Macro adapted from: https://insta.rs/docs/cmd/
431433
macro_rules! apply_common_filters {
432434
{} => {
433435
let mut settings = insta::Settings::clone_current();
@@ -437,6 +439,8 @@ mod tests {
437439
settings.add_filter(r" */tmp/\.tmp\S+", "[TEMP_FILE]");
438440
// Windows Temp folder
439441
settings.add_filter(r" *\[…\].*/Local/Temp/\S+", "[TEMP_FILE]");
442+
// Commit ids that follow a vertical bar
443+
settings.add_filter(r"│[a-z0-9]{7} ", "│[AAAAA] ");
440444
let _bound = settings.bind_to_scope();
441445
}
442446
}
@@ -458,21 +462,51 @@ mod tests {
458462
.map_err(|e| eprintln!("KeyConfig loading error: {e}"))
459463
.unwrap_or_default();
460464

461-
let app = App::new(
465+
let mut app = App::new(
462466
RefCell::new(path),
463467
tx_git,
464468
tx_app,
465469
input.clone(),
466470
theme,
467-
key_config,
471+
key_config.clone(),
468472
)
469473
.unwrap();
470474

471475
let mut terminal =
472476
Terminal::new(TestBackend::new(120, 40)).unwrap();
473477

474-
let _ = draw(&mut terminal, &app);
478+
draw(&mut terminal, &app).unwrap();
475479

476480
assert_snapshot!(terminal.backend());
481+
482+
let event =
483+
AsyncNotification::Git(AsyncGitNotification::Status);
484+
app.update_async(event).unwrap();
485+
486+
sleep(Duration::from_millis(5));
487+
488+
draw(&mut terminal, &app).unwrap();
489+
490+
assert_snapshot!("app_loading_finished", terminal.backend());
491+
492+
let event = crossterm::event::KeyEvent::new(
493+
key_config.keys.tab_log.code,
494+
key_config.keys.tab_log.modifiers,
495+
);
496+
app.event(crate::input::InputEvent::Input(
497+
crossterm::event::Event::Key(event),
498+
))
499+
.unwrap();
500+
501+
sleep(Duration::from_millis(5));
502+
503+
app.update().unwrap();
504+
505+
draw(&mut terminal, &app).unwrap();
506+
507+
assert_snapshot!(
508+
"app_log_tab_showing_one_commit",
509+
terminal.backend()
510+
);
477511
}
478512
}
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)