Merge pull request #213 from ing-bank/feat/publicTestHelpers

Public test-helpers
This commit is contained in:
Mikhail Bashkirov 2019-07-30 16:10:23 +02:00 committed by GitHub
commit fd6ddb8960
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 123 additions and 110 deletions

View file

@ -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';

View file

@ -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,

View file

@ -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);
}
}

View file

@ -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'],
},
},
};

View file

@ -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';

View file

@ -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';

View file

@ -0,0 +1 @@
export { DatepickerInputObject } from './test-helpers/DatepickerInputObject.js';

View file

@ -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 {

View file

@ -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';

View file

@ -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,

View file

@ -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';

View file

@ -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();
};

View file

@ -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';

View file

@ -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';

View file

@ -0,0 +1 @@
export { smokeTestValidator } from './test-helpers/smokeTestValidator.js';

View file

@ -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);
}

View file

@ -1,5 +1,5 @@
import { expect } from '@open-wc/testing';
import { smokeTestValidator } from './test-utils.js';
import { smokeTestValidator } from '../test-helpers.js';
import {
isString,