Repository navigation
Expand file tree
/
Copy pathapi_spec.ts
More file actions
67 lines (52 loc) · 1.77 KB
/
Copy pathapi_spec.ts
File metadata and controls
67 lines (52 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {refreshStory, updateStory} from "helpers/api";
describe("updateStory", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("sends a PUT with JSON body and CSRF token", async () => {
const mockResponse = new Response("", {status: 200});
const fetchSpy = vi.spyOn(globalThis, "fetch").
mockResolvedValue(mockResponse);
const meta = document.createElement("meta");
meta.name = "csrf-token";
meta.content = "test-token";
document.head.appendChild(meta);
await updateStory("42", {is_starred: true});
expect(fetchSpy).toHaveBeenCalledWith("/stories/42", {
body: JSON.stringify({is_starred: true}),
headers: {
"Content-Type": "application/json",
"X-CSRF-Token": "test-token",
},
method: "PUT",
});
meta.remove();
});
it("sends empty CSRF token when meta tag is missing", async () => {
const fetchSpy = vi.spyOn(globalThis, "fetch").
mockResolvedValue(new Response());
await updateStory("1", {is_starred: false});
expect(fetchSpy).toHaveBeenCalledTimes(1);
const {headers} = fetchSpy.mock.calls[0]?.[1] ?? {};
expect(headers).toHaveProperty("X-CSRF-Token", "");
});
});
describe("refreshStory", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("sends a POST with CSRF token", async () => {
const fetchSpy = vi.spyOn(globalThis, "fetch").
mockResolvedValue(new Response("{}", {status: 200}));
const meta = document.createElement("meta");
meta.name = "csrf-token";
meta.content = "test-token";
document.head.appendChild(meta);
await refreshStory("42");
expect(fetchSpy).toHaveBeenCalledWith("/stories/42/refresh", {
headers: {"X-CSRF-Token": "test-token"},
method: "POST",
});
meta.remove();
});
});