From 8e337ad6b14f2ef2badc0aa4e6ce8da6a415fbb9 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 24 Sep 2020 10:09:20 +0200 Subject: [PATCH] Add sortJson tests --- packages/launchpad/src/encoding.spec.ts | 56 ++++++++++++++++++++++++- packages/launchpad/src/encoding.ts | 3 +- packages/launchpad/types/encoding.d.ts | 1 + 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/packages/launchpad/src/encoding.spec.ts b/packages/launchpad/src/encoding.spec.ts index ccdb8a4a..8fc85df6 100644 --- a/packages/launchpad/src/encoding.spec.ts +++ b/packages/launchpad/src/encoding.spec.ts @@ -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, + ]); + }); + }); +}); diff --git a/packages/launchpad/src/encoding.ts b/packages/launchpad/src/encoding.ts index 81bafa2f..df041d12 100644 --- a/packages/launchpad/src/encoding.ts +++ b/packages/launchpad/src/encoding.ts @@ -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; } diff --git a/packages/launchpad/types/encoding.d.ts b/packages/launchpad/types/encoding.d.ts index 05f8d8ce..1144a81d 100644 --- a/packages/launchpad/types/encoding.d.ts +++ b/packages/launchpad/types/encoding.d.ts @@ -1,5 +1,6 @@ import { Msg } from "./msgs"; import { StdFee } from "./types"; +export declare function sortJson(json: any): any; /** * The document to be signed *