Add sortJson tests

This commit is contained in:
Simon Warta 2020-09-24 10:09:20 +02:00
parent 3edc6d51e7
commit 8e337ad6b1
3 changed files with 58 additions and 2 deletions

View File

@ -1 +1,55 @@
describe("encoding", () => {});
import { sortJson } from "./encoding";
describe("encoding", () => {
describe("sortJson", () => {
it("leaves non-objects unchanged", () => {
expect(sortJson(true)).toEqual(true);
expect(sortJson(false)).toEqual(false);
expect(sortJson("aabbccdd")).toEqual("aabbccdd");
expect(sortJson(75)).toEqual(75);
expect(sortJson(null)).toEqual(null);
expect(sortJson([5, 6, 7, 1])).toEqual([5, 6, 7, 1]);
expect(sortJson([5, ["a", "b"], true, null, 1])).toEqual([5, ["a", "b"], true, null, 1]);
});
it("sorts objects by key", () => {
// already sorted
expect(sortJson({})).toEqual({});
expect(sortJson({ a: 3 })).toEqual({ a: 3 });
expect(sortJson({ a: 3, b: 2, c: 1 })).toEqual({ a: 3, b: 2, c: 1 });
// not yet sorted
expect(sortJson({ b: 2, a: 3, c: 1 })).toEqual({ a: 3, b: 2, c: 1 });
expect(sortJson({ aaa: true, aa: true, a: true })).toEqual({ a: true, aa: true, aaa: true });
});
it("sorts nested objects", () => {
// already sorted
expect(sortJson({ x: { y: { z: null } } })).toEqual({ x: { y: { z: null } } });
// not yet sorted
expect(sortJson({ b: { z: true, x: true, y: true }, a: true, c: true })).toEqual({
a: true,
b: { x: true, y: true, z: true },
c: true,
});
});
it("sorts objects in arrays", () => {
// already sorted
expect(sortJson([1, 2, { x: { y: { z: null } } }, 4])).toEqual([1, 2, { x: { y: { z: null } } }, 4]);
// not yet sorted
expect(sortJson([1, 2, { b: { z: true, x: true, y: true }, a: true, c: true }, 4])).toEqual([
1,
2,
{
a: true,
b: { x: true, y: true, z: true },
c: true,
},
4,
]);
});
});
});

View File

@ -5,7 +5,8 @@ import { Uint53 } from "@cosmjs/math";
import { Msg } from "./msgs";
import { StdFee } from "./types";
function sortJson(json: any): any {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function sortJson(json: any): any {
if (typeof json !== "object" || json === null) {
return json;
}

View File

@ -1,5 +1,6 @@
import { Msg } from "./msgs";
import { StdFee } from "./types";
export declare function sortJson(json: any): any;
/**
* The document to be signed
*