|
| 1 | +import { Angular } from "../../angular.js"; |
| 2 | +import { CookieProvider, CookieService } from "./cookie.js"; |
| 3 | + |
| 4 | +describe("$cookie service", () => { |
| 5 | + let $injector, $cookie, el, cookieProvider; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + el = document.createElement("div"); |
| 9 | + document.body.appendChild(el); |
| 10 | + clearCookies(); |
| 11 | + const angular = new Angular(); |
| 12 | + angular.module("default", []).config(($cookieProvider) => { |
| 13 | + cookieProvider = $cookieProvider; |
| 14 | + }); |
| 15 | + |
| 16 | + // bootstrap the module that already provides $cookie |
| 17 | + $injector = angular.bootstrap(el, ["default"]); |
| 18 | + $cookie = $injector.get("$cookie"); |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + clearCookies(); |
| 23 | + el.remove(); |
| 24 | + }); |
| 25 | + |
| 26 | + // --------------------------- Tests --------------------------- |
| 27 | + it("should be defined", () => { |
| 28 | + expect($cookie).toBeDefined(); |
| 29 | + expect($cookie instanceof CookieService).toBeTrue(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("is available for configuration during config phase", () => { |
| 33 | + expect(cookieProvider).toBeDefined(); |
| 34 | + expect(cookieProvider instanceof CookieProvider).toBeTrue(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should set and get a raw cookie", () => { |
| 38 | + $cookie.put("foo", "bar"); |
| 39 | + expect($cookie.get("foo")).toBe("bar"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should remove a cookie", () => { |
| 43 | + $cookie.put("temp", "123"); |
| 44 | + $cookie.remove("temp"); |
| 45 | + expect($cookie.get("temp")).toBeNull(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should store and retrieve objects via putObject/getObject", () => { |
| 49 | + const obj = { id: 1, name: "Alice" }; |
| 50 | + $cookie.putObject("user", obj); |
| 51 | + expect($cookie.getObject("user")).toEqual(obj); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should return null for invalid JSON in getObject", () => { |
| 55 | + document.cookie = "broken={unclosed"; |
| 56 | + expect($cookie.getObject("broken")).toBeNull(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("getAll should return all cookies as an object", () => { |
| 60 | + $cookie.put("x", "10"); |
| 61 | + $cookie.put("y", "20"); |
| 62 | + const all = $cookie.getAll(); |
| 63 | + expect(all.x).toBe("10"); |
| 64 | + expect(all.y).toBe("20"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should maintain multiple cookies independently", () => { |
| 68 | + $cookie.put("a", "1"); |
| 69 | + $cookie.put("b", "2"); |
| 70 | + expect($cookie.get("a")).toBe("1"); |
| 71 | + expect($cookie.get("b")).toBe("2"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should merge user options with defaults safely", () => { |
| 75 | + // Only use options that work locally |
| 76 | + const opts = { path: "/src/services/cookie/cookie.html", samesite: "Lax" }; |
| 77 | + |
| 78 | + expect(() => $cookie.put("optTest", "v", opts)).not.toThrow(); |
| 79 | + expect($cookie.get("optTest")).toBe("v"); // Should succeed |
| 80 | + }); |
| 81 | + |
| 82 | + it("should round-trip putObject/getObject correctly", () => { |
| 83 | + const obj = { foo: "bar", num: 42 }; |
| 84 | + $cookie.putObject("objTest", obj); |
| 85 | + expect($cookie.getObject("objTest")).toEqual(obj); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should remove a cookie with options applied without error", () => { |
| 89 | + $cookie.put("removeTest", "val", { path: "/" }); |
| 90 | + expect($cookie.get("removeTest")).toBe("val"); |
| 91 | + expect(() => $cookie.remove("removeTest", { path: "/" })).not.toThrow(); |
| 92 | + expect($cookie.get("removeTest")).toBeNull(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should handle multiple putObject and getObject independently", () => { |
| 96 | + $cookie.putObject("o1", { a: 1 }); |
| 97 | + $cookie.putObject("o2", { b: 2 }); |
| 98 | + expect($cookie.getObject("o1")).toEqual({ a: 1 }); |
| 99 | + expect($cookie.getObject("o2")).toEqual({ b: 2 }); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should handle setting and removing multiple cookies reliably", () => { |
| 103 | + $cookie.put("c1", "v1"); |
| 104 | + $cookie.put("c2", "v2"); |
| 105 | + $cookie.remove("c1"); |
| 106 | + expect($cookie.get("c1")).toBeNull(); |
| 107 | + expect($cookie.get("c2")).toBe("v2"); |
| 108 | + }); |
| 109 | + |
| 110 | + function clearCookies() { |
| 111 | + const cookies = document.cookie.split(";").map((c) => c.trim()); |
| 112 | + cookies.forEach((c) => { |
| 113 | + const eq = c.indexOf("="); |
| 114 | + if (eq > -1) { |
| 115 | + const key = decodeURIComponent(c.substring(0, eq)); |
| 116 | + |
| 117 | + // Delete at root path |
| 118 | + document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`; |
| 119 | + |
| 120 | + // Delete at current path |
| 121 | + document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=${location.pathname}`; |
| 122 | + |
| 123 | + // Optionally, delete at multiple levels of parent paths |
| 124 | + const pathParts = location.pathname.split("/").filter(Boolean); |
| 125 | + let pathAcc = ""; |
| 126 | + pathParts.forEach((part) => { |
| 127 | + pathAcc += "/" + part; |
| 128 | + document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=${pathAcc}`; |
| 129 | + }); |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | +}); |
0 commit comments