Skip to content

Commit 6152afd

Browse files
committed
maint(pat-depends): Always throw update event and add changed dom structure.
Trigger pat-update on pat-depends itself and add updated dom structure to pat-update event data. This allows other patterns to also listen to changes in pat-depends. Goes together with the previous change on pat-autofocus. Related: #1092
1 parent fde478c commit 6152afd

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

src/pat/depends/depends.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ export default Base.extend({
9999
} else if (this.$el.is("a")) {
100100
this.$el.off("click.patternDepends");
101101
}
102-
if (this.$el.hasClass("pat-autosuggest")) {
103-
this.$el.findInclusive("input.pat-autosuggest").trigger("pat-update", {
104-
pattern: "depends",
105-
enabled: true,
106-
});
107-
}
108102
this.$el.removeClass("disabled");
103+
this.$el.trigger("pat-update", {
104+
pattern: "depends",
105+
action: "attribute-changed",
106+
dom: this.$el[0],
107+
enabled: true,
108+
});
109109
},
110110

111111
disable() {
@@ -114,13 +114,13 @@ export default Base.extend({
114114
} else if (this.$el.is("a")) {
115115
this.$el.on("click.patternDepends", (e) => e.preventDefault());
116116
}
117-
if (this.$el.hasClass("pat-autosuggest")) {
118-
this.$el.findInclusive("input.pat-autosuggest").trigger("pat-update", {
119-
pattern: "depends",
120-
enabled: false,
121-
});
122-
}
123117
this.$el.addClass("disabled");
118+
this.$el.trigger("pat-update", {
119+
pattern: "depends",
120+
action: "attribute-changed",
121+
dom: this.$el[0],
122+
enabled: false,
123+
});
124124
},
125125

126126
onChange(event) {

src/pat/depends/depends.test.js

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import pattern from "./depends";
33
import utils from "../../core/utils";
44

55
describe("pat-depends", function () {
6-
describe("init", function () {
6+
describe("1 - init", function () {
77
beforeEach(function () {
88
$("<div/>", { id: "lab" }).appendTo(document.body);
99
});
@@ -40,7 +40,7 @@ describe("pat-depends", function () {
4040
});
4141
});
4242

43-
describe("disable", function () {
43+
describe("2 - disable", function () {
4444
beforeEach(function () {
4545
$("<div/>", { id: "lab" }).appendTo(document.body);
4646
});
@@ -84,7 +84,7 @@ describe("pat-depends", function () {
8484
});
8585
});
8686

87-
describe("enable", function () {
87+
describe("3 - enable", function () {
8888
beforeEach(function () {
8989
$("<div/>", { id: "lab" }).appendTo(document.body);
9090
});
@@ -128,4 +128,63 @@ describe("pat-depends", function () {
128128
expect($._data($dependent[0]).events).toBe(undefined);
129129
});
130130
});
131+
132+
describe("4 - pat-update", function () {
133+
it("4.1 - Throw pat-update on enabling", async function () {
134+
document.body.innerHTML = `
135+
<input
136+
type="checkbox"
137+
id="control"
138+
value="yes"
139+
checked="checked"/>
140+
<button
141+
id="dependent"
142+
type="button"
143+
class="pat-depends"
144+
data-pat-depends="condition: control"
145+
>Click me</button>
146+
`;
147+
const el = document.querySelector(".pat-depends");
148+
const instance = new pattern(el);
149+
await utils.timeout(1); // wait a tick for async to settle.
150+
151+
let data;
152+
$(el).on("pat-update", (e, d) => {
153+
data = d;
154+
});
155+
instance.enable();
156+
expect(data.pattern).toBe("depends");
157+
expect(data.action).toBe("attribute-changed");
158+
expect(data.dom).toBe(el);
159+
expect(data.enabled).toBe(true);
160+
});
161+
it("4.2 - Throw pat-update on disabling", async function () {
162+
document.body.innerHTML = `
163+
<input
164+
type="checkbox"
165+
id="control"
166+
value="yes"
167+
checked="checked"/>
168+
<button
169+
id="dependent"
170+
type="button"
171+
class="pat-depends"
172+
data-pat-depends="condition: control"
173+
>Click me</button>
174+
`;
175+
const el = document.querySelector(".pat-depends");
176+
const instance = new pattern(el);
177+
await utils.timeout(1); // wait a tick for async to settle.
178+
179+
let data;
180+
$(el).on("pat-update", (e, d) => {
181+
data = d;
182+
});
183+
instance.disable();
184+
expect(data.pattern).toBe("depends");
185+
expect(data.action).toBe("attribute-changed");
186+
expect(data.dom).toBe(el);
187+
expect(data.enabled).toBe(false);
188+
});
189+
});
131190
});

0 commit comments

Comments
 (0)