23
let focusWithin;
24
let getFindAllNodesFailureDescription;
25
let observeVisibleRects;
26
+ let mockIntersectionObserver;
27
+ let simulateIntersection;
28
+ let setBoundingClientRect;
29
30
let container;
31
54
55
container = document.createElement('div');
56
document.body.appendChild(container);
57
+ const IntersectionMocks = require('./utils/IntersectionMocks');
58
+ mockIntersectionObserver = IntersectionMocks.mockIntersectionObserver;
59
+ simulateIntersection = IntersectionMocks.simulateIntersection;
60
+ setBoundingClientRect = IntersectionMocks.setBoundingClientRect;
61
});
62
63
afterEach(() => {
615
});
616
617
describe('findBoundingRects', () => {
611
- // Stub out getBoundingClientRect for the specified target.
612
- // This API is required by the test selectors but it isn't implemented by jsdom.
613
- function setBoundingClientRect(target, {x, y, width, height}) {
614
- target.getBoundingClientRect = function () {
615
- return {
616
- width,
617
- height,
618
- left: x,
619
- right: x + width,
620
- top: y,
621
- bottom: y + height,
622
- };
623
- };
624
- }
625
-
618
// @gate www || experimental
619
it('should return a single rect for a component that returns a single root host element', async () => {
620
const ref = React.createRef();
1215
});
1216
1217
describe('observeVisibleRects', () => {
1226
- // Stub out getBoundingClientRect for the specified target.
1227
- // This API is required by the test selectors but it isn't implemented by jsdom.
1228
- function setBoundingClientRect(target, {x, y, width, height}) {
1229
- target.getBoundingClientRect = function () {
1230
- return {
1231
- width,
1232
- height,
1233
- left: x,
1234
- right: x + width,
1235
- top: y,
1236
- bottom: y + height,
1237
- };
1238
- };
1239
- }
1240
-
1241
- function simulateIntersection(...entries) {
1242
- callback(
1243
- entries.map(([target, rect, ratio]) => ({
1244
- boundingClientRect: {
1245
- top: rect.y,
1246
- left: rect.x,
1247
- width: rect.width,
1248
- height: rect.height,
1249
- },
1250
- intersectionRatio: ratio,
1251
- target,
1252
- })),
1253
- );
1254
- }
1255
-
1256
- let callback;
1257
- let observedTargets;
1218
+ let observerMock;
1219
1220
beforeEach(() => {
1260
- callback = null;
1261
- observedTargets = [];
1262
-
1263
- class IntersectionObserver {
1264
- constructor() {
1265
- callback = arguments[0];
1266
- }
1267
-
1268
- disconnect() {
1269
- callback = null;
1270
- observedTargets.splice(0);
1271
- }
1272
-
1273
- observe(target) {
1274
- observedTargets.push(target);
1275
- }
1276
-
1277
- unobserve(target) {
1278
- const index = observedTargets.indexOf(target);
1279
- if (index >= 0) {
1280
- observedTargets.splice(index, 1);
1281
- }
1282
- }
1283
- }
1284
-
1285
- // This is a broken polyfill.
1286
- // It is only intended to provide bare minimum test coverage.
1287
- // More meaningful tests will require the use of fixtures.
1288
- window.IntersectionObserver = IntersectionObserver;
1221
+ observerMock = mockIntersectionObserver();
1222
});
1223
1224
// @gate www || experimental
1250
handleVisibilityChange,
1251
);
1252
1320
- expect(callback).not.toBeNull();
1321
- expect(observedTargets).toHaveLength(1);
1253
+ expect(observerMock.callback).not.toBeNull();
1254
+ expect(observerMock.observedTargets).toHaveLength(1);
1255
expect(handleVisibilityChange).not.toHaveBeenCalled();
1256
1257
// Simulate IntersectionObserver notification.
1303
handleVisibilityChange,
1304
);
1305
1373
- expect(callback).not.toBeNull();
1374
- expect(observedTargets).toHaveLength(2);
1306
+ expect(observerMock.callback).not.toBeNull();
1307
+ expect(observerMock.observedTargets).toHaveLength(2);
1308
expect(handleVisibilityChange).not.toHaveBeenCalled();
1309
1310
// Simulate IntersectionObserver notification.
1370
handleVisibilityChange,
1371
);
1372
1440
- expect(callback).not.toBeNull();
1441
- expect(observedTargets).toHaveLength(1);
1373
+ expect(observerMock.callback).not.toBeNull();
1374
+ expect(observerMock.observedTargets).toHaveLength(1);
1375
expect(handleVisibilityChange).not.toHaveBeenCalled();
1376
1377
disconnect();
1445
- expect(callback).toBeNull();
1378
+ expect(observerMock.callback).toBeNull();
1379
});
1380
1381
// This test reuires gating because it relies on the __DEV__ only commit hook to work.
1503
handleVisibilityChange,
1504
);
1505
1573
- expect(callback).not.toBeNull();
1574
- expect(observedTargets).toHaveLength(1);
1575
- expect(observedTargets[0]).toBe(ref1.current);
1506
+ expect(observerMock.callback).not.toBeNull();
1507
+ expect(observerMock.observedTargets).toHaveLength(1);
1508
+ expect(observerMock.observedTargets[0]).toBe(ref1.current);
1509
});
1510
});
1511
});