From 819e8bc25eae574e031ec9191f623af0271c7f6c Mon Sep 17 00:00:00 2001 From: Feoktist Shovchko Date: Wed, 19 Jul 2023 23:35:43 +0300 Subject: [PATCH 1/2] test(esl-utils): test coverage for utils/dom/attr --- src/modules/esl-utils/dom/test/attr.test.ts | 121 ++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/modules/esl-utils/dom/test/attr.test.ts diff --git a/src/modules/esl-utils/dom/test/attr.test.ts b/src/modules/esl-utils/dom/test/attr.test.ts new file mode 100644 index 000000000..440bbf3ab --- /dev/null +++ b/src/modules/esl-utils/dom/test/attr.test.ts @@ -0,0 +1,121 @@ +import {hasAttr, getAttr, setAttr} from '../attr'; + +describe('Attribute', () => { + const attrName = 'test-attr'; + const attrValue = 'test-value'; + const attrFallback = 'test-fallback'; + + describe('hasAttr', () => { + test('false when attribute is not present', () => expect(hasAttr(document.createElement('div'), attrName)).toBe(false)); + + test('false for non-valid element', () => expect(hasAttr(null as any as Element, attrName)).toBe(false)); + + test('true when attribute is present', () => { + const $el = document.createElement('div'); + $el.setAttribute(attrName, attrValue); + + expect(hasAttr($el, attrName)).toBe(true); + }); + + test('true for an array of elements with set attribute', () => { + const $el1 = document.createElement('div'); + const $el2 = document.createElement('div'); + $el1.setAttribute(attrName, attrValue); + $el2.setAttribute(attrName, attrValue); + + expect(hasAttr([$el1, $el2], attrName)).toBe(true); + }); + + test('false for an array of elements without set attribute', () => { + const $el1 = document.createElement('div'); + const $el2 = document.createElement('div'); + $el1.setAttribute(attrName, attrValue); + + expect(hasAttr([$el1, $el2], attrName)).toBe(false); + }); + }); + + describe('getAttr', () => { + test('attribute value when attribute is present', () => { + const $el = document.createElement('div'); + $el.setAttribute(attrName, attrValue); + + expect(getAttr($el, attrName)).toBe(attrValue); + }); + + test('null when attribute is not present', () => expect(getAttr(document.createElement('div'), attrName)).toBe(null)); + + test('null for non-element even with fallback value', () => expect(getAttr(null as any as Element, attrName, attrFallback)).toBe(null)); + + test('fallback value when attribute is not present', () => expect(getAttr(document.createElement('div'), attrName, attrFallback)).toBe(attrFallback)); + + test('null by looking up first element`s attribute', () => { + const $el2 = document.createElement('div'); + const $el1 = document.createElement('div'); + $el2.setAttribute(attrName, attrValue); + + expect(getAttr([$el1, $el2], attrName)).toBe(null); + }); + + test('attribute value of first element', () => { + const attrValue2 = 'test-value-2'; + const $el2 = document.createElement('div'); + const $el1 = document.createElement('div'); + $el1.setAttribute(attrName, attrValue); + $el2.setAttribute(attrName, attrValue2); + + expect(getAttr([$el1, $el2], attrName)).toBe(attrValue); + }); + }); + + describe('setAttr', () => { + test('sets attribute value', () => { + const $el = document.createElement('div'); + + setAttr($el, attrName, attrValue); + expect($el.getAttribute(attrName)).toBe(attrValue); + }); + + test('removes attribute when param value is null', () => { + const $el = document.createElement('div'); + $el.setAttribute(attrName, attrValue); + + setAttr($el, attrName, null); + expect($el.hasAttribute(attrName)).toBe(false); + }); + + test('removes attribute when param value is undefined', () => { + const $el = document.createElement('div'); + $el.setAttribute(attrName, attrValue); + + setAttr($el, attrName, undefined); + expect($el.hasAttribute(attrName)).toBe(false); + }); + + test('sets a boolean attribute when param value is true', () => { + const $el = document.createElement('div'); + + setAttr($el, attrName, true); + expect($el.getAttribute(attrName)).toBe(''); + }); + + test('shouldn`t throw any type errors for non-valid element', () => { + try { + setAttr(null as any as Element, attrName, true); + } catch (e) { + expect(e.message).not.toBe('Cannot read properties of null (reading \'setAttribute\')'); + } + }); + + test('sets attribute for array of elements', () => { + const $el1 = document.createElement('div'); + const $el2 = document.createElement('div'); + const $el3 = document.createElement('div'); + + setAttr([$el1, $el2, $el3], attrName, attrValue); + expect($el1.getAttribute(attrName)).toBe(attrValue); + expect($el2.getAttribute(attrName)).toBe(attrValue); + expect($el3.getAttribute(attrName)).toBe(attrValue); + }); + }); +}); From 05e92cbab2489bdf168c32c1b42463607a03659c Mon Sep 17 00:00:00 2001 From: Feoktist Shovchko Date: Thu, 20 Jul 2023 09:56:13 +0300 Subject: [PATCH 2/2] test(esl-utils): tests refactoring --- src/modules/esl-utils/dom/test/attr.test.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/modules/esl-utils/dom/test/attr.test.ts b/src/modules/esl-utils/dom/test/attr.test.ts index 440bbf3ab..0212148f3 100644 --- a/src/modules/esl-utils/dom/test/attr.test.ts +++ b/src/modules/esl-utils/dom/test/attr.test.ts @@ -8,7 +8,9 @@ describe('Attribute', () => { describe('hasAttr', () => { test('false when attribute is not present', () => expect(hasAttr(document.createElement('div'), attrName)).toBe(false)); - test('false for non-valid element', () => expect(hasAttr(null as any as Element, attrName)).toBe(false)); + test('false for non-valid element', () => expect(hasAttr(null as any, attrName)).toBe(false)); + + test('false for non-valid host element', () => expect(hasAttr({$host: null} as any, attrName)).toBe(false)); test('true when attribute is present', () => { const $el = document.createElement('div'); @@ -45,7 +47,9 @@ describe('Attribute', () => { test('null when attribute is not present', () => expect(getAttr(document.createElement('div'), attrName)).toBe(null)); - test('null for non-element even with fallback value', () => expect(getAttr(null as any as Element, attrName, attrFallback)).toBe(null)); + test('null for non-valid host', () => expect(getAttr({$host: null} as any, attrName)).toBe(null)); + + test('null for non-valid element with fallback value', () => expect(getAttr(null as any, attrName, attrFallback)).toBe(null)); test('fallback value when attribute is not present', () => expect(getAttr(document.createElement('div'), attrName, attrFallback)).toBe(attrFallback)); @@ -99,13 +103,9 @@ describe('Attribute', () => { expect($el.getAttribute(attrName)).toBe(''); }); - test('shouldn`t throw any type errors for non-valid element', () => { - try { - setAttr(null as any as Element, attrName, true); - } catch (e) { - expect(e.message).not.toBe('Cannot read properties of null (reading \'setAttribute\')'); - } - }); + test('shouldn`t throw any type errors for non-valid host', () => expect(() => setAttr({$host: null} as any, attrName, true)).not.toThrowError()); + + test('shouldn`t throw any type errors for non-valid element', () => expect(() => setAttr(null as any, attrName, true)).not.toThrowError()); test('sets attribute for array of elements', () => { const $el1 = document.createElement('div');