# How to use JS/TS PocketIC to test interactions between 2 or more canisters?

**URL:** <https://forum.dfinity.org/t/how-to-use-js-ts-pocketic-to-test-interactions-between-2-or-more-canisters/33924>\
**Category:** Motoko\
**Created:** [August 6, 2024, 10:49am UTC](https://forum.dfinity.org/t/how-to-use-js-ts-pocketic-to-test-interactions-between-2-or-more-canisters/33924 "2024-08-06T10:49:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)\
**Post date:** [August 6, 2024, 10:49am UTC](https://forum.dfinity.org/t/how-to-use-js-ts-pocketic-to-test-interactions-between-2-or-more-canisters/33924/1 "2024-08-06T10:49:32Z")

</div>

@NathanosDev , I am using Pocket IC with JS/TS to implement a test suite for my Motoko project

I would like to use my js/ts test suite to create communication examples between 2 or more canisters. So, far I have created tests where a js client (example.psec.ts) send update/query calls to a single canister. Now I would want to start 2 or 3 canisters so that the js client sends calls to canister1 ans canister1 sends more calls to canister2/3 and so on. what is the best way to do something like that?

---

<div class="post-metadata">

**Author:** ![NathanosDev](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/nathanosdev/32/7204_2.png) [@NathanosDev](https://forum.dfinity.org/u/NathanosDev)\
**Post date:** [August 6, 2024, 11:23am UTC](https://forum.dfinity.org/t/how-to-use-js-ts-pocketic-to-test-interactions-between-2-or-more-canisters/33924/2 "2024-08-06T11:23:44Z")

</div>

You can check the [multicanister example](https://github.com/hadronous/pic-js/tree/main/examples/multicanister) for this exact scenario.

---

<div class="post-metadata">

**Author:** ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)\
**Post date:** [August 7, 2024, 3:48pm UTC](https://forum.dfinity.org/t/how-to-use-js-ts-pocketic-to-test-interactions-between-2-or-more-canisters/33924/3 "2024-08-07T15:48:20Z")

</div>

@NathanosDev , a more specific question related to the multicanister example is how can I create a second canister that requires the principal of the first canister:

Let’s assume I want to create 2 canisters: 1) Ledger and 2) Reader which requires the principal of Ledger:

```auto
export async function TestReader(pic: PocketIc, readerCanisterId: Principal) {
  const fixture = await pic.setupCanister<TestService_reader>({
    idlFactory: TestIdlFactory_reader,
    wasm: READER_READER_WASM_PATH,
    arg: IDL.encode(init_reader({ IDL }), []), 
  });

  return fixture;
}

export async function TestLedger(pic: PocketIc, ledgerCanisterId: Principal) {
  const fixture = await pic.setupCanister<TestService_ledger>({
    idlFactory: TestIdlFactory_ledger,
    wasm: READER_LEDGER_WASM_PATH,
    arg: IDL.encode(init_ledger({ IDL }), []), 
  });

  return fixture;
}

```

Now I create the Ledger canister:

```auto
const fixture_ledger = await TestLedger(pic, Principal.fromText("aaaaa-aa"));
can_ledger = fixture_ledger.actor;
canCanisterId_ledger = fixture_ledger.canisterId; 

```

How can I now create the Reader canister and pass the Ledger principal?

---

<div class="post-metadata">

**Author:** ![NathanosDev](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/nathanosdev/32/7204_2.png) [@NathanosDev](https://forum.dfinity.org/u/NathanosDev)\
**Post date:** [August 8, 2024, 7:59am UTC](https://forum.dfinity.org/t/how-to-use-js-ts-pocketic-to-test-interactions-between-2-or-more-canisters/33924/4 "2024-08-08T07:59:45Z")

</div>

You can use the init args to pass principals of other canisters, as I did in that example.

---

<div class="post-metadata">

**Author:** ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)\
**Post date:** [August 8, 2024, 9:32am UTC](https://forum.dfinity.org/t/how-to-use-js-ts-pocketic-to-test-interactions-between-2-or-more-canisters/33924/5 "2024-08-08T09:32:17Z")

</div>

@NathanosDev  
`setupCanister` documentation says that “`Arg` is a candid encoded argument to pass to the canister’s init function”.

Looking into the example, the canister code that receives the arguments seems to be a class actor with a KeyValue parameter  
`actor class KeyValue(phonebook : PhoneBook.Self, superheroes : SuperHeroes.Self) { `

Do I need to define my canister code as a class actor with a KeyValue to be able to receive `args`? Is it possble for any canister defined as `actor {...}` to receive `args`?

The reason to use `actor XXXX {...}` instead of `actor classs XXXX() {...}` is because I need to access the principal of from inside this actor `Principal.fromActor(XXXX)`. Is there a way to access the preincipal of the class actor instance from inside the class actor. Otherwise, is there a way to pass args to a non class actor?

---

<div class="post-metadata">

**Author:** ![ggreif](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ggreif/32/301_2.png) [@ggreif](https://forum.dfinity.org/u/ggreif)\
**Post date:** [August 8, 2024, 12:37pm UTC](https://forum.dfinity.org/t/how-to-use-js-ts-pocketic-to-test-interactions-between-2-or-more-canisters/33924/6 "2024-08-08T12:37:23Z")

</div>

> [@ildefons](#):
>
> Is there a way to access the principal of the class actor instance from inside the class actor.

Yes, there is a syntax for capturing the `actor class`’ identity:

```Motoko
actor class C() = c {
 /// methods can use `c`
}

```

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [August 8, 2024, 1:32pm UTC](https://forum.dfinity.org/t/how-to-use-js-ts-pocketic-to-test-interactions-between-2-or-more-canisters/33924/7 "2024-08-08T13:32:52Z")

</div>

If you want to receive the installation argument you do need to use a class.
