forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.spec.ts
88 lines (72 loc) 路 2.99 KB
/
utils.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import * as utils from '../utils';
import 'jest-dom/extend-expect'
describe('isCoveredByReact', () => {
it('should identify standard events as covered by React', () => {
expect(utils.isCoveredByReact('click', document)).toEqual(true);
});
it('should identify custom events as not covered by React', () => {
expect(utils.isCoveredByReact('change', document)).toEqual(true);
expect(utils.isCoveredByReact('ionchange', document)).toEqual(false);
});
});
describe('syncEvent', () => {
it('should add event on sync and readd on additional syncs', () => {
var div = document.createElement("div");
const addEventListener = jest.spyOn(div, "addEventListener");
const removeEventListener = jest.spyOn(div, "removeEventListener");
const ionClickCallback = jest.fn();
utils.syncEvent(div, 'ionClick', ionClickCallback);
expect(removeEventListener).not.toHaveBeenCalled();
expect(addEventListener).toHaveBeenCalledWith('ionClick', expect.any(Function));
utils.syncEvent(div, 'ionClick', ionClickCallback);
expect(removeEventListener).toHaveBeenCalledWith('ionClick', expect.any(Function));
expect(addEventListener).toHaveBeenCalledWith('ionClick', expect.any(Function));
const event = new CustomEvent('ionClick', { detail: 'test'});
div.dispatchEvent(event);
expect(ionClickCallback).toHaveBeenCalled();
})
});
describe('ensureElementInBody', () => {
it('should return if exists', () => {
const element = document.createElement("some-random-thing");
document.body.innerHTML = '';
document.body.appendChild(element);
const returnedElement = utils.ensureElementInBody('some-random-thing');
expect(returnedElement).toEqual(element);
expect(document.body.children.length).toEqual(1);
});
it('should create if it does not exist', () => {
document.body.innerHTML = '';
const returnedElement = utils.ensureElementInBody('some-random-thing');
expect(returnedElement).toBeDefined();
expect(returnedElement.tagName).toEqual('SOME-RANDOM-THING');
expect(document.body.children.length).toEqual(1);
});
});
describe('attachEventProps', () => {
it('should pass props to a dom node', () => {
const onIonClickCallback = () => {};
var div = document.createElement("div");
utils.attachEventProps(div, {
'children': [],
'style': 'color: red',
'ref': () => {},
'onClick': () => {},
'onIonClick': onIonClickCallback,
'testprop': ['red']
});
expect((div as any).testprop).toEqual(['red']);
expect(div).toHaveStyle('');
expect(Object.keys((div as any).__events)).toEqual(['ionClick']);
});
});
describe('generateUniqueId', () => {
const uniqueRegexMatch = /^(\w){8}-(\w){4}-(\w){4}-(\w){4}-(\w){12}$/;
it('should generate a global unique id', () => {
const first = utils.generateUniqueId();
const second = utils.generateUniqueId();
expect(first).toMatch(uniqueRegexMatch);
expect(second).not.toEqual(first);
expect(second).toMatch(uniqueRegexMatch);
});
});