@@ -9,23 +9,29 @@ import { Actions } from "../../../constants";
99import { mockDispatch } from "../../../__test_support__/fake_dispatch" ;
1010import * as mapUtil from "../../../farm_designer/map/util" ;
1111import { Mode } from "../../../farm_designer/map/interfaces" ;
12+ import { useFrame } from "@react-three/fiber" ;
13+ import { Quaternion } from "three" ;
1214import {
1315 createRenderer ,
1416 unmountRenderer ,
1517} from "../../../__test_support__/test_renderer" ;
1618
1719describe ( "<Weed />" , ( ) => {
1820 let getModeSpy : jest . SpyInstance ;
21+ let reactUseRefSpy : jest . SpyInstance | undefined ;
1922 const mountedWrappers : ReturnType < typeof createRenderer > [ ] = [ ] ;
2023
2124 beforeEach ( ( ) => {
2225 getModeSpy = jest . spyOn ( mapUtil , "getMode" ) . mockReturnValue ( Mode . none ) ;
26+ ( useFrame as jest . Mock ) . mockClear ( ) ;
2327 } ) ;
2428
2529 afterEach ( ( ) => {
2630 mountedWrappers . splice ( 0 ) . forEach ( wrapper =>
2731 unmountRenderer ( wrapper ) ) ;
2832 getModeSpy . mockRestore ( ) ;
33+ reactUseRefSpy ?. mockRestore ( ) ;
34+ reactUseRefSpy = undefined ;
2935 } ) ;
3036
3137 const fakeProps = ( ) : WeedProps => ( {
@@ -66,6 +72,20 @@ describe("<Weed />", () => {
6672 expect ( mockNavigate ) . toHaveBeenCalledWith ( Path . weeds ( "1" ) ) ;
6773 } ) ;
6874
75+ it ( "doesn't navigate after orbiting over a weed" , ( ) => {
76+ const p = fakeProps ( ) ;
77+ const dispatch = jest . fn ( ) ;
78+ p . dispatch = mockDispatch ( dispatch ) ;
79+ p . weed . body . id = 1 ;
80+ const wrapper = createRenderer ( < Weed { ...p } /> ) ;
81+ mountedWrappers . push ( wrapper ) ;
82+ const weed = wrapper . root
83+ . findAll ( node => node . props . name == "weed-1" ) [ 0 ] ;
84+ weed . props . onClick ( { delta : 3 } ) ;
85+ expect ( dispatch ) . not . toHaveBeenCalled ( ) ;
86+ expect ( mockNavigate ) . not . toHaveBeenCalled ( ) ;
87+ } ) ;
88+
6989 it ( "doesn't navigate to weed info" , ( ) => {
7090 const p = fakeProps ( ) ;
7191 p . dispatch = undefined ;
@@ -108,4 +128,64 @@ describe("<Weed />", () => {
108128 } ) ;
109129 expect ( mockNavigate ) . toHaveBeenCalledWith ( Path . weeds ( "1" ) ) ;
110130 } ) ;
131+
132+ it ( "navigates from a weed radius instance" , ( ) => {
133+ const p = fakeInstanceProps ( ) ;
134+ const dispatch = jest . fn ( ) ;
135+ p . dispatch = mockDispatch ( dispatch ) ;
136+ p . weeds [ 0 ] . body . id = 1 ;
137+ const wrapper = createRenderer ( < WeedInstances { ...p } /> ) ;
138+ mountedWrappers . push ( wrapper ) ;
139+ const weedRadius = wrapper . root
140+ . findAll ( node => node . props . name == "weed-radius" ) [ 0 ] ;
141+ weedRadius . props . onClick ( { instanceId : 0 } ) ;
142+ expect ( dispatch ) . toHaveBeenCalledWith ( {
143+ type : Actions . SET_PANEL_OPEN , payload : true ,
144+ } ) ;
145+ expect ( mockNavigate ) . toHaveBeenCalledWith ( Path . weeds ( "1" ) ) ;
146+ } ) ;
147+
148+ it ( "doesn't navigate after orbiting over weed instances" , ( ) => {
149+ const p = fakeInstanceProps ( ) ;
150+ const dispatch = jest . fn ( ) ;
151+ p . dispatch = mockDispatch ( dispatch ) ;
152+ p . weeds [ 0 ] . body . id = 1 ;
153+ const wrapper = createRenderer ( < WeedInstances { ...p } /> ) ;
154+ mountedWrappers . push ( wrapper ) ;
155+ const weedIcons = wrapper . root
156+ . findAll ( node => node . props . name == "weed-icons" ) [ 0 ] ;
157+ const weedRadius = wrapper . root
158+ . findAll ( node => node . props . name == "weed-radius" ) [ 0 ] ;
159+ weedIcons . props . onClick ( { instanceId : 0 , delta : 3 } ) ;
160+ weedRadius . props . onClick ( { instanceId : 0 , delta : 3 } ) ;
161+ expect ( dispatch ) . not . toHaveBeenCalled ( ) ;
162+ expect ( mockNavigate ) . not . toHaveBeenCalled ( ) ;
163+ } ) ;
164+
165+ it ( "updates weed icon matrices on frame" , ( ) => {
166+ const iconRef = {
167+ current : {
168+ setMatrixAt : jest . fn ( ) ,
169+ instanceMatrix : { needsUpdate : false } ,
170+ } ,
171+ } ;
172+ const radiusRef = {
173+ current : {
174+ setMatrixAt : jest . fn ( ) ,
175+ instanceMatrix : { needsUpdate : false } ,
176+ } ,
177+ } ;
178+ const updateStateRef = { current : { } } ;
179+ reactUseRefSpy = jest . spyOn ( React , "useRef" )
180+ . mockImplementationOnce ( ( ) => iconRef )
181+ . mockImplementationOnce ( ( ) => updateStateRef )
182+ . mockImplementationOnce ( ( ) => radiusRef )
183+ . mockImplementation ( value => ( { current : value } ) ) ;
184+ const wrapper = createRenderer ( < WeedInstances { ...fakeInstanceProps ( ) } /> ) ;
185+ mountedWrappers . push ( wrapper ) ;
186+ const frameFn = ( useFrame as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
187+ frameFn ( { camera : { quaternion : new Quaternion ( ) } } ) ;
188+ expect ( iconRef . current . setMatrixAt ) . toHaveBeenCalled ( ) ;
189+ expect ( iconRef . current . instanceMatrix . needsUpdate ) . toEqual ( true ) ;
190+ } ) ;
111191} ) ;
0 commit comments