Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
feat: test ProductRegistered emission
Browse files Browse the repository at this point in the history
  • Loading branch information
Oshioke-Salaki committed Oct 27, 2024
1 parent a04bdf2 commit 5871d80
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
8 changes: 4 additions & 4 deletions onchain/src/product/product.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub mod Product {

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
pub enum Event {
#[flat]
OwnableEvent: OwnableComponent::Event,
ProductRegistered: ProductRegistered
Expand All @@ -35,9 +35,9 @@ pub mod Product {
/// @param product_id the ID of the product
/// @param ipfs_hash
#[derive(Drop, starknet::Event)]
struct ProductRegistered {
product_id: felt252,
ipfs_hash: ByteArray
pub struct ProductRegistered {
pub product_id: felt252,
pub ipfs_hash: ByteArray
}

#[constructor]
Expand Down
32 changes: 31 additions & 1 deletion onchain/tests/test_product.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use openzeppelin::access::ownable::interface::{IOwnableDispatcher, IOwnableDispa
use starknet::ContractAddress;
use snforge_std::{
declare, ContractClassTrait, DeclareResultTrait, start_cheat_caller_address,
stop_cheat_caller_address
stop_cheat_caller_address, spy_events, EventSpyAssertionsTrait
};
use scanguard::interfaces::IProduct::{IProductsDispatcher, IProductsDispatcherTrait};
use scanguard::product::product::{Product::Event, Product::ProductRegistered};


const ZERO_ADDR: felt252 = 0x0;
Expand Down Expand Up @@ -78,6 +79,35 @@ fn test_register_product_() {
stop_cheat_caller_address(product_contract_address);
}

#[test]
fn test_register_product_event_emission() {
let product_contract_address = __setup__(OWNER_ADDR);
let product_dispatcher = IProductsDispatcher { contract_address: product_contract_address };
let mut spy = spy_events();

start_cheat_caller_address(product_contract_address, OWNER_ADDR.try_into().unwrap());

let product_id: felt252 = 1;
let ipfs_hash: ByteArray = "QmVqyWcuoBpHvt5tT5Gw9eJz2qYJyGKw4NY4yEdFcopK69";

product_dispatcher.register_product(product_id, ipfs_hash.clone());

let verified_product = product_dispatcher.verify(product_id);
assert(ipfs_hash == verified_product.ipfs_hash, 'no products found');

stop_cheat_caller_address(product_contract_address);

spy
.assert_emitted(
@array![
(
product_contract_address,
Event::ProductRegistered(ProductRegistered { product_id, ipfs_hash })
)
]
);
}

#[test]
fn test_register_multiple_products() {
let product_contract_address = __setup__(OWNER_ADDR);
Expand Down

0 comments on commit 5871d80

Please sign in to comment.