@react-three/test-renderer 8.1.0 → 8.1.2

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.
@@ -9,13 +9,13 @@ const treeObjectFactory = (type, props, children) => ({
9
9
  props,
10
10
  children
11
11
  });
12
-
13
12
  const toTreeBranch = obj => obj.map(child => {
14
- return treeObjectFactory(lowerCaseFirstLetter(child.type || child.constructor.name), { ...child.__r3f.memoizedProps
13
+ return treeObjectFactory(lowerCaseFirstLetter(child.type || child.constructor.name), {
14
+ ...child.__r3f.memoizedProps
15
15
  }, toTreeBranch([...(child.children || []), ...child.__r3f.objects]));
16
16
  });
17
-
18
- const toTree = root => root.children.map(obj => treeObjectFactory(lowerCaseFirstLetter(obj.type), { ...obj.__r3f.memoizedProps
17
+ const toTree = root => root.children.map(obj => treeObjectFactory(lowerCaseFirstLetter(obj.type), {
18
+ ...obj.__r3f.memoizedProps
19
19
  }, toTreeBranch([...obj.children, ...obj.__r3f.objects])));
20
20
 
21
21
  const graphObjectFactory = (type, name, children) => ({
@@ -23,7 +23,6 @@ const graphObjectFactory = (type, name, children) => ({
23
23
  name,
24
24
  children
25
25
  });
26
-
27
26
  const toGraph = object => object.children.map(child => graphObjectFactory(child.type, child.name || '', toGraph(child)));
28
27
 
29
28
  const is = {
@@ -33,24 +32,19 @@ const is = {
33
32
  num: a => typeof a === 'number',
34
33
  und: a => a === void 0,
35
34
  arr: a => Array.isArray(a),
36
-
37
35
  equ(a, b) {
38
36
  // Wrong type or one of the two undefined, doesn't match
39
- if (typeof a !== typeof b || !!a !== !!b) return false; // Atomic, just compare a against b
40
-
41
- if (is.str(a) || is.num(a) || is.obj(a)) return a === b; // Array, shallow compare first to see if it's a match
42
-
43
- if (is.arr(a) && a == b) return true; // Last resort, go through keys
44
-
37
+ if (typeof a !== typeof b || !!a !== !!b) return false;
38
+ // Atomic, just compare a against b
39
+ if (is.str(a) || is.num(a) || is.obj(a)) return a === b;
40
+ // Array, shallow compare first to see if it's a match
41
+ if (is.arr(a) && a == b) return true;
42
+ // Last resort, go through keys
45
43
  let i;
46
-
47
44
  for (i in a) if (!(i in b)) return false;
48
-
49
45
  for (i in b) if (a[i] !== b[i]) return false;
50
-
51
46
  return is.und(i) ? a === b : true;
52
47
  }
53
-
54
48
  };
55
49
 
56
50
  const createCanvas = ({
@@ -61,15 +55,11 @@ const createCanvas = ({
61
55
  const canvas = document.createElement('canvas');
62
56
  canvas.height = height;
63
57
  canvas.width = width;
64
-
65
58
  canvas.addEventListener = () => {};
66
-
67
59
  canvas.removeEventListener = () => {};
68
-
69
60
  if (beforeReturn) {
70
61
  beforeReturn(canvas);
71
62
  }
72
-
73
63
  return canvas;
74
64
  };
75
65
 
@@ -410,7 +400,6 @@ const extensions = {
410
400
  EXT_sRGB: null,
411
401
  WEBGL_compressed_texture_etc1: null
412
402
  };
413
-
414
403
  class WebGLRenderingContext {
415
404
  constructor(canvas) {
416
405
  this.getShaderPrecisionFormat = () => {
@@ -420,30 +409,23 @@ class WebGLRenderingContext {
420
409
  precision: 23
421
410
  };
422
411
  };
423
-
424
412
  this.GL_VERSION = 7938;
425
413
  this.SCISSOR_BOX = 3088;
426
414
  this.VIEWPORT = 2978;
427
-
428
415
  this.getParameter = paramId => {
429
416
  switch (paramId) {
430
417
  case this.GL_VERSION:
431
418
  return ['WebGL1'];
432
-
433
419
  case this.SCISSOR_BOX:
434
420
  case this.VIEWPORT:
435
421
  return [0, 0, 1, 1];
436
422
  }
437
423
  };
438
-
439
424
  this.getExtension = ext => {
440
425
  return extensions[ext];
441
426
  };
442
-
443
427
  this.getProgramInfoLog = () => '';
444
-
445
428
  this.getShaderInfoLog = () => '';
446
-
447
429
  this.canvas = canvas;
448
430
  this.drawingBufferWidth = canvas.width;
449
431
  this.drawingBufferHeight = canvas.height;
@@ -454,28 +436,22 @@ class WebGLRenderingContext {
454
436
  });
455
437
  Object.assign(this, enums);
456
438
  }
457
-
458
439
  }
459
-
460
440
  const createWebGLContext = canvas => new WebGLRenderingContext(canvas);
461
441
 
462
442
  const createEventFirer = (act, store) => {
463
443
  const findEventHandler = (element, eventName) => {
464
444
  const eventHandlerName = toEventHandlerName(eventName);
465
445
  const props = element.props;
466
-
467
446
  if (typeof props[eventHandlerName] === 'function') {
468
447
  return props[eventHandlerName];
469
448
  }
470
-
471
449
  if (typeof props[eventName] === 'function') {
472
450
  return props[eventName];
473
451
  }
474
-
475
452
  console.warn(`Handler for ${eventName} was not found. You must pass event names in camelCase or name of the handler https://github.com/pmndrs/react-three-fiber/blob/master/packages/test-renderer/markdown/rttr.md#create-fireevent`);
476
453
  return null;
477
454
  };
478
-
479
455
  const createSyntheticEvent = (element, data) => {
480
456
  const raycastEvent = {
481
457
  camera: store.getState().camera,
@@ -487,23 +463,18 @@ const createEventFirer = (act, store) => {
487
463
  };
488
464
  return raycastEvent;
489
465
  };
490
-
491
466
  const invokeEvent = async (element, eventName, data) => {
492
467
  const handler = findEventHandler(element, eventName);
493
-
494
468
  if (!handler) {
495
469
  return;
496
470
  }
497
-
498
471
  let returnValue;
499
472
  await act(async () => {
500
473
  returnValue = handler(createSyntheticEvent(element, data));
501
474
  });
502
475
  return returnValue;
503
476
  };
504
-
505
477
  const fireEvent = async (element, eventName, data = {}) => await invokeEvent(element, eventName, data);
506
-
507
478
  return fireEvent;
508
479
  };
509
480
 
@@ -511,7 +482,6 @@ const expectOne = (items, msg) => {
511
482
  if (items.length === 1) {
512
483
  return items[0];
513
484
  }
514
-
515
485
  const prefix = items.length === 0 ? 'RTTR: No instances found' : `RTTR: Expected 1 but found ${items.length} instances`;
516
486
  throw new Error(`${prefix} ${msg}`);
517
487
  };
@@ -520,22 +490,20 @@ const matchProps = (props, filter) => {
520
490
  // Check for matches if filter contains regex matchers
521
491
  const isRegex = filter[key] instanceof RegExp;
522
492
  const shouldMatch = isRegex && typeof props[key] === 'string';
523
- const match = shouldMatch && filter[key].test(props[key]); // Bail if props aren't identical and filters found no match
493
+ const match = shouldMatch && filter[key].test(props[key]);
524
494
 
495
+ // Bail if props aren't identical and filters found no match
525
496
  if (props[key] !== filter[key] && !match) {
526
497
  return false;
527
498
  }
528
499
  }
529
-
530
500
  return true;
531
501
  };
532
502
  const findAll = (root, decider) => {
533
503
  const results = [];
534
-
535
504
  if (decider(root)) {
536
505
  results.push(root);
537
506
  }
538
-
539
507
  root.allChildren.forEach(child => {
540
508
  results.push(...findAll(child, decider));
541
509
  });
@@ -557,64 +525,46 @@ class ReactThreeTestInstance {
557
525
  return (fiber.children || []).map(fib => wrapFiber(fib));
558
526
  }
559
527
  };
560
-
561
528
  this.find = decider => expectOne(findAll(this, decider), `matching custom checker: ${decider.toString()}`);
562
-
563
529
  this.findAll = decider => findAll(this, decider);
564
-
565
530
  this.findByType = type => expectOne(findAll(this, node => Boolean(node.type && node.type === type)), `with node type: "${type || 'Unknown'}"`);
566
-
567
531
  this.findAllByType = type => findAll(this, node => Boolean(node.type && node.type === type));
568
-
569
532
  this.findByProps = props => expectOne(this.findAllByProps(props), `with props: ${JSON.stringify(props)}`);
570
-
571
533
  this.findAllByProps = props => findAll(this, node => Boolean(node.props && matchProps(node.props, props)));
572
-
573
534
  this._fiber = _fiber;
574
535
  }
575
-
576
536
  get instance() {
577
537
  return this._fiber;
578
538
  }
579
-
580
539
  get type() {
581
540
  return this._fiber.type;
582
541
  }
583
-
584
542
  get props() {
585
543
  return this._fiber.__r3f.memoizedProps;
586
544
  }
587
-
588
545
  get parent() {
589
546
  const parent = this._fiber.__r3f.parent;
590
-
591
547
  if (parent !== null) {
592
548
  return wrapFiber(parent);
593
549
  }
594
-
595
550
  return parent;
596
551
  }
597
-
598
552
  get children() {
599
553
  return this.getChildren(this._fiber);
600
554
  }
601
-
602
555
  get allChildren() {
603
556
  return this.getChildren(this._fiber, {
604
557
  exhaustive: true
605
558
  });
606
559
  }
607
-
608
560
  }
609
561
  const fiberToWrapper = new WeakMap();
610
562
  const wrapFiber = fiber => {
611
563
  let wrapper = fiberToWrapper.get(fiber);
612
-
613
564
  if (wrapper === undefined) {
614
565
  wrapper = new ReactThreeTestInstance(fiber);
615
566
  fiberToWrapper.set(fiber, wrapper);
616
567
  }
617
-
618
568
  return wrapper;
619
569
  };
620
570
 
@@ -623,9 +573,9 @@ var index$1 = /*#__PURE__*/Object.freeze({
623
573
  ReactThreeTestInstance: ReactThreeTestInstance
624
574
  });
625
575
 
576
+ // Extend catalogue for render API in tests.
626
577
  extend(THREE);
627
578
  const act = act$1;
628
-
629
579
  const create = async (element, options) => {
630
580
  const canvas = createCanvas({
631
581
  width: options == null ? void 0 : options.width,
@@ -635,87 +585,56 @@ const create = async (element, options) => {
635
585
  if (contextId === 'webgl' || contextId === 'webgl2') {
636
586
  return createWebGLContext(canvas);
637
587
  }
638
-
639
588
  return null;
640
589
  }
641
-
642
590
  canvas.getContext = getContext;
643
591
  }
644
592
  });
645
- const _fiber = canvas;
646
-
647
- const _root = createRoot(_fiber).configure({
593
+ const _root = createRoot(canvas).configure({
648
594
  frameloop: 'never',
649
595
  ...options,
650
596
  events: undefined
651
597
  });
652
-
653
- let scene = null;
654
- await act(async () => {
655
- scene = _root.render(element).getState().scene;
656
- });
657
- const _store = _roots.get(_fiber).store;
598
+ const _store = _roots.get(canvas).store;
599
+ await act(async () => _root.render(element));
600
+ const scene = _store.getState().scene;
658
601
  return {
659
602
  scene: wrapFiber(scene),
660
- unmount: async () => {
661
- await act(async () => {
662
- _root.unmount();
663
- });
603
+ async unmount() {
604
+ await act(async () => _root.unmount());
664
605
  },
665
- getInstance: () => {
666
- var _mockRoots$get;
667
-
668
- // this is our root
669
- const fiber = (_mockRoots$get = _roots.get(_fiber)) == null ? void 0 : _mockRoots$get.fiber;
670
- const current = fiber == null ? void 0 : fiber.current.child.child;
606
+ getInstance() {
607
+ // Bail if canvas is unmounted
608
+ if (!_roots.has(canvas)) return null;
671
609
 
672
- if (current) {
673
- const root = {
674
- /**
675
- * we wrap our child in a Provider component
676
- * and context.Provider, so do a little
677
- * artificial dive to get round this and
678
- * pass context.Provider as if it was the
679
- * actual react root
680
- */
681
- current
682
- };
683
- /**
684
- * so this actually returns the instance
685
- * the user has passed through as a Fiber
686
- */
687
-
688
- return reconciler.getPublicRootInstance(root);
689
- } else {
690
- return null;
691
- }
692
- },
693
- update: async newElement => {
694
- var _mockRoots$get2;
695
-
696
- const fiber = (_mockRoots$get2 = _roots.get(_fiber)) == null ? void 0 : _mockRoots$get2.fiber;
697
-
698
- if (fiber) {
699
- await act(async () => {
700
- reconciler.updateContainer(newElement, fiber, null, () => null);
701
- });
610
+ // Traverse fiber nodes for R3F root
611
+ const root = {
612
+ current: _roots.get(canvas).fiber.current
613
+ };
614
+ while (!((_root$current$child = root.current.child) != null && _root$current$child.stateNode)) {
615
+ var _root$current$child;
616
+ root.current = root.current.child;
702
617
  }
703
618
 
704
- return;
619
+ // Return R3F instance from root
620
+ return reconciler.getPublicRootInstance(root);
621
+ },
622
+ async update(newElement) {
623
+ if (!_roots.has(canvas)) return console.warn('RTTR: attempted to update an unmounted root!');
624
+ await act(async () => _root.render(newElement));
705
625
  },
706
- toTree: () => {
626
+ toTree() {
707
627
  return toTree(scene);
708
628
  },
709
- toGraph: () => {
629
+ toGraph() {
710
630
  return toGraph(scene);
711
631
  },
712
632
  fireEvent: createEventFirer(act, _store),
713
- advanceFrames: async (frames, delta = 1) => {
633
+ async advanceFrames(frames, delta = 1) {
714
634
  const state = _store.getState();
715
-
716
635
  const storeSubscribers = state.internal.subscribers;
717
636
  const promises = [];
718
- storeSubscribers.forEach(subscriber => {
637
+ for (const subscriber of storeSubscribers) {
719
638
  for (let i = 0; i < frames; i++) {
720
639
  if (is.arr(delta)) {
721
640
  promises.push(new Promise(() => subscriber.ref.current(state, delta[i] || delta[-1])));
@@ -723,7 +642,7 @@ const create = async (element, options) => {
723
642
  promises.push(new Promise(() => subscriber.ref.current(state, delta)));
724
643
  }
725
644
  }
726
- });
645
+ }
727
646
  Promise.all(promises);
728
647
  }
729
648
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/test-renderer",
3
- "version": "8.1.0",
3
+ "version": "8.1.2",
4
4
  "description": "Test Renderer for react-three-fiber",
5
5
  "author": "Josh Ellis",
6
6
  "license": "MIT",