Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 99cac5c

Browse files
authored
Add shallPass/shallResolve helpers to examples (#160)
1 parent 57edf7d commit 99cac5c

10 files changed

+73
-47
lines changed

examples/02-deploy-contract-by-name.test.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import path from "path"
2-
import {init, emulator, deployContractByName, executeScript} from "../src"
2+
import {
3+
init,
4+
emulator,
5+
deployContractByName,
6+
executeScript,
7+
shallResolve,
8+
shallPass,
9+
} from "../src"
310

411
beforeEach(async () => {
512
// Init framework
@@ -12,34 +19,40 @@ beforeEach(async () => {
1219

1320
test("deploy contract by name", async () => {
1421
// Deploy contract Greeting with single argument
15-
await deployContractByName({
16-
name: "Greeting",
17-
args: ["Hello from Emulator"],
18-
})
22+
await shallPass(
23+
deployContractByName({
24+
name: "Greeting",
25+
args: ["Hello from Emulator"],
26+
})
27+
)
1928

2029
// Read contract field via script
21-
const [greetingMessage] = await executeScript({
22-
code: `
30+
const [greetingMessage] = await shallResolve(
31+
executeScript({
32+
code: `
2333
import Greeting from 0x1
2434
2535
pub fun main(): String{
2636
return Greeting.message
2737
}
2838
`,
29-
})
39+
})
40+
)
3041
expect(greetingMessage).toBe("Hello from Emulator")
3142

3243
// Deploy contract Hello with no arguments
3344
await deployContractByName({name: "Hello"})
34-
const [helloMessage] = await executeScript({
35-
code: `
45+
const [helloMessage] = await shallResolve(
46+
executeScript({
47+
code: `
3648
import Hello from 0x01
3749
3850
pub fun main():String{
3951
return Hello.message
4052
}
4153
`,
42-
})
54+
})
55+
)
4356
expect(helloMessage).toBe("Hi!")
4457
})
4558

examples/03-deploy-contract.test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
getAccountAddress,
66
deployContract,
77
executeScript,
8+
shallResolve,
9+
shallPass,
810
} from "../src"
911

1012
beforeEach(async () => {
@@ -29,16 +31,18 @@ test("deploy contract", async () => {
2931
`
3032
const args = [1337]
3133

32-
await deployContract({to, name, code, args})
34+
await shallPass(deployContract({to, name, code, args}))
3335

34-
const [balance] = await executeScript({
35-
code: `
36+
const [balance] = await shallResolve(
37+
executeScript({
38+
code: `
3639
import Wallet from 0x01
3740
pub fun main(): UInt{
3841
return Wallet.balance
3942
}
4043
`,
41-
})
44+
})
45+
)
4246
expect(balance).toBe("1337")
4347
})
4448

examples/06-flow-management.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getAccountAddress,
66
getFlowBalance,
77
mintFlow,
8+
shallResolve,
89
} from "../src"
910

1011
beforeEach(async () => {
@@ -19,14 +20,14 @@ test("flow management", async () => {
1920
const Alice = await getAccountAddress("Alice")
2021

2122
// Get initial balance
22-
const [initialBalance] = await getFlowBalance(Alice)
23+
const [initialBalance] = await shallResolve(getFlowBalance(Alice))
2324
expect(initialBalance).toBe("0.00100000")
2425

2526
// Add 1.0 FLOW tokens to Alice account
2627
await mintFlow(Alice, "1.0")
2728

2829
// Check updated balance
29-
const [updatedBalance] = await getFlowBalance(Alice)
30+
const [updatedBalance] = await shallResolve(getFlowBalance(Alice))
3031
const expectedBalance = parseFloat(initialBalance) + 1.0
3132
expect(parseFloat(updatedBalance)).toBe(expectedBalance)
3233

examples/07-block-offset.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
setBlockOffset,
77
builtInMethods,
88
executeScript,
9+
shallResolve,
910
} from "../src"
1011

1112
beforeEach(async () => {
@@ -16,7 +17,7 @@ beforeEach(async () => {
1617
})
1718

1819
test("block offset", async () => {
19-
const [initialBlockOffset] = await getBlockOffset()
20+
const [initialBlockOffset] = await shallResolve(getBlockOffset())
2021
expect(initialBlockOffset).toBe("0")
2122

2223
// "getCurrentBlock().height" in your Cadence code will be replaced by Manager to a mocked value
@@ -27,7 +28,7 @@ test("block offset", async () => {
2728
`
2829

2930
// We can check that non-transformed code still works just fine
30-
const [normalResult] = await executeScript({code})
31+
const [normalResult] = await shallResolve(executeScript({code}))
3132
expect(normalResult).toBe("1")
3233

3334
// Offset current block height by 42
@@ -39,7 +40,9 @@ test("block offset", async () => {
3940
// "transformers" field expects array of functions to operate update the code.
4041
// We will pass single operator "builtInMethods" provided by the framework to alter how getCurrentBlock().height is calculated
4142
const transformers = [builtInMethods]
42-
const [transformedResult] = await executeScript({code, transformers})
43+
const [transformedResult] = await shallResolve(
44+
executeScript({code, transformers})
45+
)
4346
expect(transformedResult).toBe("44")
4447
})
4548

examples/08-execute-script.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path"
2-
import {init, emulator, executeScript} from "../src"
2+
import {init, emulator, executeScript, shallResolve} from "../src"
33

44
beforeEach(async () => {
55
const basePath = path.resolve(__dirname, "./cadence")
@@ -39,8 +39,8 @@ test("execute script", async () => {
3939
]
4040
const name = "log-args"
4141

42-
const [fromCode] = await executeScript({code, args})
43-
const [fromFile] = await executeScript({name, args})
42+
const [fromCode] = await shallResolve(executeScript({code, args}))
43+
const [fromFile] = await shallResolve(executeScript({name, args}))
4444
expect(fromCode).toBe(fromFile)
4545
expect(fromCode).toBe("42")
4646

examples/09-send-transaction.test.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import path from "path"
2-
import {init, emulator, getAccountAddress, sendTransaction} from "../src"
2+
import {
3+
init,
4+
emulator,
5+
getAccountAddress,
6+
sendTransaction,
7+
shallPass,
8+
} from "../src"
39

410
beforeEach(async () => {
511
const basePath = path.resolve(__dirname, "./cadence")
@@ -29,19 +35,18 @@ test("send transaction", async () => {
2935

3036
// There are several ways to call "sendTransaction"
3137
// 1. Providing "code" field for Cadence template
32-
const [txInlineResult] = await sendTransaction({code, signers, args})
38+
const [txInlineResult] = await shallPass(
39+
sendTransaction({code, signers, args})
40+
)
3341
// 2. Providing "name" field to read Cadence template from file in "./transaction" folder
34-
const [txFileResult] = await sendTransaction({name, signers, args})
35-
36-
expect(txInlineResult).toBeTruthy()
37-
expect(txFileResult).toBeTruthy()
38-
expect(txInlineResult.statusCode).toBe(0)
39-
expect(txFileResult.statusCode).toBe(0)
42+
const [txFileResult] = await shallPass(sendTransaction({name, signers, args}))
4043

4144
// 3. Providing name of the file in short form (name, signers, args)
42-
const [txShortResult] = await sendTransaction(name, signers, args)
43-
expect(txShortResult).toBeTruthy()
44-
expect(txShortResult.statusCode).toBe(0)
45+
const [txShortResult] = await shallPass(sendTransaction(name, signers, args))
46+
47+
// Check that all transaction results are the same
48+
expect(txFileResult).toEqual(txInlineResult)
49+
expect(txShortResult).toEqual(txInlineResult)
4550
})
4651

4752
afterEach(async () => {

examples/10-templates.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,24 @@ test("templates", async () => {
2020
Profile: "0xf8d6e0586b0a20c7",
2121
}
2222

23-
const withPath = await getTemplate(
23+
const withPath = getTemplate(
2424
path.resolve(__dirname, "./cadence/scripts/replace-address.cdc"),
2525
addressMap
2626
)
27-
expect(withPath).toBeTruthy()
2827

2928
const contractTemplate = await getContractCode({name: "Greeting", addressMap})
30-
expect(contractTemplate).toBeTruthy()
3129

3230
const transactionTemplate = await getTransactionCode({
3331
name: "log-signers",
3432
addressMap,
3533
})
36-
expect(transactionTemplate).toBeTruthy()
3734

3835
const scriptTemplate = await getScriptCode({name: "log-args", addressMap})
39-
expect(scriptTemplate).toBeTruthy()
36+
37+
expect(withPath).toBeDefined()
38+
expect(contractTemplate).toBeDefined()
39+
expect(transactionTemplate).toBeDefined()
40+
expect(scriptTemplate).toBeDefined()
4041
})
4142

4243
afterEach(async () => {

examples/100-pass-array-of-dictionaries.test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path"
2-
import {init, emulator, executeScript} from "../src"
2+
import {init, emulator, executeScript, shallResolve} from "../src"
33

44
beforeEach(async () => {
55
const basePath = path.resolve(__dirname, "./cadence")
@@ -24,9 +24,8 @@ test("pass array of dictionaries", async () => {
2424
"name",
2525
]
2626

27-
const result = await executeScript({code, args})
28-
expect(result[0]).toBe("Giovanni Giorgio")
29-
expect(result[1]).toBeNull()
27+
const [result] = await shallResolve(executeScript({code, args}))
28+
expect(result).toBe("Giovanni Giorgio")
3029
})
3130

3231
afterEach(async () => {

examples/101-pass-int-dictionary.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path"
2-
import {init, emulator, executeScript} from "../src"
2+
import {init, emulator, executeScript, shallResolve} from "../src"
33

44
beforeEach(async () => {
55
const basePath = path.resolve(__dirname, "./cadence")
@@ -17,7 +17,7 @@ test("pass int dictionary", async () => {
1717

1818
const args = [{0: 1, 1: 42}, 1]
1919

20-
const [result] = await executeScript({code, args})
20+
const [result] = await shallResolve(executeScript({code, args}))
2121
expect(result).toBe("42")
2222
})
2323

examples/102-pass-string-to-int-dictionary.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path"
2-
import {init, emulator, executeScript} from "../src"
2+
import {init, emulator, executeScript, shallResolve} from "../src"
33

44
beforeEach(async () => {
55
const basePath = path.resolve(__dirname, "./cadence")
@@ -17,7 +17,7 @@ test("pass string to int dictionary", async () => {
1717

1818
const args = [{cadence: 0, test: 1337}, "cadence"]
1919

20-
const [result] = await executeScript({code, args})
20+
const [result] = await shallResolve(executeScript({code, args}))
2121
expect(result).toBe("0")
2222
})
2323

0 commit comments

Comments
 (0)