Replaced angular.toJson with Utilities.toJson [#CanConHackathon] (#7924)

This commit is contained in:
Matthew-Wise
2020-04-10 17:07:37 +01:00
committed by GitHub
parent 1e851b615f
commit 76e4b6b301
8 changed files with 93 additions and 9 deletions

View File

@@ -0,0 +1,55 @@
(function () {
describe("Utilities", function () {
describe("toJson", function () {
it("should delegate to JSON.stringify", function () {
var spy = spyOn(JSON, "stringify").and.callThrough();
expect(Utilities.toJson({})).toEqual("{}");
expect(spy).toHaveBeenCalled();
});
it("should format objects pretty", function () {
expect(Utilities.toJson({ a: 1, b: 2 }, true)).toBe(
'{\n "a": 1,\n "b": 2\n}'
);
expect(Utilities.toJson({ a: { b: 2 } }, true)).toBe(
'{\n "a": {\n "b": 2\n }\n}'
);
expect(Utilities.toJson({ a: 1, b: 2 }, false)).toBe('{"a":1,"b":2}');
expect(Utilities.toJson({ a: 1, b: 2 }, 0)).toBe('{"a":1,"b":2}');
expect(Utilities.toJson({ a: 1, b: 2 }, 1)).toBe(
'{\n "a": 1,\n "b": 2\n}'
);
expect(Utilities.toJson({ a: 1, b: 2 }, {})).toBe(
'{\n "a": 1,\n "b": 2\n}'
);
});
it("should not serialize properties starting with $$", function () {
expect(Utilities.toJson({ $$some: "value" }, false)).toEqual("{}");
});
it("should serialize properties starting with $", function () {
expect(Utilities.toJson({ $few: "v" }, false)).toEqual('{"$few":"v"}');
});
it("should not serialize $window object", function () {
expect(Utilities.toJson(window)).toEqual('"$WINDOW"');
});
it("should not serialize $document object", function () {
expect(Utilities.toJson(document)).toEqual('"$DOCUMENT"');
});
it("should not serialize scope instances", inject(function (
$rootScope
) {
expect(Utilities.toJson({ key: $rootScope })).toEqual('{"key":"$SCOPE"}');
}));
it("should serialize undefined as undefined", function () {
expect(Utilities.toJson(undefined)).toEqual(undefined);
});
});
});
})();