Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
GinMu committed Nov 8, 2019
1 parent 9cbe519 commit 2dfd5f7
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 11 deletions.
1 change: 1 addition & 0 deletions dapp/js/tp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const tpInfo = (() => {
address = null;
node = null;
isConnectedState = null;
system = null;
};

return {
Expand Down
19 changes: 11 additions & 8 deletions dapp/js/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ const requestReceipt = async (hash: string): Promise<any> => {
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) {
Expand Down
41 changes: 40 additions & 1 deletion dapp/test/unit/specs/js/contract.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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));
Expand All @@ -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);
Expand Down
48 changes: 48 additions & 0 deletions dapp/test/unit/specs/js/tp.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
})
})
2 changes: 0 additions & 2 deletions dapp/test/unit/specs/js/transaction.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 2dfd5f7

Please sign in to comment.