forked from rvanasa/vite-react-motoko
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from letmejustputthishere/feature/pic-js
Feature/pic js
- Loading branch information
Showing
9 changed files
with
180 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,32 @@ | ||
name: Tests | ||
name: tests | ||
|
||
on: | ||
# push: | ||
# branches: [ "main" ] | ||
workflow_dispatch: | ||
pull_request: | ||
|
||
concurrency: | ||
group: ${{ github.head_ref || github.run_id }} | ||
cancel-in-progress: true | ||
types: [synchronize, opened, reopened, ready_for_review, unlabeled] | ||
|
||
jobs: | ||
build: | ||
test: | ||
runs-on: ubuntu-22.04 | ||
|
||
strategy: | ||
matrix: | ||
node-version: [18] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v3 | ||
|
||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- name: Clear npm cache | ||
run: npm cache clean --force | ||
node-version: 18 | ||
|
||
- name: Install dfx | ||
uses: dfinity/setup-dfx@main | ||
|
||
- name: Install mops | ||
uses: ZenVoich/setup-mops@v1 | ||
- name: Start dfx | ||
run: | | ||
dfx cache install | ||
dfx start --background | ||
- run: npm run setup | ||
- run: npm test | ||
|
||
- name: generate declarations | ||
run: dfx generate backend | ||
|
||
- name: install dependencies | ||
run: npm i | ||
|
||
- name: run tests | ||
run: npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { Main } "../main"; | ||
|
||
let main = await Main(); | ||
let main = await Main({ phrase = "Hello" }); | ||
|
||
assert (await main.greet("Moritz")) == "Hello, Moritz!"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { describe, it, expect, afterEach, beforeEach } from 'vitest'; | ||
|
||
import { AnonymousIdentity } from '@dfinity/agent'; | ||
import { PocketIc, createIdentity, type Actor } from '@hadronous/pic'; | ||
import type { _SERVICE } from '../src/declarations/backend/backend.did'; | ||
import { deployCanister } from './setup'; | ||
|
||
describe('canister tests', () => { | ||
let pic: PocketIc; | ||
let actor: Actor<_SERVICE>; | ||
|
||
const alice = createIdentity('superSecretAlicePassword'); | ||
const bob = createIdentity('superSecretBobPassword'); | ||
|
||
afterEach(async () => { | ||
await pic.tearDown(); | ||
}); | ||
|
||
describe('when calling greet on the canister deployed with the default init args', () => { | ||
beforeEach(async () => { | ||
({ pic, actor } = await deployCanister({ | ||
deployer: alice.getPrincipal() | ||
})); | ||
}); | ||
|
||
it('the argument should be prefixed with `Hello, `', async () => { | ||
await expect(actor.greet('Moritz')).resolves.toEqual('Hello, Moritz!'); | ||
}); | ||
|
||
it('the argument should be prefixed with `Hello, `, even for very long names', async () => { | ||
const veryLongName = 'a'.repeat(1000); | ||
await expect(actor.greet(veryLongName)).resolves.toEqual('Hello, ' + veryLongName + '!'); | ||
}); | ||
|
||
it('a call to whoami with the anonymous principal should return the anonymous principal', async () => { | ||
actor.setIdentity(new AnonymousIdentity()); | ||
await expect(actor.whoAmI()).resolves.toEqual(new AnonymousIdentity().getPrincipal()); | ||
}); | ||
|
||
it('a call to whoami with the alice principal should return the alice principal', async () => { | ||
actor.setIdentity(alice); | ||
await expect(actor.whoAmI()).resolves.toEqual(alice.getPrincipal()); | ||
}); | ||
|
||
it('a call to whoami with the bob principal should return the bob principal', async () => { | ||
actor.setIdentity(bob); | ||
await expect(actor.whoAmI()).resolves.toEqual(bob.getPrincipal()); | ||
}); | ||
}); | ||
|
||
describe('when calling greet on the canister deployed with `bonjour` as an init arg', () => { | ||
beforeEach(async () => { | ||
({ pic, actor } = await deployCanister({ | ||
initArgs: { phrase: 'bonjour' }, | ||
deployer: alice.getPrincipal() | ||
})); | ||
}); | ||
|
||
it('the argument should be prefixed with `bonjour, `', async () => { | ||
await expect(actor.greet('Moritz')).resolves.toEqual('bonjour, Moritz!'); | ||
}); | ||
|
||
it('the argument should be prefixed with `Hello, `, even for very long names', async () => { | ||
const veryLongName = 'a'.repeat(1000); | ||
await expect(actor.greet(veryLongName)).resolves.toEqual('bonjour, ' + veryLongName + '!'); | ||
}); | ||
|
||
it('a call to whoami with the anonymous principal should return the anonymous principal', async () => { | ||
actor.setIdentity(new AnonymousIdentity()); | ||
await expect(actor.whoAmI()).resolves.toEqual(new AnonymousIdentity().getPrincipal()); | ||
}); | ||
|
||
it('a call to whoami with the alice principal should return the alice principal', async () => { | ||
actor.setIdentity(alice); | ||
await expect(actor.whoAmI()).resolves.toEqual(alice.getPrincipal()); | ||
}); | ||
|
||
it('a call to whoami with the bob principal should return the bob principal', async () => { | ||
actor.setIdentity(bob); | ||
await expect(actor.whoAmI()).resolves.toEqual(bob.getPrincipal()); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { IDL } from '@dfinity/candid'; | ||
import { idlFactory, init } from '../src/declarations/backend/backend.did.js'; | ||
import type { _SERVICE } from '../src/declarations/backend/backend.did'; | ||
import { resolve } from 'node:path'; | ||
import { PocketIc } from '@hadronous/pic'; | ||
import { Principal } from '@dfinity/principal'; | ||
|
||
type InitArgs = { | ||
phrase: string; | ||
}; | ||
const defaultInitArgs: InitArgs = { | ||
phrase: 'Hello' | ||
}; | ||
const WASM_PATH = resolve(__dirname, '..', '.dfx', 'local', 'canisters', 'backend', 'backend.wasm'); | ||
|
||
interface DeployOptions { | ||
initArgs?: InitArgs; | ||
deployer?: Principal; | ||
} | ||
|
||
export async function deployCanister({ | ||
initArgs = defaultInitArgs, | ||
deployer = Principal.anonymous() | ||
}: DeployOptions) { | ||
const encodedInitArgs = IDL.encode(init({ IDL }), [initArgs]); | ||
const pic = await PocketIc.create(); | ||
const fixture = await pic.setupCanister<_SERVICE>( | ||
idlFactory, | ||
WASM_PATH, | ||
undefined, | ||
encodedInitArgs, | ||
deployer | ||
); | ||
const actor = fixture.actor; | ||
const canisterId = fixture.canisterId; | ||
return { pic, actor, canisterId }; | ||
} |