summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/utils/dom_utils_spec.js
diff options
context:
space:
mode:
Diffstat (limited to 'spec/frontend/lib/utils/dom_utils_spec.js')
-rw-r--r--spec/frontend/lib/utils/dom_utils_spec.js50
1 files changed, 49 insertions, 1 deletions
diff --git a/spec/frontend/lib/utils/dom_utils_spec.js b/spec/frontend/lib/utils/dom_utils_spec.js
index 10b4a10a8ff..d918016a5f4 100644
--- a/spec/frontend/lib/utils/dom_utils_spec.js
+++ b/spec/frontend/lib/utils/dom_utils_spec.js
@@ -1,4 +1,9 @@
-import { addClassIfElementExists, canScrollUp, canScrollDown } from '~/lib/utils/dom_utils';
+import {
+ addClassIfElementExists,
+ canScrollUp,
+ canScrollDown,
+ parseBooleanDataAttributes,
+} from '~/lib/utils/dom_utils';
const TEST_MARGIN = 5;
@@ -112,4 +117,47 @@ describe('DOM Utils', () => {
expect(canScrollDown(element, TEST_MARGIN)).toBe(false);
});
});
+
+ describe('parseBooleanDataAttributes', () => {
+ let element;
+
+ beforeEach(() => {
+ setFixtures('<div data-foo-bar data-baz data-qux="">');
+ element = document.querySelector('[data-foo-bar]');
+ });
+
+ it('throws if not given an element', () => {
+ expect(() => parseBooleanDataAttributes(null, ['baz'])).toThrow();
+ });
+
+ it('throws if not given an array of dataset names', () => {
+ expect(() => parseBooleanDataAttributes(element)).toThrow();
+ });
+
+ it('returns an empty object if given an empty array of names', () => {
+ expect(parseBooleanDataAttributes(element, [])).toEqual({});
+ });
+
+ it('correctly parses boolean-like data attributes', () => {
+ expect(
+ parseBooleanDataAttributes(element, [
+ 'fooBar',
+ 'foobar',
+ 'baz',
+ 'qux',
+ 'doesNotExist',
+ 'toString',
+ ]),
+ ).toEqual({
+ fooBar: true,
+ foobar: false,
+ baz: true,
+ qux: true,
+ doesNotExist: false,
+
+ // Ensure prototype properties aren't false positives
+ toString: false,
+ });
+ });
+ });
});