Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
frontend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
minerva
frontend
Commits
ac14c550
Commit
ac14c550
authored
1 year ago
by
Adrian Orłów
Browse files
Options
Downloads
Patches
Plain Diff
feat(map): improve hook and tests of useOlMap
parent
38b68875
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!223
reset the pin numbers before search results are fetch (so the results will be...
,
!36
feat(map): add render components and tests
Pipeline
#79930
failed
1 year ago
Stage: lint
Stage: prettier
Stage: test
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/components/Map/MapViewer/utils/useOlMap.test.ts
+88
-0
88 additions, 0 deletions
src/components/Map/MapViewer/utils/useOlMap.test.ts
src/components/Map/MapViewer/utils/useOlMap.ts
+10
-7
10 additions, 7 deletions
src/components/Map/MapViewer/utils/useOlMap.ts
with
98 additions
and
7 deletions
src/components/Map/MapViewer/utils/useOlMap.test.ts
0 → 100644
+
88
−
0
View file @
ac14c550
import
mapSlice
,
{
setMapData
}
from
'
@/redux/map/map.slice
'
;
import
{
getReduxWrapperUsingSliceReducer
}
from
'
@/utils/testing/getReduxWrapperUsingSliceReducer
'
;
import
{
renderHook
,
waitFor
}
from
'
@testing-library/react
'
;
import
{
Map
}
from
'
ol
'
;
import
React
from
'
react
'
;
import
{
useOlMap
}
from
'
./useOlMap
'
;
const
useRefValue
=
{
current
:
null
,
};
Object
.
defineProperty
(
useRefValue
,
'
current
'
,
{
get
:
jest
.
fn
(()
=>
({
innerHTML
:
''
,
appendChild
:
jest
.
fn
(),
addEventListener
:
jest
.
fn
(),
getRootNode
:
jest
.
fn
(),
})),
set
:
jest
.
fn
(()
=>
({
innerHTML
:
''
,
appendChild
:
jest
.
fn
(),
addEventListener
:
jest
.
fn
(),
getRootNode
:
jest
.
fn
(),
})),
});
jest
.
spyOn
(
React
,
'
useRef
'
).
mockReturnValue
(
useRefValue
);
describe
(
'
useOlMap - util
'
,
()
=>
{
const
{
Wrapper
,
store
}
=
getReduxWrapperUsingSliceReducer
(
'
map
'
,
mapSlice
);
describe
(
'
when initializing
'
,
()
=>
{
it
(
'
should set map instance
'
,
async
()
=>
{
const
dummyElement
=
document
.
createElement
(
'
div
'
);
const
{
result
}
=
renderHook
(()
=>
useOlMap
({
target
:
dummyElement
}),
{
wrapper
:
Wrapper
});
await
waitFor
(()
=>
expect
(
result
.
current
.
mapInstance
).
toBeInstanceOf
(
Map
));
});
it
(
'
should render content inside the target element
'
,
async
()
=>
{
const
FIRST_NODE
=
0
;
const
dummyElement
=
document
.
createElement
(
'
div
'
);
renderHook
(()
=>
useOlMap
({
target
:
dummyElement
}),
{
wrapper
:
Wrapper
});
expect
(
dummyElement
.
childNodes
[
FIRST_NODE
]).
toHaveClass
(
'
ol-viewport
'
);
});
});
describe
(
'
when initialized
'
,
()
=>
{
it
(
'
should modify view of the map instance on position config change
'
,
async
()
=>
{
const
dummyElement
=
document
.
createElement
(
'
div
'
);
const
{
result
}
=
renderHook
(()
=>
useOlMap
({
target
:
dummyElement
}),
{
wrapper
:
Wrapper
});
const
setViewSpy
=
jest
.
spyOn
(
result
.
current
.
mapInstance
as
Map
,
'
setView
'
);
const
CALLED_ONCE
=
1
;
store
.
dispatch
(
setMapData
({
position
:
{
x
:
0
,
y
:
0
,
},
}),
);
await
waitFor
(()
=>
expect
(
setViewSpy
).
toBeCalledTimes
(
CALLED_ONCE
));
});
it
(
'
should modify layers of the map instance on size config change
'
,
async
()
=>
{
const
dummyElement
=
document
.
createElement
(
'
div
'
);
const
{
result
}
=
renderHook
(()
=>
useOlMap
({
target
:
dummyElement
}),
{
wrapper
:
Wrapper
});
const
setLayersSpy
=
jest
.
spyOn
(
result
.
current
.
mapInstance
as
Map
,
'
setLayers
'
);
const
CALLED_ONCE
=
1
;
store
.
dispatch
(
setMapData
({
size
:
{
maxZoom
:
10
,
minZoom
:
2
,
tileSize
:
256
,
width
:
1000
,
height
:
1000
,
},
}),
);
await
waitFor
(()
=>
expect
(
setLayersSpy
).
toBeCalledTimes
(
CALLED_ONCE
));
});
});
});
This diff is collapsed.
Click to expand it.
src/components/Map/MapViewer/utils/useOlMap.ts
+
10
−
7
View file @
ac14c550
import
Map
from
'
ol/Map
'
;
import
{
MutableRefObject
,
useEffect
,
useRef
,
useState
}
from
'
react
'
;
import
React
,
{
MutableRefObject
,
useEffect
,
useState
}
from
'
react
'
;
import
{
MapInstance
}
from
'
../MapViewer.types
'
;
import
{
useOlMapConfig
}
from
'
./useOlMapConfig
'
;
import
{
useOlMapInit
}
from
'
./useOlMapInit
'
;
interface
UseOlMapInput
{
target
?:
HTMLElement
;
}
interface
UseOlMapOutput
{
mapRef
:
MutableRefObject
<
null
|
HTMLDivElement
>
;
mapInstance
:
MapInstance
;
}
type
UseOlMap
=
()
=>
UseOlMapOutput
;
type
UseOlMap
=
(
input
?:
UseOlMapInput
)
=>
UseOlMapOutput
;
export
const
useOlMap
:
UseOlMap
=
()
=>
{
const
mapRef
=
useRef
<
null
|
HTMLDivElement
>
(
null
);
export
const
useOlMap
:
UseOlMap
=
(
{
target
}
=
{}
)
=>
{
const
mapRef
=
React
.
useRef
<
null
|
HTMLDivElement
>
(
null
);
const
[
mapInstance
,
setMapInstance
]
=
useState
<
MapInstance
>
(
undefined
);
const
mapConfig
=
useOlMapConfig
();
useOlMapInit
();
...
...
@@ -24,11 +27,11 @@ export const useOlMap: UseOlMap = () => {
}
const
map
=
new
Map
({
target
:
mapRef
.
current
,
target
:
target
||
mapRef
.
current
,
});
setMapInstance
(
map
);
},
[]);
setMapInstance
(
currentMap
=>
currentMap
||
map
);
},
[
target
]);
useEffect
(()
=>
{
if
(
!
mapInstance
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment