@thednp/shorty 2.0.2 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +5 -5
  2. package/dist/shorty.cjs +1 -1
  3. package/dist/shorty.cjs.map +1 -1
  4. package/dist/shorty.d.ts +12 -11
  5. package/dist/shorty.js +1 -1
  6. package/dist/shorty.js.map +1 -1
  7. package/dist/shorty.mjs +14 -4
  8. package/dist/shorty.mjs.map +1 -1
  9. package/package.json +17 -15
  10. package/src/boolean/isApple.ts +1 -1
  11. package/src/boolean/isFirefox.ts +3 -1
  12. package/src/boolean/isMobile.ts +1 -1
  13. package/src/event/one.ts +1 -1
  14. package/src/get/getRectRelativeToOffsetParent.ts +1 -1
  15. package/src/index.ts +11 -0
  16. package/src/misc/createCustomEvent.ts +5 -5
  17. package/src/misc/createElementNS.ts +1 -1
  18. package/src/misc/emulateAnimationEnd.ts +3 -2
  19. package/src/misc/emulateTransitionEnd.ts +2 -2
  20. package/src/selectors/closest.ts +5 -2
  21. package/test/att.test.ts +43 -0
  22. package/test/boolean.test.ts +30 -0
  23. package/test/class.test.ts +26 -0
  24. package/test/event.test.ts +39 -0
  25. package/{cypress/test.html → test/fixtures/getExampleDom.ts} +17 -32
  26. package/test/fixtures/style.css +18 -0
  27. package/test/get.test.ts +150 -0
  28. package/{cypress/e2e/is.cy.ts → test/is.test.ts} +77 -74
  29. package/test/misc.test.ts +352 -0
  30. package/test/selectors.test.ts +90 -0
  31. package/tsconfig.json +6 -1
  32. package/vitest.config-ui.ts +21 -0
  33. package/vitest.config.ts +20 -0
  34. package/cypress/e2e/att.cy.ts +0 -46
  35. package/cypress/e2e/boolean.cy.ts +0 -44
  36. package/cypress/e2e/class.cy.ts +0 -28
  37. package/cypress/e2e/event.cy.ts +0 -51
  38. package/cypress/e2e/get.cy.ts +0 -168
  39. package/cypress/e2e/misc.cy.ts +0 -354
  40. package/cypress/e2e/selectors.cy.ts +0 -85
  41. package/cypress/plugins/esbuild-istanbul.ts +0 -50
  42. package/cypress/plugins/tsCompile.ts +0 -34
  43. package/cypress/support/commands.ts +0 -37
  44. package/cypress/support/e2e.ts +0 -21
  45. package/cypress/support/index.js +0 -22
  46. package/cypress.config.ts +0 -30
  47. /package/{cypress → test}/fixtures/custom-elem.js +0 -0
@@ -1,168 +0,0 @@
1
- /// <reference types="cypress" />
2
- // @ts-nocheck
3
-
4
- // import SHORTY from '../../src/index';
5
- import * as SHORTY from '../../src/index';
6
-
7
- import CustomElem from '../fixtures/custom-elem';
8
-
9
- describe('Shorty Library Tests', () => {
10
- before(() => {
11
- cy.visit('cypress/test.html');
12
- });
13
-
14
- it('Test get folder', () => {
15
- const {
16
- getBoundingClientRect,
17
- getDocument,
18
- getDocumentBody,
19
- getDocumentElement,
20
- getDocumentHead,
21
- getElementAnimationDelay,
22
- getElementAnimationDuration,
23
- getElementTransitionDelay,
24
- getElementTransitionDuration,
25
- getElementStyle,
26
- getNodeScroll,
27
- getParentNode,
28
- getRectRelativeToOffsetParent,
29
- getUID,
30
- getWindow,
31
- ObjectValues,
32
- } = SHORTY;
33
-
34
- cy
35
- .get('.alert')
36
- .should($element => {
37
- const element = $element[0];
38
- element.style.transform = 'scale(1.01)';
39
- const win = getWindow(element);
40
- const CE = new CustomElem();
41
- CE.className = 'btn btn-outline-primary';
42
- win.document.body.append(CE);
43
-
44
- // we round values so all browsers return same values
45
- let { x, y, top, left, right, bottom, width, height } = getBoundingClientRect(
46
- element,
47
- true,
48
- );
49
- expect(
50
- ObjectValues([x, y, top, left, right, bottom, width, height]).map(Math.round),
51
- 'getBoundingClientRect',
52
- ).to.deep.equal([63, 87, 87, 63, 927, 204, 864, 117]);
53
- element.style.transform = '';
54
-
55
- expect(getWindow(), 'getWindow').to.be.instanceOf(Window); // root WINDOW
56
- expect(getWindow(element.ownerDocument), 'getWindow(document)').to.be.instanceOf(
57
- win.Window,
58
- );
59
- expect(getWindow(CE), 'getWindow(CustomElement)').to.be.instanceOf(win.Window);
60
- expect(getWindow(CE.shadowRoot), 'getWindow(CustomElement.shadowRoot)').to.be.instanceOf(
61
- win.Window,
62
- );
63
- expect(getWindow(win.top), 'getWindow(window)').to.be.instanceOf(win.top.Window);
64
-
65
- expect(getDocument(), 'getDocument()').to.be.instanceOf(Document);
66
- expect(getDocument(element), 'getDocument(node)').to.be.instanceOf(win.Document);
67
- expect(getDocument(win.document), 'getDocument(document)').to.be.instanceOf(win.Document);
68
- expect(getDocument(win), 'getDocument(window)').to.be.instanceOf(win.Document);
69
-
70
- expect(getDocumentBody(element), 'getDocumentBody').to.be.instanceOf(win.HTMLBodyElement);
71
- expect(getDocumentElement(element), 'getDocumentElement').to.be.instanceOf(
72
- win.HTMLHtmlElement,
73
- );
74
- expect(getDocumentHead(element), 'getDocumentHead').to.be.instanceOf(win.HTMLHeadElement);
75
-
76
- expect(getElementAnimationDelay(element), 'getElementAnimationDelay').to.equal(0);
77
-
78
- expect(getElementAnimationDuration(element), 'getElementAnimationDuration').to.equal(0);
79
-
80
- CE.style.animation = 'animate-me 1s ease 0.5s';
81
- expect(getElementAnimationDelay(CE), 'getElementAnimationDelay - seconds').to.equal(500);
82
-
83
- expect(getElementAnimationDuration(CE), 'getElementAnimationDuration - seconds').to.equal(
84
- 1000,
85
- );
86
-
87
- CE.style.animation = 'animate-me 1200ms ease 400ms';
88
- expect(getElementAnimationDelay(CE), 'getElementAnimationDelay - miliseconds').to.equal(
89
- 400,
90
- );
91
-
92
- expect(
93
- getElementAnimationDuration(CE),
94
- 'getElementAnimationDuration - miliseconds',
95
- ).to.equal(1200);
96
-
97
- element.style.transition = 'opacity .145s linear .1s';
98
- expect(getElementTransitionDelay(element), 'getElementTransitionDelay - seconds').to.equal(
99
- 100,
100
- );
101
- expect(
102
- getElementTransitionDuration(element),
103
- 'getElementTransitionDuration - seconds',
104
- ).to.equal(145);
105
-
106
- element.style.transition = 'opacity 140ms linear 10ms';
107
- expect(
108
- getElementTransitionDelay(element),
109
- 'getElementTransitionDelay- miliseconds',
110
- ).to.equal(10);
111
-
112
- expect(
113
- getElementTransitionDuration(element),
114
- 'getElementTransitionDuration- miliseconds',
115
- ).to.equal(140);
116
-
117
- element.style.transition = '';
118
-
119
- expect(getElementStyle(element, 'color'), 'getElementStyle(color)').to.equal(
120
- 'rgb(102, 77, 3)',
121
- );
122
-
123
- expect(getNodeScroll(win), 'getNodeScroll(window)').to.deep.equal({ x: 0, y: 0 });
124
- expect(getNodeScroll(element), 'getNodeScroll(element)').to.deep.equal({ x: 0, y: 0 });
125
- expect(
126
- getNodeScroll(element.offsetParent as HTMLElement),
127
- 'getNodeScroll(element.offsetParent)',
128
- ).to.deep.equal({ x: 0, y: 0 });
129
- expect(getNodeScroll(getDocumentBody(element)), 'getNodeScroll(body)').to.deep.equal({
130
- x: 0,
131
- y: 0,
132
- });
133
-
134
- expect(getParentNode(getDocumentElement()), 'getParentNode()').to.be.instanceOf(
135
- HTMLHtmlElement,
136
- ); // root HTML
137
- expect(getParentNode(win), 'getParentNode(window)').to.be.instanceOf(win.HTMLHtmlElement);
138
- expect(getParentNode(getDocumentBody(element)), 'getParentNode(body)').to.be.instanceOf(
139
- win.HTMLHtmlElement,
140
- );
141
- expect(getParentNode(element), 'getParentNode(node)').to.have.class('container');
142
- expect(getParentNode(CE), 'getParentNode(CustomElement)').to.be.instanceOf(
143
- win.HTMLBodyElement,
144
- );
145
- expect(
146
- getParentNode(CE.shadowRoot),
147
- 'getParentNode(CustomElement.shadowRoot)',
148
- ).to.be.instanceOf(CustomElem);
149
-
150
- ({ x, y, width, height } = getRectRelativeToOffsetParent(
151
- element,
152
- getDocumentElement(win),
153
- getNodeScroll(getDocumentElement(win)),
154
- ));
155
-
156
- expect(
157
- [x, y, width, height].map(Math.round),
158
- 'getRectRelativeToOffsetParent',
159
- ).to.deep.equal([68, 88, 864, 117]);
160
-
161
- expect(getUID(element), 'getUID()').to.eq(0);
162
- expect(getUID(element, 'Alert'), 'getUID(key) - set & returns').to.eq(0);
163
- expect(getUID(element, 'Alert'), 'getUID(key) - returns').to.eq(0);
164
- expect(getUID(win, 'Alert'), 'getUID(key) - set & returns').to.eq(1);
165
- expect(getUID(win, 'Alert'), 'getUID(key) - returns').to.eq(1);
166
- });
167
- });
168
- });
@@ -1,354 +0,0 @@
1
- /// <reference types="cypress" />
2
- // @ts-nocheck
3
-
4
- // import SHORTY from '../../src/index';
5
- import * as SHORTY from '../../src/index';
6
-
7
- describe('Shorty Library Tests', () => {
8
- beforeEach(() => {
9
- cy.visit('cypress/test.html');
10
- });
11
-
12
- it('Test misc folder - emulateTransitionEnd - no transition', () => {
13
- const {
14
- dispatchEvent,
15
- emulateTransitionEnd,
16
- createCustomEvent,
17
- reflow,
18
- querySelector,
19
- setElementStyle,
20
- getElementStyle,
21
- removeClass,
22
- addClass,
23
- one,
24
- } = SHORTY;
25
-
26
- cy
27
- .window()
28
- .then(win => {
29
- const el = querySelector('.alert', win.document) as HTMLElement;
30
- const btn = querySelector('.btn-close', el);
31
- const alertHideEvent = createCustomEvent('hide-alert', { relatedTarget: null });
32
-
33
- setElementStyle(el, {
34
- transition: 'none',
35
- transitionProperty: 'none',
36
- transitionDuration: '0s',
37
- 'transition-delay': '0s',
38
- '--transition-prop': 'none',
39
- });
40
-
41
- expect(getElementStyle(el, 'transitionProperty')).to.equal('none');
42
- expect(getElementStyle(el, 'transition-duration')).to.equal('0s');
43
- expect(getElementStyle(el, 'transitionDelay')).to.equal('0s');
44
- expect(getElementStyle(el, '--transition-prop')).to.equal('none');
45
-
46
- cy.log('emulateTransitionEnd').then(() => {
47
- one(el, 'hide-alert', function hideHandler(e) {
48
- cy.log('hide-alert triggered');
49
- expect(e.target).to.equal(el);
50
- expect(e.relatedTarget).to.equal(btn);
51
- });
52
-
53
- one(btn, 'click', function handleBtn(e) {
54
- cy.log('clicked btn');
55
- expect(e.target).to.equal(btn);
56
- removeClass(el, 'show');
57
- reflow(el);
58
- alertHideEvent.relatedTarget = btn;
59
- dispatchEvent(el, alertHideEvent);
60
- emulateTransitionEnd(el, function () {
61
- addClass(el, 'show');
62
- cy.log('transitionend triggered');
63
- });
64
- });
65
-
66
- btn.click();
67
- cy.wait(100);
68
- });
69
- });
70
- });
71
-
72
- it('Test misc folder - emulateTransitionEnd - default', () => {
73
- const {
74
- dispatchEvent,
75
- emulateTransitionEnd,
76
- createCustomEvent,
77
- reflow,
78
- querySelector,
79
- removeClass,
80
- addClass,
81
- one,
82
- focus,
83
- } = SHORTY;
84
-
85
- cy
86
- // .wait('@pageload')
87
- .window()
88
- .then(win => {
89
- const el = querySelector('.alert', win.document);
90
- const btn = querySelector('.btn-close', el);
91
- const alertHideEvent = createCustomEvent('hide-alert', { relatedTarget: null });
92
-
93
- cy.log('begin **emulateTransitionEnd**').then(() => {
94
- one(el, 'hide-alert', function hideHandler(e) {
95
- cy.log('hide-alert triggered');
96
- expect(e.target).to.equal(el);
97
- expect(e.relatedTarget).to.equal(btn);
98
- });
99
-
100
- one(btn, 'click', function handleBtn(e) {
101
- cy.log('clicked btn');
102
- expect(e.target).to.equal(btn);
103
- removeClass(el, 'show');
104
- reflow(el);
105
- alertHideEvent.relatedTarget = btn;
106
- dispatchEvent(el, alertHideEvent);
107
- emulateTransitionEnd(el, function () {
108
- addClass(el, 'show');
109
- focus(btn);
110
- console.log('transitionend triggered');
111
- });
112
- });
113
-
114
- btn.click();
115
- cy.wait(300);
116
- });
117
- });
118
- });
119
-
120
- it('Test misc folder - emulateAnimationEnd - default', () => {
121
- const { emulateAnimationEnd, getElementStyle, querySelector, addClass } = SHORTY;
122
- cy
123
- // .wait('@pageload')
124
- .window()
125
- .then(win => {
126
- const el = querySelector('.alert', win.document);
127
-
128
- cy.log('begin **emulateAnimationEnd**').then(() => {
129
- addClass(el, 'animate-test');
130
- emulateAnimationEnd(el, () => {
131
- console.log('animationend fired');
132
- expect(getElementStyle(el, 'animationName'), 'animationName').to.equal('animate-test');
133
- expect(getElementStyle(el, 'animationDuration'), 'animationDuration').to.equal('0.3s');
134
- expect(getElementStyle(el, 'animationDelay'), 'animationDelay').to.equal('0s');
135
- });
136
-
137
- cy.wait(350);
138
- });
139
- });
140
- });
141
-
142
- it('Test misc folder - emulateAnimationEnd - no duration', () => {
143
- const { emulateAnimationEnd, setElementStyle, getElementStyle, querySelector, addClass } =
144
- SHORTY;
145
- cy
146
- // .wait('@pageload')
147
- .window()
148
- .then(win => {
149
- const el = querySelector('.alert', win.document);
150
- setElementStyle(el, { animationDuration: '0s' });
151
-
152
- cy.log('begin **emulateAnimationEnd**').then(() => {
153
- addClass(el, 'animate-test');
154
- emulateAnimationEnd(el, () => {
155
- console.log('animationend fired');
156
- expect(getElementStyle(el, 'animationName'), 'animationName').to.equal('animate-test');
157
- expect(getElementStyle(el, 'animationDuration'), 'animationDuration').to.equal('0s');
158
- expect(getElementStyle(el, 'animationDelay'), 'animationDelay').to.equal('0s');
159
- });
160
-
161
- cy.wait(100);
162
- });
163
- });
164
- });
165
-
166
- it('Test misc folder - everything else', () => {
167
- const {
168
- ArrayFrom,
169
- Float32ArrayFrom,
170
- Float64ArrayFrom,
171
- distinct,
172
- noop,
173
- ObjectHasOwn,
174
- ObjectEntries,
175
- ObjectAssign,
176
- ObjectKeys,
177
- ObjectValues,
178
- ObjectDefineProperty,
179
- createElement,
180
- createElementNS,
181
- Data,
182
- getInstance,
183
- normalizeOptions,
184
- Timer,
185
- toLowerCase,
186
- toUpperCase,
187
- querySelector,
188
- getElementsByClassName,
189
- } = SHORTY;
190
- cy
191
- // .wait('@pageload')
192
- .window()
193
- .then(win => {
194
- const el = querySelector('.alert', win.document);
195
- const table = querySelector('.table', win.document);
196
-
197
- const defaults = { op1: true, op2: true, op3: 5, title: null };
198
- const jsOps = { op1: false, op2: false, op3: 8, title: 'something' };
199
- expect(ObjectHasOwn(jsOps,'op3')).to.be.true;
200
- expect(ObjectHasOwn(jsOps,'momo')).to.be.false;
201
- expect(ObjectHasOwn(null,'momo')).to.be.false;
202
- expect(normalizeOptions(el, defaults, {}, 'bs'), 'normalizeOptions(data)').to.deep.equal({
203
- op1: false,
204
- op2: true,
205
- op3: 10,
206
- title: null,
207
- });
208
- expect(normalizeOptions(el, defaults, jsOps, 'bs'), 'normalizeOptions(js)').to.deep.equal({
209
- op1: false,
210
- op2: false,
211
- op3: 8,
212
- title: 'something',
213
- });
214
- expect(noop()).to.be.undefined;
215
-
216
- Timer.set('el', noop, 150);
217
- Timer.set(el, () => console.log(el.tagName + ' has timer of 150'), 150, 'alert');
218
- expect(Timer.get('el', 'alert')).to.be.null;
219
- expect(Timer.get(el, 'alert')).to.not.be.undefined;
220
- Timer.clear('el', 'alert');
221
- Timer.clear(el, 'alert');
222
- expect(Timer.get(el, 'alert')).to.be.null;
223
-
224
- Timer.set(el, () => console.log(el.tagName + ' has timer of 250'), 250);
225
- expect(Timer.get(el)).to.not.be.null;
226
- Timer.clear(el);
227
- expect(Timer.get(el)).to.be.null;
228
-
229
- expect([0, 1, 1, 2, 3, 3].filter(distinct), 'filter(DISTINCT)').to.deep.equal([0, 1, 2, 3]);
230
-
231
- expect(toLowerCase('textSample'), 'toLowerCase(string)').to.equal('textsample');
232
- expect(toUpperCase('textSample'), 'toUpperCase(string)').to.equal('TEXTSAMPLE');
233
-
234
- // expect(
235
- // Object.defineProperty({ c: 3}, 'a', { value: {b: 1}, writable: true }),
236
- // 'ObjectDefineProperty(object1, prop, value)',
237
- // ).to.deep.equal({ c: 3, a: {b: 1} });
238
- expect(
239
- ObjectAssign({ c: 3 }, { a: 1, b: 2 }),
240
- 'ObjectAssign(object1, object2)',
241
- ).to.deep.equal({ a: 1, b: 2, c: 3 });
242
- expect(ObjectEntries({ a: 1, b: 2 }), 'ObjectEntries(object)').to.deep.equal([
243
- ['a', 1],
244
- ['b', 2],
245
- ]);
246
- expect(ObjectKeys({ a: 1, b: 2 }), 'ObjectKeys(object)').to.deep.equal(['a', 'b']);
247
- expect(ObjectValues({ a: 1, b: 2 }), 'ObjectValues(object)').to.deep.equal([1, 2]);
248
-
249
- expect(Float32ArrayFrom([0, 1, 2, 3]), 'Float32ArrayFrom(array)').to.be.instanceOf(
250
- Float32Array,
251
- );
252
- expect(Float64ArrayFrom([0, 1, 2, 3]), 'Float64ArrayFrom(array)').to.be.instanceOf(
253
- Float64Array,
254
- );
255
- expect(Float64ArrayFrom([0, 1, 2, 3]).length, 'Float64ArrayFrom(array)').to.eq(4);
256
-
257
- expect(
258
- ArrayFrom(new Float32Array([0, 1, 2, 3])),
259
- 'ArrayFrom(Float32Array)',
260
- ).to.be.instanceOf(Array);
261
- expect(ArrayFrom(new Float64Array([0, 1, 2, 3])), 'Array(Float64Array)').to.be.instanceOf(
262
- Array,
263
- );
264
- expect(
265
- ArrayFrom(getElementsByClassName('table', win.document)),
266
- 'ArrayFrom(HTMLCollection)',
267
- ).to.deep.equal([table]);
268
-
269
- expect(createElement(), 'createElement()').to.be.undefined;
270
- expect(createElement('input'), 'createElement(string)').to.be.instanceOf(HTMLInputElement);
271
- expect(
272
- createElement({
273
- tagName: 'p',
274
- className: 'lead',
275
- innerText: 'This is a newly created paragraph.',
276
- }),
277
- 'createElement(object)',
278
- )
279
- .to.be.instanceOf(HTMLParagraphElement)
280
- .and.have.class('lead')
281
- .and.contain('This is a newly created paragraph.');
282
- expect(
283
- createElement({
284
- tagName: 'p',
285
- textContent: 'This is a newly created paragraph.',
286
- }),
287
- 'createElement(object)',
288
- ).to.be.instanceOf(HTMLParagraphElement)
289
- .and.contain('This is a newly created paragraph.');
290
-
291
- expect(
292
- createElement({
293
- className: 'lead',
294
- innerText: 'This is a newly created paragraph.',
295
- }),
296
- 'createElement(incompleteObject)',
297
- ).to.be.undefined;
298
-
299
- expect(createElementNS(), 'createElementNS()').to.be.undefined;
300
- expect(
301
- createElementNS('http://www.w3.org/2000/svg', 'svg'),
302
- 'createElementNS(ns, string)',
303
- ).to.be.instanceOf(SVGElement);
304
- expect(
305
- createElementNS('http://www.w3.org/1999/xhtml', {
306
- tagName: 'button',
307
- className: 'btn',
308
- innerText: 'New Item',
309
- textContent: 'New Item',
310
- }),
311
- 'createElementNS(ns, object)',
312
- )
313
- .to.be.instanceOf(HTMLButtonElement)
314
- .and.have.class('btn')
315
- .and.contain('New Item');
316
-
317
- expect(
318
- createElementNS('http://www.w3.org/2000/svg', {
319
- tagName: 'path',
320
- d: 'M98,158l157,156L411,158l27,27L255,368L71,185L98,158z',
321
- }),
322
- 'createElementNS(ns, object)',
323
- ).to.be.instanceOf(SVGPathElement)
324
- .and.have.property('d').equal('M98,158l157,156L411,158l27,27L255,368L71,185L98,158z');
325
-
326
- expect(
327
- createElementNS('http://www.w3.org/2000/svg', {
328
- tagName: '',
329
- className: 'icon',
330
- d: 'M98,158l157,156L411,158l27,27L255,368L71,185L98,158z',
331
- }),
332
- 'createElementNS(ns, incompleteObject)',
333
- ).to.be.undefined;
334
-
335
- Data.set(el);
336
- expect(Data.get(el), 'not enough params - Data.get(node)').to.be.null;
337
-
338
- Data.set('str', 'strKey', 'data');
339
- expect(Data.get('str', 'strKey'), 'not HTMLElement - Data.get(string, string, string)').to
340
- .be.null;
341
-
342
- Data.set(el, 'Alert', { element: el });
343
- expect(Data.get(el, 'Alert'), 'Data.get(node, key)').to.deep.equal({ element: el });
344
- expect(getInstance(el, 'Alert'), 'getInstance(node, key)').to.deep.equal({ element: el });
345
- expect(Data.getAllFor('Alert').size, 'Data.getAllFor(string).size').to.equal(1);
346
-
347
- Data.remove(el, 'Alert');
348
- expect(Data.get(el, 'Alert'), 'removed - Data.get(node, string)').to.be.null;
349
- expect(getInstance(el, 'Alert'), 'removed - getInstance(node, string)').to.be.null;
350
- expect(Data.getAllFor('Alert'), 'removed - Data.getAllFor(string)').to.be.null;
351
- Data.remove(el, 'Alert');
352
- });
353
- });
354
- });
@@ -1,85 +0,0 @@
1
- /// <reference types="cypress" />
2
- // @ts-nocheck
3
-
4
- // import SHORTY from '../../src/index';
5
- import * as SHORTY from '../../src/index';
6
-
7
- import CustomElem from '../fixtures/custom-elem';
8
-
9
- describe('Shorty Library Tests', () => {
10
- before(() => {
11
- cy.visit('cypress/test.html')
12
- .wait(100);
13
- });
14
-
15
- it('Test selectors folder', () => {
16
- const {
17
- closest,
18
- getCustomElements,
19
- getElementById,
20
- getElementsByClassName,
21
- getElementsByTagName,
22
- matches,
23
- querySelector,
24
- querySelectorAll,
25
- } = SHORTY;
26
-
27
- cy
28
- .get('.alert')
29
- .then($alert => {
30
- const [el] = $alert;
31
- const win = el.ownerDocument.defaultView;
32
-
33
- const CE = new CustomElem();
34
- win.document.body.append(CE);
35
-
36
- expect(querySelectorAll('div'), 'querySelectorAll(div)').to.have.length(0);
37
- expect(querySelectorAll('div', win.document), 'querySelectorAll(div, parent)').to.have.length(1);
38
-
39
- expect(querySelector(), 'querySelector()').to.be.null;
40
- expect(querySelector(el), 'querySelector(node)').to.equal(el);
41
- expect(querySelector('.alert'), 'querySelector(selector)').to.be.null;
42
- expect(querySelector('.alert', win.document), 'querySelector(selector, parent)').to.eq(el);
43
-
44
- expect(closest(el), 'closest()').to.be.null;
45
- expect(closest(el, 'wombat'), 'closest(invalid)').to.be.null;
46
- expect(closest(el, 'body'), 'closest(body)').to.eq(win.document.body);
47
-
48
- expect(getCustomElements(), 'getCustomElements()').to.deep.equal([]);
49
- expect(getCustomElements(win.document), 'getCustomElements(expected)').to.deep.equal([CE]);
50
-
51
- expect(getElementById(), 'getElementById()').to.be.null;
52
- expect(getElementById('alertDemo'), 'getElementById(id)').to.be.null;
53
- expect(
54
- getElementById('alertDemo', win.document),
55
- 'getElementById(id, parent)',
56
- ).to.be.instanceOf(win.HTMLDivElement);
57
-
58
- expect(getElementsByClassName(), 'getElementsByClassName()').to.be.instanceOf(
59
- HTMLCollection,
60
- );
61
- expect(
62
- getElementsByClassName('alert'),
63
- 'getElementsByClassName(selector)',
64
- ).to.be.instanceOf(HTMLCollection);
65
- expect(
66
- getElementsByClassName('alert', win.document),
67
- 'getElementsByClassName(selector, parent)',
68
- ).to.be.instanceOf(win.HTMLCollection);
69
-
70
- expect(getElementsByTagName(), 'getElementsByTagName()').to.be.instanceOf(HTMLCollection);
71
- expect(getElementsByTagName('div'), 'getElementsByTagName(selector)').to.be.instanceOf(
72
- HTMLCollection,
73
- );
74
- expect(
75
- getElementsByTagName('div', win.document),
76
- 'getElementsByTagName(selector, parent)',
77
- ).to.be.instanceOf(win.HTMLCollection);
78
-
79
- expect(
80
- [...getElementsByClassName('alert', win.document)].filter(x => matches(x, 'div')),
81
- 'matches(node, selector)',
82
- ).to.deep.equal([el]);
83
- });
84
- });
85
- });
@@ -1,50 +0,0 @@
1
- // sources
2
- // * https://github.com/enketo/enketo-express/blob/master/tools/esbuild-plugin-istanbul.js
3
- 'use strict';
4
- import esbuild from 'esbuild';
5
- import { promises } from 'fs';
6
- import { createInstrumenter } from 'istanbul-lib-instrument';
7
- import { extname, sep } from 'path';
8
- import tsCompile from './tsCompile';
9
-
10
- // import Cypress settings
11
- const sourceFolder = 'src';
12
- const [name] = process.cwd().split(sep).slice(-1);
13
-
14
- const sourceFilter = `${name}${sep}${sourceFolder}`;
15
- const instrumenter = createInstrumenter({
16
- compact: false,
17
- esModules: true,
18
- preserveComments: true,
19
- autoWrap: true,
20
- });
21
-
22
- const createEsbuildIstanbulPlugin = (): esbuild.Plugin => {
23
- return {
24
- name: 'istanbul',
25
- setup(build: esbuild.PluginBuild) {
26
- build.onLoad(
27
- { filter: /\.(ts|tsx)$/ },
28
- async ({ path }: esbuild.OnLoadArgs): Promise<{ contents: string } & Record<string, any>> => {
29
-
30
- if (!path.includes(sourceFilter)) {
31
- // console.log("> compiling typescript %s for output build", path);
32
- const contents = await promises.readFile(path, 'utf8');
33
- return {
34
- contents: ['.ts', '.tsx'].includes(extname(path)) ? tsCompile(path).outputText : contents,
35
- };
36
- }
37
-
38
- // console.log("🧡 instrumenting %s for output coverage", path);
39
- const { outputText, sourceMap } = tsCompile(path);
40
-
41
- return {
42
- contents: instrumenter.instrumentSync(outputText, path, sourceMap),
43
- };
44
- },
45
- );
46
- },
47
- };
48
- };
49
-
50
- export default createEsbuildIstanbulPlugin;
@@ -1,34 +0,0 @@
1
- // compile.ts
2
- import TypeScript from 'typescript';
3
- import { basename } from 'path';
4
- import { RawSourceMap } from 'source-map';
5
- import { readFileSync } from 'fs';
6
-
7
- export default function tsCompile(
8
- path: string,
9
- ops?: Partial<TypeScript.TranspileOptions>,
10
- ): TypeScript.TranspileOutput & { sourceMap: RawSourceMap } {
11
- // Default options -- you could also perform a merge, or use the project tsconfig.json
12
- const options: TypeScript.TranspileOptions = Object.assign(
13
- {
14
- compilerOptions: {
15
- allowJs: true,
16
- esModuleInterop: true,
17
- removeComments: false,
18
- target: 99, // ESNext
19
- allowSyntheticDefaultImports: true,
20
- isolatedModules: true,
21
- noEmitHelpers: true,
22
- sourceMap: true,
23
- } as Partial<TypeScript.CompilerOptions>,
24
- },
25
- ops,
26
- );
27
- const contents = readFileSync(path, { encoding: 'utf8' });
28
- const { outputText, sourceMapText } = TypeScript.transpileModule(contents, options);
29
- const sourceMap: RawSourceMap = JSON.parse(sourceMapText || '');
30
- sourceMap.file = basename(path);
31
- sourceMap.sources = [basename(path)];
32
-
33
- return { outputText, sourceMap, sourceMapText };
34
- }