From cdd95409628d7b179b390f84a921323ca68e463f Mon Sep 17 00:00:00 2001 From: glihm Date: Thu, 10 Oct 2024 20:20:39 -0600 Subject: [PATCH] tests: add test with dojo init flow --- examples/dojo_simple/src/lib.cairo | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/examples/dojo_simple/src/lib.cairo b/examples/dojo_simple/src/lib.cairo index c0457c8..16f3fb0 100644 --- a/examples/dojo_simple/src/lib.cairo +++ b/examples/dojo_simple/src/lib.cairo @@ -29,7 +29,7 @@ pub mod models { pub mod events { use starknet::ContractAddress; - + #[derive(Drop, Serde)] #[dojo::event] pub struct PositionUpdated { @@ -57,6 +57,11 @@ pub mod actions { pub a: u8, } + fn dojo_init(world: @IWorldDispatcher, id: u32, a: u8) { + let m = ModelInContract { id, a }; + m.set(world); + } + #[abi(embed_v0)] impl ActionsImpl of IActions { fn spawn(ref world: IWorldDispatcher) { @@ -78,8 +83,25 @@ pub mod sn_actions { #[cfg(test)] mod tests { + use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; #[test] fn test_spawn_world_full() { let _world = spawn_test_world!(); } + + #[test] + fn test_dojo_init_flow() { + let world = spawn_test_world!(); + + let actions_addr = world + .register_contract('salt1', super::actions::TEST_CLASS_HASH.try_into().unwrap()); + + world.grant_writer(dojo::utils::bytearray_hash(@"ds"), actions_addr); + + world.init_contract(selector_from_tag!("ds-actions"), [10, 20].span()); + + let model = super::actions::ModelInContractStore::get(world, 10); + assert_eq!(model.a, 20); + } } +