-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathindex.test.ts
More file actions
71 lines (66 loc) · 2.88 KB
/
Copy pathindex.test.ts
File metadata and controls
71 lines (66 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import "mocha";
import * as chai from "chai";
import { safeJsonParse, safeJsonStringify } from "../src";
const json = { something: true };
const valid = JSON.stringify(json);
const invalid = "{something:true}";
const str = "hello world";
const missing = { something: true, missing: undefined };
describe("@walletconnect/safe-json", () => {
describe("safeJsonParse", () => {
it("should throw when value is not a string", () => {
chai
.expect(() => safeJsonParse(json as any))
.to.throw("Cannot safe json parse value of type object");
});
it("should return an object when valid json", () => {
const result = safeJsonParse(valid);
chai.expect(result).to.eql(json);
});
it("should return the same input when invalid json", () => {
const result = safeJsonParse(invalid);
chai.expect(result).to.eql(invalid);
});
it("should handle bigint", () => {
const result = safeJsonParse(safeJsonStringify({ bigint: BigInt(1) }));
chai.expect(result).to.deep.eq({ bigint: BigInt(1) });
});
it("should handle negative bigint", () => {
const result = safeJsonParse(safeJsonStringify({ amount: BigInt(-100) }));
chai.expect(result).to.deep.eq({ amount: BigInt(-100) });
});
it("should handle number inside string literal. Case 1", () => {
const nested = '{"x":"12345678901234567,"}';
const result = safeJsonParse(nested);
chai.expect(result).to.deep.eq(JSON.parse(nested));
const big = safeJsonParse(safeJsonStringify({ bigint: BigInt(1) }));
chai.expect(big).to.deep.eq({ bigint: BigInt(1) });
});
it("should handle number inside string literal. Case 2", () => {
const nested =
'{"params":{"proposer":{"metadata":{"description":"Trade Any Token on DODOEX. Swap ETH to WETH at 0.99852536006139370845107244063040676283327993685155310925333096461126073315184832, 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE, 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"}}}}';
const result = safeJsonParse(nested);
chai.expect(result).to.deep.eq(JSON.parse(nested));
});
it("should handle BigInt", () => {
const bigIntId = "1702044452707006208";
const data = `{"id":${bigIntId},"jsonrpc":"2.0","result":"cb0072aaf3f3aab9b5a334f3a273634a2a6d2bee5fe7be205dc9f30f032c9734"}`;
const result = safeJsonParse(data);
chai.expect(result.id).to.deep.eq(BigInt(bigIntId));
});
});
describe("safeJsonStringify", () => {
it("should return a stringfied json", () => {
const result = safeJsonStringify(json);
chai.expect(result).to.eql(valid);
});
it("should ignored undefined values", () => {
const result = safeJsonStringify(missing);
chai.expect(result).to.eql(valid);
});
it("should return input when already a string", () => {
const result = safeJsonStringify(str);
chai.expect(result).to.eql(str);
});
});
});