-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add middlewares decorator Add middlewares test
- Loading branch information
1 parent
fd6db41
commit a860555
Showing
6 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,84 @@ | ||
import { Worker } from "near-workspaces"; | ||
import test from "ava"; | ||
|
||
test.beforeEach(async (t) => { | ||
// Init the worker and start a Sandbox server | ||
const worker = await Worker.init(); | ||
|
||
// Prepare sandbox for tests, create accounts, deploy contracts, etx. | ||
const root = worker.rootAccount; | ||
|
||
// Deploy the contract. | ||
const middlewares = await root.devDeploy("build/middlewares.wasm"); | ||
|
||
// Create the init args. | ||
const args = JSON.stringify({ randomData: "anything" }); | ||
// Capture the result of the init function call. | ||
const result = await middlewares.callRaw(middlewares, "init", args); | ||
|
||
// Extract the logs. | ||
const { logs } = result.result.receipts_outcome[0].outcome; | ||
// Create the expected logs. | ||
const expectedLogs = [`Log from middleware: ${args}`]; | ||
|
||
// Check for correct logs. | ||
t.deepEqual(logs, expectedLogs); | ||
|
||
// Create test users | ||
const ali = await root.createSubAccount("ali"); | ||
|
||
// Save state for test runs | ||
t.context.worker = worker; | ||
t.context.accounts = { root, middlewares, ali }; | ||
}); | ||
|
||
test.afterEach.always(async (t) => { | ||
await t.context.worker.tearDown().catch((error) => { | ||
console.log("Failed to tear down the worker:", error); | ||
}); | ||
}); | ||
|
||
test("The middleware logs with call functions", async (t) => { | ||
const { ali, middlewares } = t.context.accounts; | ||
|
||
// Create the arguments which will be passed to the function. | ||
const args = JSON.stringify({ id: "1", text: "hello" }); | ||
// Call the function. | ||
const result = await ali.callRaw(middlewares, "add", args); | ||
// Extract the logs. | ||
const { logs } = result.result.receipts_outcome[0].outcome; | ||
// Create the expected logs. | ||
const expectedLogs = [`Log from middleware: ${args}`]; | ||
|
||
t.deepEqual(logs, expectedLogs); | ||
}); | ||
|
||
test("The middleware logs with view functions", async (t) => { | ||
const { ali, middlewares } = t.context.accounts; | ||
|
||
// Create the arguments which will be passed to the function. | ||
const args = JSON.stringify({ id: "1", accountId: "hello" }); | ||
// Call the function. | ||
const result = await ali.callRaw(middlewares, "get", args); | ||
// Extract the logs. | ||
const { logs } = result.result.receipts_outcome[0].outcome; | ||
// Create the expected logs. | ||
const expectedLogs = [`Log from middleware: ${args}`]; | ||
|
||
t.deepEqual(logs, expectedLogs); | ||
}); | ||
|
||
test("The middleware logs with private functions", async (t) => { | ||
const { ali, middlewares } = t.context.accounts; | ||
|
||
// Create the arguments which will be passed to the function. | ||
const args = { id: "test", accountId: "tset" }; | ||
// Call the function. | ||
const result = await ali.callRaw(middlewares, "get_private", ""); | ||
// Extract the logs. | ||
const { logs } = result.result.receipts_outcome[0].outcome; | ||
// Create the expected logs. | ||
const expectedLogs = [`Log from middleware: ${args}`]; | ||
|
||
t.deepEqual(logs, expectedLogs); | ||
}); |
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,45 @@ | ||
import { NearBindgen, near, call, view } from "near-sdk-js"; | ||
import { initialize, middleware } from "../../lib/near-bindgen"; | ||
|
||
@NearBindgen({ requireInit: true }) | ||
export class Contract { | ||
@initialize({}) | ||
@middleware({ | ||
call: (args) => near.log(`Log from middleware: ${args}`), | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
init({ randomData: _ }: { randomData: string }) {} | ||
|
||
@call({}) | ||
@middleware({ | ||
call: (args) => near.log(`Log from middleware: ${args}`), | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
add({ id: _, text: _t }: { id: string; text: string }) {} | ||
|
||
@view({}) | ||
@middleware({ | ||
call: (args) => near.log(`Log from middleware: ${args}`), | ||
}) | ||
get({ id, accountId }: { id: string; accountId: string }): { | ||
id: string; | ||
accountId: string; | ||
} { | ||
return { id: accountId, accountId: id }; | ||
} | ||
|
||
@view({}) | ||
get_private(): { id: string; accountId: string } { | ||
return this.getFromPrivate({ id: "test", accountId: "tset" }); | ||
} | ||
|
||
@middleware({ | ||
call: (args) => near.log(`Log from middleware: ${args}`), | ||
}) | ||
getFromPrivate({ id, accountId }: { id: string; accountId: string }): { | ||
id: string; | ||
accountId: string; | ||
} { | ||
return { id, accountId }; | ||
} | ||
} |