mirror of
https://github.com/fluencelabs/examples
synced 2025-06-16 03:21:21 +00:00
feat!: Replace old fluence-js with JS Client (#425)
This commit is contained in:
14
js-client-examples/hello-world/src/__test__/test.spec.ts
Normal file
14
js-client-examples/hello-world/src/__test__/test.spec.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { main } from '../main.js';
|
||||
|
||||
describe('smoke test', () => {
|
||||
it('should work', async () => {
|
||||
console.log = jest.fn();
|
||||
|
||||
await main();
|
||||
|
||||
expect(console.log).toBeCalledTimes(3);
|
||||
expect(console.log).toHaveBeenNthCalledWith(1, 'Hello, world!');
|
||||
expect(console.log).toHaveBeenNthCalledWith(2, 'Wealth awaits you very soon.');
|
||||
expect(console.log).toHaveBeenNthCalledWith(3, 'The relay time is: ', expect.anything());
|
||||
}, 15000);
|
||||
});
|
3
js-client-examples/hello-world/src/index.ts
Normal file
3
js-client-examples/hello-world/src/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { main } from './main.js';
|
||||
|
||||
main().catch((e) => console.error('error: ', e));
|
30
js-client-examples/hello-world/src/main.ts
Normal file
30
js-client-examples/hello-world/src/main.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import '@fluencelabs/js-client.node';
|
||||
import { Fluence } from '@fluencelabs/js-client.api';
|
||||
import { randomKras } from '@fluencelabs/fluence-network-environment';
|
||||
import { registerHelloWorld, sayHello, getRelayTime, tellFortune } from './_aqua/hello-world.js';
|
||||
|
||||
export async function main() {
|
||||
await Fluence.connect(randomKras());
|
||||
|
||||
registerHelloWorld({
|
||||
hello: (str) => {
|
||||
console.log(str);
|
||||
},
|
||||
getFortune: async () => {
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(resolve, 1000);
|
||||
});
|
||||
return 'Wealth awaits you very soon.';
|
||||
},
|
||||
});
|
||||
|
||||
await sayHello();
|
||||
|
||||
console.log(await tellFortune());
|
||||
|
||||
const relayTime = await getRelayTime();
|
||||
|
||||
console.log('The relay time is: ', new Date(relayTime).toLocaleString());
|
||||
|
||||
await Fluence.disconnect();
|
||||
}
|
Reference in New Issue
Block a user