Skip to content

Commit 0a9196b

Browse files
committed
Use strict equality by casting the variable to a string.
1 parent 660e0f5 commit 0a9196b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/django_unicorn/static/unicorn/js/unicorn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function getComponent(componentNameOrKey) {
9191
if (isEmpty(component)) {
9292
const _component = components[id];
9393

94-
if (_component.key == componentNameOrKey) {
94+
if (String(_component.key) === String(componentNameOrKey)) {
9595
component = _component;
9696
}
9797
}

tests/js/unicorn/issue_728.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import test from "ava";
2+
import { getComponent } from "../../../django_unicorn/static/unicorn/js/unicorn.js";
3+
import { components } from "../../../django_unicorn/static/unicorn/js/store.js";
4+
import { getComponent as getComponentUtil } from "../utils.js";
5+
6+
test("getComponent by key with type mismatch (existing string key, int lookup)", (t) => {
7+
const component = getComponentUtil();
8+
component.key = "123";
9+
components[component.id] = component;
10+
11+
// Should find it even if we pass an integer
12+
t.truthy(getComponent(123));
13+
});
14+
15+
test("getComponent by key with type mismatch (existing int key, string lookup)", (t) => {
16+
const component = getComponentUtil();
17+
component.key = 456;
18+
components[component.id] = component;
19+
20+
// Should find it even if we pass a string
21+
t.truthy(getComponent("456"));
22+
});

0 commit comments

Comments
 (0)