-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.test.js
145 lines (103 loc) · 4.89 KB
/
script.test.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require('./test-helper');
require('./script');
var setUrls = false;
global.console = {
warn: jest.fn()
}
beforeEach(() => {
global.console.warn.mockClear();
});
test('Script loads', ()=>{
expect(window.powerbiresizescript).toBe(1);
});
test('Multiple reports on the page should all work', () => {
createElement('16', '9', '', '', 1000, 700, true, setUrls);
createElement('4', '3', '', '', 1200, 700, false, setUrls);
expect(document.body.children.length).toBe(2);
});
test('Client width greater than 569 with ratio 16x9', () => {
let element = createElement('16', '9', '', '', 1000, 700, true, setUrls);
window.document.dispatchEvent(new Event("DOMContentLoaded"));
let img = getChildByTag(element, 'img');
expect(img.style.width).toBe("100%");
expect(img.style.height).toBe("618.5px");
});
test('Client width less 569 with ratio 16x9', () => {
let element = createElement('16', '9', '', '', 560, 700, true, setUrls);
window.document.dispatchEvent(new Event("DOMContentLoaded"));
let img = getChildByTag(element, 'img');
expect(element.getAttribute('pbi-resize-width')).toBe("16");
expect(element.getAttribute('pbi-resize-height')).toBe("9");
setSize(img, '568.88px', (320 + 56 + 'px'));
expect(global.console.warn).toHaveBeenCalledTimes(2);
});
test('Client width less than 437 with ratio 4x3', () => {
let element = createElement('4', '3', '', '', 430, 700, true, setUrls);
window.document.dispatchEvent(new Event("DOMContentLoaded"));
let img = getChildByTag(element, 'img');
expect(img.style.width).toBe("426.66px");
expect(img.style.height).toBe("376px");
expect(global.console.warn).toHaveBeenCalledTimes(2);
});
test('Max height less than 320', () => {
let clientWidth = 400;
let element = createElement('4', '3', '4', '3', clientWidth, 700, true, setUrls);
window.document.dispatchEvent(new Event("DOMContentLoaded"));
let img = getChildByTag(element, 'img');
expect(img.style.width).toBe('426.66px');
expect(img.style.height).toBe('376px');
expect(global.console.warn).toHaveBeenCalledTimes(2);
});
test('Client width less than 320', () => {
let clientWidth = 300;
let element = createElement('4', '6', '4', '6', clientWidth, 700, true, setUrls);
window.document.dispatchEvent(new Event("DOMContentLoaded"));
let img = getChildByTag(element, 'img');
expect(img.style.width).toBe('320px');
expect(img.style.height).toBe('536px');
expect(global.console.warn).toHaveBeenCalledTimes(2);
});
test('Max height or client width greater than 320', () => {
let clientWidth = 400;
let element = createElement('5', '6', '5', '6', clientWidth, 700, true, setUrls);
window.document.dispatchEvent(new Event("DOMContentLoaded"));
let sizeRatio = element.getAttribute('pbi-resize-width') / element.getAttribute('pbi-resize-height');
let maxHeight = Math.max(clientWidth, 320) / sizeRatio;
let height = maxHeight + 56 + "px";
let img = getChildByTag(element, 'img');
expect(img.style.width).toBe(clientWidth + 'px');
expect(img.style.height).toBe(height);
expect(global.console.warn).toHaveBeenCalledTimes(2);
});
test('Client width greater than 569 with ratio 4x3', () => {
let clientWidth = 800;
let element = createElement('4', '3', '', '', clientWidth, 700, true, setUrls);
window.document.dispatchEvent(new Event("DOMContentLoaded"));
let sizeRatio = element.getAttribute('pbi-resize-width') / element.getAttribute('pbi-resize-height');
let newHeight = Math.max(clientWidth / sizeRatio, 320) + 56 + "px";
element.setAttribute('height', newHeight);
expect(element.getAttribute('width')).toBe("100%");
expect(element.getAttribute('height')).toBe(newHeight);
expect(global.console.warn).toHaveBeenCalledTimes(0);
});
test('Resize window less than 569 with aspect ratio 16x9', () => {
let clientWidth = 800;
let element = createElement('16', '9', '16', '9', clientWidth, 700, true, setUrls);
window.document.dispatchEvent(new Event("DOMContentLoaded"));
expect(global.console.warn).toHaveBeenCalledTimes(0);
let img = getChildByTag(element, 'img');
Object.defineProperty(element, 'clientWidth', { value: 500, configurable: true });
window.dispatchEvent(new Event('resize'));
expect(img.style.width).toBe('568.88px');
expect(img.style.height).toBe((320 + 56 + 'px'));
expect(global.console.warn).toHaveBeenCalledTimes(2);
});
test('Still works with mobile', () => {
let element = createElement('', '', '4', '3', 500, 700, true, null);
element.setAttribute("pbi-resize-m-img", 'pbi-resize-m-img');
element.setAttribute("pbi-resize-m-src", 'pbi-resize-m-src');
window.document.dispatchEvent(new Event("DOMContentLoaded"));
let img = getChildByTag(element, 'img');
expect(img.style.width).toBe("100%");
expect(img.style.height).toBe(Math.max(500 / (4/3), 320) + 56 + "px");
});