Skip to content

Commit e64ecac

Browse files
committed
maint(pat ajax): Add more tests for passing arguments.
1 parent 50c70fa commit e64ecac

File tree

1 file changed

+68
-7
lines changed

1 file changed

+68
-7
lines changed

src/pat/ajax/ajax.test.js

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
import registry from "../../core/registry";
12
import pattern from "./ajax";
23
import $ from "jquery";
34
import { jest } from "@jest/globals";
45

56
var $lab;
67

78
describe("pat-ajax", function () {
8-
beforeEach(function () {
9-
$lab = $("<div/>", { id: "lab" }).appendTo(document.body);
10-
});
9+
describe("anchor", function () {
10+
beforeEach(function () {
11+
$lab = $("<div/>", { id: "lab" }).appendTo(document.body);
12+
});
1113

12-
afterEach(function () {
13-
$("#lab").remove();
14-
});
14+
afterEach(function () {
15+
document.body.innerHTML = "";
16+
jest.restoreAllMocks();
17+
});
1518

16-
describe("anchor", function () {
1719
it("triggers ajax request on click", function () {
1820
var $a = $("<a href='href.html' />").appendTo($lab);
1921
pattern.init($a);
@@ -33,6 +35,7 @@ describe("pat-ajax", function () {
3335
var $form, $button, spy_ajax;
3436

3537
beforeEach(function () {
38+
$lab = $("<div/>", { id: "lab" }).appendTo(document.body);
3639
$form = $("<form action='action.html'></form>").appendTo($lab);
3740
$button = $(
3841
"<button type='submit' name='submit' value='submit' />"
@@ -42,6 +45,11 @@ describe("pat-ajax", function () {
4245
spy_ajax = jest.spyOn($, "ajax");
4346
});
4447

48+
afterEach(function () {
49+
document.body.innerHTML = "";
50+
jest.restoreAllMocks();
51+
});
52+
4553
it("triggers ajax request on submit", function () {
4654
$form.submit();
4755
expect(spy_ajax).toHaveBeenCalled();
@@ -75,4 +83,57 @@ describe("pat-ajax", function () {
7583
expect(ajaxargs.data).toEqual("input1=value1");
7684
});
7785
});
86+
87+
describe("Arguments can be set", function () {
88+
afterEach(function () {
89+
document.body.innerHTML = "";
90+
jest.restoreAllMocks();
91+
});
92+
93+
// URL
94+
it("Gets URL from anchor-href", function () {
95+
document.body.innerHTML = `<a class="pat-ajax" href="somewhere.html"/>`;
96+
registry.scan(document.body);
97+
jest.spyOn($, "ajax");
98+
document.body.querySelector(".pat-ajax").click();
99+
const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
100+
expect(ajaxargs.url).toEqual("somewhere.html");
101+
});
102+
it("Gets URL from form-action", function () {
103+
document.body.innerHTML = `<form class="pat-ajax" action="somewhere.html"/>`;
104+
registry.scan(document.body);
105+
jest.spyOn($, "ajax");
106+
$(".pat-ajax").submit();
107+
const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
108+
expect(ajaxargs.url).toEqual("somewhere.html");
109+
});
110+
it("Can explicitly set URL on anchor", function () {
111+
document.body.innerHTML = `
112+
<a
113+
class="pat-ajax"
114+
href="somewhere.html"
115+
data-pat-ajax="url: else.html"
116+
/>
117+
`;
118+
registry.scan(document.body);
119+
jest.spyOn($, "ajax");
120+
document.body.querySelector(".pat-ajax").click();
121+
const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
122+
expect(ajaxargs.url).toEqual("else.html");
123+
});
124+
it("Can explicitly set URL on form", function () {
125+
document.body.innerHTML = `
126+
<form
127+
class="pat-ajax"
128+
action="somewhere.html"
129+
data-pat-ajax="url: else.html"
130+
/>
131+
`;
132+
registry.scan(document.body);
133+
jest.spyOn($, "ajax");
134+
$(".pat-ajax").submit();
135+
const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
136+
expect(ajaxargs.url).toEqual("else.html");
137+
});
138+
});
78139
});

0 commit comments

Comments
 (0)