File tree Expand file tree Collapse file tree
workspaces/global-floating-action-button
plugins/global-floating-action-button/src/components Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ ' @red-hat-developer-hub/backstage-plugin-global-floating-action-button ' : patch
3+ ---
4+
5+ render fab on document body when the target element is not found
Original file line number Diff line number Diff line change @@ -49,21 +49,36 @@ beforeEach(() => {
4949 document . body . innerHTML = '<div class="BackstagePage-root-123"></div>' ;
5050} ) ;
5151
52+ const renderFab = ( htmlContent : string ) => {
53+ document . body . innerHTML = htmlContent ;
54+ render (
55+ < FloatingButton
56+ floatingButtons = { [
57+ {
58+ icon : < AddIcon /> ,
59+ label : 'Add' ,
60+ } ,
61+ ] }
62+ slot = { Slot . BOTTOM_LEFT }
63+ /> ,
64+ ) ;
65+ } ;
66+
5267describe ( 'Floating Button' , ( ) => {
5368 it ( 'should render a floating button' , ( ) => {
54- render (
55- < FloatingButton
56- floatingButtons = { [
57- {
58- icon : < AddIcon /> ,
59- label : 'Add' ,
60- color : 'primary' ,
61- toolTip : 'Main menu' ,
62- } ,
63- ] }
64- slot = { Slot . BOTTOM_LEFT }
65- /> ,
66- ) ;
69+ renderFab ( '<div class="BackstagePage-root-123"></div>' ) ;
70+ expect ( screen . getByTestId ( 'floating-button' ) ) . toBeInTheDocument ( ) ;
71+ expect ( screen . getByTestId ( 'AddIcon' ) ) . toBeInTheDocument ( ) ;
72+ } ) ;
73+
74+ it ( 'should render a floating button in the UI when the `BackstagePage-root` classname is not found' , ( ) => {
75+ renderFab ( '<div class="BackstagePage-xxx-123"></div>' ) ;
76+ expect ( screen . getByTestId ( 'floating-button' ) ) . toBeInTheDocument ( ) ;
77+ expect ( screen . getByTestId ( 'AddIcon' ) ) . toBeInTheDocument ( ) ;
78+ } ) ;
79+
80+ it ( 'should render a floating button when the `BackstagePage-root` classname is not found but the html tag is found' , ( ) => {
81+ renderFab ( '<main class="BackstagePage-xxx-123"></div>' ) ;
6782 expect ( screen . getByTestId ( 'floating-button' ) ) . toBeInTheDocument ( ) ;
6883 expect ( screen . getByTestId ( 'AddIcon' ) ) . toBeInTheDocument ( ) ;
6984 } ) ;
Original file line number Diff line number Diff line change @@ -59,7 +59,9 @@ export const FloatingButton = ({
5959
6060 React . useEffect ( ( ) => {
6161 const checkTargetElement = ( ) => {
62- const element = document . querySelector ( '[class^="BackstagePage-root"]' ) ;
62+ const element =
63+ document . querySelector ( '[class^="BackstagePage-root"]' ) ??
64+ document . querySelector ( 'main' ) ;
6365 if ( element ) {
6466 setTargetElement ( element ) ;
6567 } else {
@@ -80,9 +82,7 @@ export const FloatingButton = ({
8082 if ( fabs ?. length === 0 ) {
8183 return null ;
8284 }
83- if ( ! targetElement ) {
84- return null ;
85- }
85+
8686 let fabDiv ;
8787 if ( fabs . length > 1 ) {
8888 fabDiv = (
You can’t perform that action at this time.
0 commit comments