/
landing.js
214 lines (192 loc) · 5.95 KB
/
landing.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* global document, window, localStorage */
// Allow taking a bit longer on the first run
const firstRun = localStorage.getItem('firstRun');
localStorage.setItem('firstRun', 'true');
const baseMinimalTime = firstRun ? 3000 : 1500;
document.addEventListener('DOMContentLoaded', () => {
const hand = document.querySelector('.jest-hand');
const cards = hand.querySelectorAll('.jest-card');
function cardTransform(offset, handWidth) {
const transform =
'rotate(' +
offset * 4 +
'deg) translateX(' +
(offset - (Math.abs(offset) * offset) / 7) *
Math.min(140, handWidth / 8) +
'px)';
return transform;
}
function positionCards() {
const handWidth = hand.offsetWidth;
cards.forEach(card => {
const offset = parseInt(card.dataset.index, 10) - 2;
card.parentElement.style.transform = cardTransform(offset, handWidth);
});
}
const results = [];
const timeouts = [];
function resolveRun(card, index, minTime) {
minTime = minTime || 500;
setTimeout(() => {
if (index === 2) {
results[index] = null;
card.classList.add('jest-card-run');
} else if (results[index]) {
card.classList.remove('jest-card-fail');
card.classList.add('jest-card-pass');
card.querySelectorAll('.jest-card-label').forEach(el => {
el.innerHTML = 'PASS';
});
} else {
card.classList.remove('jest-card-pass');
card.classList.add('jest-card-fail');
card.querySelectorAll('.jest-card-label').forEach(el => {
el.innerHTML = 'FAIL';
});
}
}, minTime);
if (timeouts[index]) {
clearTimeout(timeouts[index]);
}
timeouts[index] = setTimeout(
() => {
card.classList.remove('jest-card-running');
card.classList.add('jest-card-popping');
setTimeout(() => {
results[index] = results[index] || null;
card.classList.remove('jest-card-popping');
}, 400);
},
index === 2
? baseMinimalTime + minTime
: Math.random() * baseMinimalTime + minTime
);
}
function forceRun(minTime) {
let fails = 0;
cards.forEach((card, index) => {
card.classList.add('jest-card-running');
const result = index === 2 || fails > 1 || Math.random() > 0.25;
if (!result) {
fails += 1;
}
results[index] = result;
resolveRun(card, index, minTime);
});
}
function runTest(card, index) {
if (!card.classList.contains('jest-card-running') && !results[index]) {
if (index === 2) {
return forceRun(1000);
}
card.classList.add('jest-card-running');
if (results[index] == null) {
results[index] = Math.random() > 0.2;
resolveRun(card, index);
}
}
return undefined;
}
hand.addEventListener('click', ev => {
let card;
if (ev.target.classList.contains('jest-card-hitslop')) {
card = ev.target.firstChild;
} else if (ev.target.classList.contains('jest-card')) {
card = ev.target;
} else if (ev.target.classList.contains('jest-card-front')) {
card = ev.target.parentElement;
}
if (card) {
const index = parseInt(card.dataset.index, 10);
runTest(card, index);
}
});
let resizeTimeout;
window.addEventListener('resize', () => {
if (!resizeTimeout) {
resizeTimeout = setTimeout(() => {
resizeTimeout = null;
positionCards();
}, 500);
}
});
function setUpMatcherButtons() {
const matcherSection = document.querySelector('.matchers .blockContent');
const screenshotImg = document.querySelector('.matchers img');
const buttonWrapper = document.createElement('p');
buttonWrapper.className = 'buttons-wrapper';
const buttons = [
{
title: 'toBe',
url: '/img/content/matchers/toBe.png',
},
{
title: 'toBeCloseTo',
url: '/img/content/matchers/toBeCloseTo.png',
},
{
title: 'toEqual',
url: '/img/content/matchers/toEqual.png',
},
{
title: 'toStrictEqual',
url: '/img/content/matchers/toStrictEqual.png',
},
{
title: 'toHaveProperty',
url: '/img/content/matchers/toHaveProperty.png',
},
{
title: 'toMatchSnapshot',
url: '/img/content/matchers/toMatchSnapshot.png',
},
{
title: 'toThrowError',
url: '/img/content/matchers/toThrowError.png',
},
];
screenshotImg.onload = () => {
screenshotImg.style.opacity = 1;
};
for (const button of buttons) {
const clickButton = document.createElement('a');
clickButton.text = button.title;
clickButton.className = 'button landing';
clickButton.onclick = () => {
document
.querySelectorAll('.matchers .button.landing')
.forEach(b => (b.className = 'button landing'));
clickButton.className = 'button landing active';
screenshotImg.style.opacity = 0.5;
screenshotImg.src = button.url;
};
buttonWrapper.appendChild(clickButton);
}
matcherSection.appendChild(buttonWrapper);
const firstButton = document.querySelector(
'.matchers .blockContent .button'
);
firstButton.onclick();
}
// Without forking Docusaurus which is on route to a breaking major semver,
// we can't make the screenshots clickable. This fixes that with client-side
// JS. Let's call it progressive enhancement, sure.
function makeScreenshotsClickable() {
document.querySelectorAll('.blockImage img').forEach(img => {
img.style.cursor = 'pointer';
img.onclick = () => {
document.location = img.src;
};
});
}
forceRun(2000);
positionCards();
setUpMatcherButtons();
makeScreenshotsClickable();
});