Skip to content

Commit

Permalink
test(update_events): add test for stored dismissed ids
Browse files Browse the repository at this point in the history
  • Loading branch information
tymmesyde committed Dec 14, 2023
1 parent 02e468b commit 8a8546f
Showing 1 changed file with 74 additions and 4 deletions.
78 changes: 74 additions & 4 deletions src/unit_tests/ctx/update_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ use crate::{
models::{common::Loadable, ctx::Ctx},
runtime::{
msg::{Action, ActionCtx},
EnvFutureExt, Runtime, RuntimeAction, TryEnvFuture,
Env, EnvFutureExt, Runtime, RuntimeAction, TryEnvFuture,
},
types::{
api::{APIResult, GetModalResponse, GetNotificationResponse},
events::DismissedEventsBucket,
},
types::api::{APIResult, GetModalResponse, GetNotificationResponse},
unit_tests::{default_fetch_handler, Request, TestEnv, FETCH_HANDLER},
};

Expand All @@ -31,7 +34,7 @@ fn fetch_handler(request: Request) -> TryEnvFuture<Box<dyn Any + Send>> {
Request { url, .. } if url == "https://api.strem.io/api/getNotification" => {
future::ok(Box::new(APIResult::Ok {
result: Some(GetNotificationResponse {
id: "event_id".to_owned(),
id: "notification_id".to_owned(),
title: "title".to_owned(),
message: "message".to_owned(),
external_url: None,
Expand Down Expand Up @@ -160,7 +163,74 @@ fn test_dismissed_events() {
TestEnv::run(|| {
runtime.dispatch(RuntimeAction {
field: None,
action: Action::Ctx(ActionCtx::DismissEvent("event_id".to_owned())),
action: Action::Ctx(ActionCtx::DismissEvent("notification_id".to_owned())),
})
});

assert_eq!(
runtime.model().unwrap().ctx.events.modal,
Loadable::Ready(None),
"Modal should be ready and empty"
);

assert_eq!(
runtime.model().unwrap().ctx.events.notification,
Loadable::Ready(None),
"Notification should be ready and empty"
);
}

#[test]
fn test_stored_dismissed_events() {
#[derive(Model, Clone, Debug)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
}

let _env_mutex = TestEnv::reset().expect("Should have exclusive lock to TestEnv");

*FETCH_HANDLER.write().unwrap() = Box::new(fetch_handler);

let mut dismissed_events = DismissedEventsBucket::default();
dismissed_events
.items
.insert("modal_id".to_owned(), TestEnv::now().into());
dismissed_events
.items
.insert("notification_id".to_owned(), TestEnv::now().into());

let (runtime, _rx) = Runtime::<TestEnv, _>::new(
TestModel {
ctx: Ctx {
dismissed_events,
..Ctx::default()
},
},
vec![],
1000,
);

assert!(
runtime.model().unwrap().ctx.events.modal.is_loading(),
"Modal should be loading"
);

assert!(
runtime
.model()
.unwrap()
.ctx
.events
.notification
.is_loading(),
"Notification should be loading"
);

TestEnv::run(|| {
runtime.dispatch(RuntimeAction {
field: None,
action: Action::Ctx(ActionCtx::GetEvents),
})
});

Expand Down

0 comments on commit 8a8546f

Please sign in to comment.