@react-three/test-renderer 8.1.0 → 8.1.1

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