@onflow/flow-js-testing
Version:
This package will expose a set of utility methods, to allow Cadence code testing with libraries like Jest
73 lines (64 loc) • 1.96 kB
JavaScript
import path from "path"
import {init, emulator, executeScript, shallResolve} from "../../src"
import {DEFAULT_TEST_TIMEOUT} from "../util/timeout.const"
jest.setTimeout(DEFAULT_TEST_TIMEOUT)
describe("metadata examples", () => {
beforeEach(async () => {
const basePath = path.resolve("./cadence")
await init(basePath)
return emulator.start()
})
afterEach(async () => {
return emulator.stop()
})
test("simple dictionary - {String: String}", async () => {
const code = `
access(all) fun main(metadata: {String: String}): String{
return metadata["name"]!
}
`
const name = "Cadence"
const args = [{name}]
const [result] = await shallResolve(executeScript({code, args}))
expect(result).toBe(name)
})
test("simple dictionary - {String: Int}", async () => {
const code = `
access(all) fun main(metadata: {String: Int}): Int{
return metadata["answer"]!
}
`
const answer = "42"
const args = [{answer}]
const [result] = await shallResolve(executeScript({code, args}))
expect(result).toBe(answer)
})
test("simple array - [String]", async () => {
const code = `
access(all) fun main(list: [String]): String{
return list[0]
}
`
const value = "test"
const args = [[value]]
const [result] = await shallResolve(executeScript({code, args}))
expect(result).toBe(value)
})
test("nested arrays - [[Int]]", async () => {
const code = `
access(all) fun main(list: [[Int]], index: Int): Int {
log("this is log message we want to output")
log(list[0][0])
log(list[0][1])
log(list[0][2])
return list[0][index]
}
`
const value = ["1", "3", "3", "7"]
const index = "3"
const args = [[value], index]
const [result, err] = await executeScript({code, args})
expect(result).toBe(value[index])
expect(err).toBe(null)
})
})