diff --git a/dapp/js/tp.ts b/dapp/js/tp.ts index 3e1ed88..290f5a6 100644 --- a/dapp/js/tp.ts +++ b/dapp/js/tp.ts @@ -73,6 +73,7 @@ const tpInfo = (() => { address = null; node = null; isConnectedState = null; + system = null; }; return { diff --git a/dapp/js/transaction.ts b/dapp/js/transaction.ts index 09dc7ab..136c243 100644 --- a/dapp/js/transaction.ts +++ b/dapp/js/transaction.ts @@ -23,14 +23,17 @@ const requestReceipt = async (hash: string): Promise => { try { res = await new Promise((resolve, reject) => { // limiting frequency because transaction needs some time to be confirmed - setTimeout(async () => { - try { - const res = await instance.moac.getTransactionReceipt(hash); - return resolve(res); - } catch (error) { - return reject(error); - } - }, 3000); + setTimeout( + async () => { + try { + const res = await instance.moac.getTransactionReceipt(hash); + return resolve(res); + } catch (error) { + return reject(error); + } + }, + /* istanbul ignore next */ process.env.NODE_ENV === "test" ? 0 : 3000 + ); }); console.log("res: ", res); } catch (error) { diff --git a/dapp/test/unit/specs/js/contract.spec.js b/dapp/test/unit/specs/js/contract.spec.js index c2c3687..c4d988c 100644 --- a/dapp/test/unit/specs/js/contract.spec.js +++ b/dapp/test/unit/specs/js/contract.spec.js @@ -585,7 +585,7 @@ describe("test contract.ts", () => { sandbox.restore(); }); - test("if request success", async () => { + test("if request success if system is android", async () => { const stub = sandbox.stub(tpInfo, "getAddress"); stub.resolves(config.testAddress); const stub1 = sandbox.stub(instance.moac, "getOptions"); @@ -601,6 +601,8 @@ describe("test contract.ts", () => { result: true, data: "test" }) + const stub4 = sandbox.stub(tpInfo, "getSystem"); + stub4.resolves("android"); const hash = await instance.sendTransactionByTp(config.testContract, "1", "0x00"); expect(hash).toBe(config.testHash); expect(stub1.calledOnceWithExactly({}, config.testAddress)); @@ -620,6 +622,43 @@ describe("test contract.ts", () => { })).toBe(true); }) + test("if request success if system is ios", async () => { + const stub = sandbox.stub(tpInfo, "getAddress"); + stub.resolves(config.testAddress); + const stub1 = sandbox.stub(instance.moac, "getOptions"); + stub1.resolves({ + nonce: "0", + gasLimit: "1000000", + gasPrice: "2000000000000000" + }) + const stub2 = sandbox.stub(Moac.prototype, "sendRawSignedTransaction"); + stub2.resolves(config.testHash); + const stub3 = sandbox.stub(tp, "signMoacTransaction"); + stub3.resolves({ + result: true, + data: "test" + }) + const stub4 = sandbox.stub(tpInfo, "getSystem"); + stub4.resolves("ios"); + const hash = await instance.sendTransactionByTp(config.testContract, "1", "0x00"); + expect(hash).toBe(config.testHash); + expect(stub1.calledOnceWithExactly({}, config.testAddress)); + expect(stub2.calledOnceWithExactly("test")).toBe(true); + expect(stub3.calledOnceWithExactly({ + chainId: '0x65', + data: '0x00', + from: '0x5edccedfe9952f5b828937b325bd1f132aa09f60', + gasLimit: '1000000', + gasPrice: '2000000000000000', + nonce: '0x0', + shardingFlag: '0x0', + systemContract: '0x0', + value: '1000000000000000000', + via: '0x', + to: '0x8eca41a83ea0efbd41401ed850774974bda6b697' + })).toBe(true); + }) + test("if request fail", async () => { const stub = sandbox.stub(tpInfo, "getAddress"); stub.resolves(config.testAddress); diff --git a/dapp/test/unit/specs/js/tp.spec.js b/dapp/test/unit/specs/js/tp.spec.js index 9e08088..ec23d87 100644 --- a/dapp/test/unit/specs/js/tp.spec.js +++ b/dapp/test/unit/specs/js/tp.spec.js @@ -108,4 +108,52 @@ describe("test tp.ts", () => { expect(stub.calledTwice).toBe(true); }) }) + + + describe("test getSystem", () => { + + afterEach(() => { + sandbox.restore(); + tpInfo.destroy(); + }); + + test("getSystem should be a function", () => { + expect(typeof tpInfo.getSystem).toBe("function") + }) + + test("return android when tokenpocket isn't connected", async () => { + const spy = sandbox.spy(tp, "getAppInfo"); + const system = await tpInfo.getSystem(); + expect(system).toBe("android"); + expect(spy.called).toBe(false); + }) + + test("getAppInfo should be called and only be called once if get success when tokenpocket is connected", async () => { + const stub = sandbox.stub(tp, "getAppInfo"); + stub.resolves({ result: true, data: { system: "ios" } }); + const stub1 = sandbox.stub(tp, "isConnected"); + stub1.returns(true); + let system = await tpInfo.getSystem(); + expect(system).toBe("ios"); + expect(stub.called).toBe(true); + system = await tpInfo.getSystem(); + expect(system).toBe("ios"); + expect(stub.calledOnce).toBe(true); + }) + + test("return null if get fail when tokenpocket is connected", async () => { + const stub = sandbox.stub(tp, "getAppInfo"); + stub.onCall(0).rejects(); + stub.onCall(1).resolves({}); + const stub1 = sandbox.stub(tp, "isConnected"); + stub1.returns(true); + let system = await tpInfo.getSystem(); + expect(system).toBe(null); + expect(stub.calledOnce).toBe(true); + tpInfo.destroy(); + system = await tpInfo.getSystem(); + expect(system).toBe(null); + expect(stub.calledTwice).toBe(true); + }) + }) }) \ No newline at end of file diff --git a/dapp/test/unit/specs/js/transaction.spec.js b/dapp/test/unit/specs/js/transaction.spec.js index 4115faf..c991fbc 100644 --- a/dapp/test/unit/specs/js/transaction.spec.js +++ b/dapp/test/unit/specs/js/transaction.spec.js @@ -24,8 +24,6 @@ describe("test transaction.ts", () => { }) test("if request fail more than tenth", async () => { - jest.setTimeout(35000); - const stub = sandbox.stub(tpInfo, "getNode"); stub.resolves(config.testNode); const stub1 = sandbox.stub(Moac.prototype, "getTransactionReceipt");