diff --git a/packages/calendar/test-helpers.js b/packages/calendar/test-helpers.js new file mode 100644 index 000000000..6c35209c3 --- /dev/null +++ b/packages/calendar/test-helpers.js @@ -0,0 +1,3 @@ +export { CalendarObject } from './test-helpers/CalendarObject.js'; +export { DayObject } from './test-helpers/DayObject.js'; +export { weekdayNames } from './test-helpers/weekdayNames.js'; diff --git a/packages/calendar/test/test-utils.js b/packages/calendar/test-helpers/CalendarObject.js similarity index 63% rename from packages/calendar/test/test-utils.js rename to packages/calendar/test-helpers/CalendarObject.js index 3da3d9699..36882e209 100644 --- a/packages/calendar/test/test-utils.js +++ b/packages/calendar/test-helpers/CalendarObject.js @@ -1,94 +1,4 @@ -export const weekdayNames = { - 'en-GB': { - Sunday: { - long: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], - short: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], - }, - }, -}; - -/** - * Abstraction around calendar day DOM structure, - * allows for writing readable, 'DOM structure agnostic' tests - */ -export class DayObject { - constructor(dayEl) { - this.el = dayEl; - } - - /** - * Node references - */ - - get calendarShadowRoot() { - return this.el.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode; - } - - get cellEl() { - return this.el.parentElement; - } - - get buttonEl() { - return this.el; - } - - /** - * States - */ - - get isDisabled() { - return this.buttonEl.hasAttribute('disabled'); - } - - get isSelected() { - return this.buttonEl.hasAttribute('selected'); - } - - get isToday() { - return this.buttonEl.hasAttribute('today'); - } - - get isCentral() { - return this.buttonEl.getAttribute('tabindex') === '0'; - } - - get isFocused() { - this.calendarShadowRoot.activeElement; - this.buttonEl; - return this.calendarShadowRoot.activeElement === this.buttonEl; - } - - get monthday() { - return Number(this.buttonEl.textContent); - } - - /** - * Text - */ - - get weekdayNameShort() { - const weekdayEls = Array.from( - this.el.parentElement.parentElement.querySelectorAll('.calendar__day-cell'), - ); - const dayIndex = weekdayEls.indexOf(this.el.parentElement); - return weekdayNames['en-GB'].Sunday.short[dayIndex]; - } - - get weekdayNameLong() { - const weekdayEls = Array.from( - this.el.parentElement.parentElement.querySelectorAll('.calendar__day-cell'), - ); - const dayIndex = weekdayEls.indexOf(this.el.parentElement); - return weekdayNames['en-GB'].Sunday.long[dayIndex]; - } - - /** - * Other - */ - get cellIndex() { - return Array.from(this.cellEl.parentElement.children).indexOf(this.cellEl); - } -} +import { DayObject } from './DayObject.js'; /** * Abstraction around calendar DOM structure, diff --git a/packages/calendar/test-helpers/DayObject.js b/packages/calendar/test-helpers/DayObject.js new file mode 100644 index 000000000..f42dad165 --- /dev/null +++ b/packages/calendar/test-helpers/DayObject.js @@ -0,0 +1,82 @@ +import { weekdayNames } from './weekdayNames.js'; + +/** + * Abstraction around calendar day DOM structure, + * allows for writing readable, 'DOM structure agnostic' tests + */ +export class DayObject { + constructor(dayEl) { + this.el = dayEl; + } + + /** + * Node references + */ + + get calendarShadowRoot() { + return this.el.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode; + } + + get cellEl() { + return this.el.parentElement; + } + + get buttonEl() { + return this.el; + } + + /** + * States + */ + + get isDisabled() { + return this.buttonEl.hasAttribute('disabled'); + } + + get isSelected() { + return this.buttonEl.hasAttribute('selected'); + } + + get isToday() { + return this.buttonEl.hasAttribute('today'); + } + + get isCentral() { + return this.buttonEl.getAttribute('tabindex') === '0'; + } + + get isFocused() { + return this.calendarShadowRoot.activeElement === this.buttonEl; + } + + get monthday() { + return Number(this.buttonEl.textContent); + } + + /** + * Text + */ + + get weekdayNameShort() { + const weekdayEls = Array.from( + this.el.parentElement.parentElement.querySelectorAll('.calendar__day-cell'), + ); + const dayIndex = weekdayEls.indexOf(this.el.parentElement); + return weekdayNames['en-GB'].Sunday.short[dayIndex]; + } + + get weekdayNameLong() { + const weekdayEls = Array.from( + this.el.parentElement.parentElement.querySelectorAll('.calendar__day-cell'), + ); + const dayIndex = weekdayEls.indexOf(this.el.parentElement); + return weekdayNames['en-GB'].Sunday.long[dayIndex]; + } + + /** + * Other + */ + get cellIndex() { + return Array.from(this.cellEl.parentElement.children).indexOf(this.cellEl); + } +} diff --git a/packages/calendar/test-helpers/weekdayNames.js b/packages/calendar/test-helpers/weekdayNames.js new file mode 100644 index 000000000..15c4e8a72 --- /dev/null +++ b/packages/calendar/test-helpers/weekdayNames.js @@ -0,0 +1,8 @@ +export const weekdayNames = { + 'en-GB': { + Sunday: { + long: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], + short: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], + }, + }, +}; diff --git a/packages/calendar/test/lion-calendar.test.js b/packages/calendar/test/lion-calendar.test.js index fee43708b..c2d720bd5 100644 --- a/packages/calendar/test/lion-calendar.test.js +++ b/packages/calendar/test/lion-calendar.test.js @@ -5,7 +5,7 @@ import { html } from '@lion/core'; import { localize } from '@lion/localize'; import { localizeTearDown } from '@lion/localize/test-helpers.js'; -import { CalendarObject, DayObject } from './test-utils.js'; +import { CalendarObject, DayObject } from '../test-helpers.js'; import './keyboardEventShimIE.js'; import { isSameDate } from '../src/utils/isSameDate.js'; diff --git a/packages/calendar/test/utils/dataTemplate.test.js b/packages/calendar/test/utils/dataTemplate.test.js index 7b7113eb8..41c53e349 100644 --- a/packages/calendar/test/utils/dataTemplate.test.js +++ b/packages/calendar/test/utils/dataTemplate.test.js @@ -3,7 +3,7 @@ import { expect, fixture } from '@open-wc/testing'; import { createMultipleMonth } from '../../src/utils/createMultipleMonth.js'; import { dataTemplate } from '../../src/utils/dataTemplate.js'; -import { weekdayNames } from '../test-utils.js'; +import { weekdayNames } from '../../test-helpers.js'; // eslint-disable-next-line camelcase import snapshot_enGB_Sunday_201812 from './snapshots/monthTemplate_en-GB_Sunday_2018-12.js'; diff --git a/packages/input-datepicker/test-helpers.js b/packages/input-datepicker/test-helpers.js new file mode 100644 index 000000000..21d3f7ca2 --- /dev/null +++ b/packages/input-datepicker/test-helpers.js @@ -0,0 +1 @@ +export { DatepickerInputObject } from './test-helpers/DatepickerInputObject.js'; diff --git a/packages/input-datepicker/test/test-utils.js b/packages/input-datepicker/test-helpers/DatepickerInputObject.js similarity index 95% rename from packages/input-datepicker/test/test-utils.js rename to packages/input-datepicker/test-helpers/DatepickerInputObject.js index bb6b5a192..93f8a9d1a 100644 --- a/packages/input-datepicker/test/test-utils.js +++ b/packages/input-datepicker/test-helpers/DatepickerInputObject.js @@ -1,4 +1,4 @@ -import { CalendarObject } from '@lion/calendar/test/test-utils.js'; +import { CalendarObject } from '@lion/calendar/test-helpers.js'; // TODO: refactor CalendarObject to this approach (only methods when arguments are needed) export class DatepickerInputObject { diff --git a/packages/input-datepicker/test/lion-input-datepicker.test.js b/packages/input-datepicker/test/lion-input-datepicker.test.js index 5297e96a9..646a0eb2b 100644 --- a/packages/input-datepicker/test/lion-input-datepicker.test.js +++ b/packages/input-datepicker/test/lion-input-datepicker.test.js @@ -12,7 +12,7 @@ import { keyCodes } from '@lion/overlays/src/utils/key-codes.js'; import { keyUpOn } from '@polymer/iron-test-helpers/mock-interactions.js'; import { LionCalendar } from '@lion/calendar'; import { isSameDate } from '@lion/calendar/src/utils/isSameDate.js'; -import { DatepickerInputObject } from './test-utils.js'; +import { DatepickerInputObject } from '../test-helpers.js'; import { LionInputDatepicker } from '../src/LionInputDatepicker.js'; import '../lion-input-datepicker.js'; diff --git a/packages/input-iban/test/validators.test.js b/packages/input-iban/test/validators.test.js index 8834a0f0e..8a08bcdd2 100644 --- a/packages/input-iban/test/validators.test.js +++ b/packages/input-iban/test/validators.test.js @@ -1,5 +1,5 @@ import { expect } from '@open-wc/testing'; -import { smokeTestValidator } from '@lion/validate/test/test-utils.js'; +import { smokeTestValidator } from '@lion/validate/test-helpers.js'; import { isIBAN, diff --git a/packages/localize/test-helpers.js b/packages/localize/test-helpers.js index ea1a85e10..37b59cbfc 100644 --- a/packages/localize/test-helpers.js +++ b/packages/localize/test-helpers.js @@ -1,9 +1,7 @@ -import { localize } from './src/localize.js'; - -export const localizeTearDown = () => { - // makes sure that between tests the localization is reset to default state - localize._teardownHtmlLangAttributeObserver(); - document.documentElement.lang = 'en-GB'; - localize._setupHtmlLangAttributeObserver(); - localize.reset(); -}; +export { + fakeImport, + resetFakeImport, + setupEmptyFakeImportsFor, + setupFakeImport, +} from './test-helpers/fake-imports.js'; +export { localizeTearDown } from './test-helpers/localizeTearDown.js'; diff --git a/packages/localize/test/test-utils.js b/packages/localize/test-helpers/fake-imports.js similarity index 100% rename from packages/localize/test/test-utils.js rename to packages/localize/test-helpers/fake-imports.js diff --git a/packages/localize/test-helpers/localizeTearDown.js b/packages/localize/test-helpers/localizeTearDown.js new file mode 100644 index 000000000..ad37ec777 --- /dev/null +++ b/packages/localize/test-helpers/localizeTearDown.js @@ -0,0 +1,9 @@ +import { localize } from '../src/localize.js'; + +export const localizeTearDown = () => { + // makes sure that between tests the localization is reset to default state + localize._teardownHtmlLangAttributeObserver(); + document.documentElement.lang = 'en-GB'; + localize._setupHtmlLangAttributeObserver(); + localize.reset(); +}; diff --git a/packages/localize/test/LocalizeManager.test.js b/packages/localize/test/LocalizeManager.test.js index fecc2f8d7..8f494bde2 100644 --- a/packages/localize/test/LocalizeManager.test.js +++ b/packages/localize/test/LocalizeManager.test.js @@ -1,7 +1,7 @@ import { expect, oneEvent, aTimeout } from '@open-wc/testing'; import sinon from 'sinon'; import { fetchMock } from '@bundled-es-modules/fetch-mock'; -import { setupFakeImport, resetFakeImport, fakeImport } from './test-utils.js'; +import { setupFakeImport, resetFakeImport, fakeImport } from '../test-helpers.js'; import { LocalizeManager } from '../src/LocalizeManager.js'; diff --git a/packages/localize/test/LocalizeMixin.test.js b/packages/localize/test/LocalizeMixin.test.js index 059b9ac35..f323a0ff0 100644 --- a/packages/localize/test/LocalizeMixin.test.js +++ b/packages/localize/test/LocalizeMixin.test.js @@ -11,14 +11,14 @@ import sinon from 'sinon'; import { isDirective } from '@lion/core'; import { LionLitElement } from '@lion/core/src/LionLitElement.js'; import { + localizeTearDown, setupEmptyFakeImportsFor, resetFakeImport, fakeImport, setupFakeImport, -} from './test-utils.js'; +} from '../test-helpers.js'; import { localize } from '../src/localize.js'; -import { localizeTearDown } from '../test-helpers.js'; import { LocalizeMixin } from '../src/LocalizeMixin.js'; diff --git a/packages/validate/test-helpers.js b/packages/validate/test-helpers.js new file mode 100644 index 000000000..95a395010 --- /dev/null +++ b/packages/validate/test-helpers.js @@ -0,0 +1 @@ +export { smokeTestValidator } from './test-helpers/smokeTestValidator.js'; diff --git a/packages/validate/test/test-utils.js b/packages/validate/test-helpers/smokeTestValidator.js similarity index 65% rename from packages/validate/test/test-utils.js rename to packages/validate/test-helpers/smokeTestValidator.js index bdf10ac1f..2b59cba17 100644 --- a/packages/validate/test/test-utils.js +++ b/packages/validate/test-helpers/smokeTestValidator.js @@ -1,8 +1,9 @@ +// eslint-disable-next-line import/no-extraneous-dependencies import { expect } from '@open-wc/testing'; export const smokeTestValidator = (name, validator, value, params = undefined) => { const generated = validator(params); - expect(generated[0](value, params)[name]).to.be.true; + expect(generated[0](value, params)[name]).to.equal(true); if (params) { expect(generated[1]).to.equals(params); } diff --git a/packages/validate/test/validators.test.js b/packages/validate/test/validators.test.js index b97b060d5..6e4e902a9 100644 --- a/packages/validate/test/validators.test.js +++ b/packages/validate/test/validators.test.js @@ -1,5 +1,5 @@ import { expect } from '@open-wc/testing'; -import { smokeTestValidator } from './test-utils.js'; +import { smokeTestValidator } from '../test-helpers.js'; import { isString,