Skip to content

Commit dc5b33b

Browse files
committed
Add some tests for Element.
1 parent 702a061 commit dc5b33b

File tree

16 files changed

+1026
-31
lines changed

16 files changed

+1026
-31
lines changed

django_unicorn/static/js/component.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@ import { debounce } from "./delayers.js";
33
import { Element } from "./element.js";
44
import morphdom from "./morphdom/2.6.1/morphdom.js"
55

6+
export const MORPHDOM_OPTIONS = {
7+
childrenOnly: false,
8+
// eslint-disable-next-line consistent-return
9+
getNodeKey(node) {
10+
// A node's unique identifier. Used to rearrange elements rather than
11+
// creating and destroying an element that already exists.
12+
if (node.attributes) {
13+
const key = node.getAttribute("unicorn:key") || node.id;
14+
15+
if (key) {
16+
return key;
17+
}
18+
}
19+
},
20+
// eslint-disable-next-line consistent-return
21+
onBeforeElUpdated(fromEl, toEl) {
22+
// Because morphdom also supports vDom nodes, it uses isSameNode to detect
23+
// sameness. When dealing with DOM nodes, we want isEqualNode, otherwise
24+
// isSameNode will ALWAYS return false.
25+
if (fromEl.isEqualNode(toEl)) {
26+
return false;
27+
}
28+
},
29+
};
30+
631
/**
732
* Encapsulate component.
833
*/
@@ -259,7 +284,8 @@ export class Component {
259284

260285
/**
261286
* Sets all model values.
262-
* @param {Object} elementToExclude Prevent a particular element from being updated. Object example: `{id: 'elementId', key: 'elementKey'}`.
287+
* @param {Object} elementToExclude Prevent a particular element from being updated.
288+
* Object example: `{id: 'elementId', key: 'elementKey'}`.
263289
*/
264290
setModelValues(elementToExclude) {
265291
elementToExclude = elementToExclude || {};
@@ -353,31 +379,7 @@ export class Component {
353379
_component.errors = responseJson.errors || {};
354380
const rerenderedComponent = responseJson.dom;
355381

356-
const morphdomOptions = {
357-
childrenOnly: false,
358-
getNodeKey(node) {
359-
// A node's unique identifier. Used to rearrange elements rather than
360-
// creating and destroying an element that already exists.
361-
if (node.attributes) {
362-
const key = node.getAttribute("unicorn:key") || node.id;
363-
364-
if (key) {
365-
return key;
366-
}
367-
}
368-
},
369-
onBeforeElUpdated(fromEl, toEl) {
370-
// Because morphdom also supports vDom nodes, it uses isSameNode to detect
371-
// sameness. When dealing with DOM nodes, we want isEqualNode, otherwise
372-
// isSameNode will ALWAYS return false.
373-
if (fromEl.isEqualNode(toEl)) {
374-
return false;
375-
}
376-
},
377-
};
378-
379-
// eslint-disable-next-line no-undef
380-
morphdom(_component.root, rerenderedComponent, morphdomOptions);
382+
morphdom(_component.root, rerenderedComponent, MORPHDOM_OPTIONS);
381383

382384
// Refresh the checksum based on the new data
383385
_component.refreshChecksum();

django_unicorn/static/js/element.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class Element {
7171
}
7272

7373
if (attribute.isError) {
74-
const code = attribute.name.replace("unicorn:name:", "");
74+
const code = attribute.name.replace("unicorn:error:", "");
7575
this.errors.push({ code, message: attribute.value });
7676
}
7777
}
@@ -136,7 +136,7 @@ export class Element {
136136
*/
137137
removeErrors() {
138138
this.errors.forEach((error) => {
139-
this.el.removeAttribute(error.code);
139+
this.el.removeAttribute(`unicorn:error:${error.code}`);
140140
});
141141

142142
this.errors = [];

django_unicorn/static/js/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
* Checks if an object is empty. Useful for check if a dictionary has any values.
33
*/
44
export function isEmpty(obj) {
5-
return typeof obj === "undefined" || (Object.keys(obj).length === 0 && obj.constructor === Object);
5+
return typeof obj === "undefined" || obj === null || (Object.keys(obj).length === 0 && obj.constructor === Object);
66
}
77

88
/**
99
* Checks if a string has the search text.
1010
*/
1111
export function contains(str, search) {
12+
if (!str) {
13+
return false;
14+
}
15+
1216
return str.indexOf(search) > -1;
1317
}
1418

0 commit comments

Comments
 (0)