Skip to content

Commit 1509c92

Browse files
committed
tests
1 parent 45f87e9 commit 1509c92

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

src/services/cookie/cookie.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>AngularTS Test Runner</title>
6+
7+
<link rel="shortcut icon" type="image/png" href="/images/favicon.ico" />
8+
<link rel="stylesheet" href="/jasmine/jasmine.css" />
9+
<link rel="stylesheet" href="/public/jasmine-helper.css" />
10+
<script src="/jasmine/jasmine.js"></script>
11+
<script src="/jasmine/jasmine-html.js"></script>
12+
<script src="/jasmine/boot0.js"></script>
13+
<script src="/jasmine/boot1.js"></script>
14+
<script type="module" src="/src/services/cookie/cookie.spec.js"></script>
15+
</head>
16+
<body>
17+
<div id="app"></div>
18+
</body>
19+
</html>

src/services/cookie/cookie.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ function parseCookies() {
2121
}
2222

2323
/** Utility: stringify options */
24+
/**
25+
*
26+
* @param {CookieOptions} opts
27+
* @returns {string}
28+
*/
2429
function buildOptions(opts = {}) {
2530
let s = "";
2631

src/services/cookie/cookie.spec.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
});

src/services/cookie/cookie.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { test, expect } from "@playwright/test";
2+
3+
const TEST_URL = "src/services/cookie/cookie.html";
4+
5+
test("unit tests contain no errors", async ({ page }) => {
6+
await page.goto(TEST_URL);
7+
await page.content();
8+
await page.waitForTimeout(1000);
9+
await expect(page.locator(".jasmine-overall-result")).toHaveText(
10+
/ 0 failures/,
11+
);
12+
});

0 commit comments

Comments
 (0)