-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sitewidebanner to publisher
- Loading branch information
1 parent
db74a23
commit 6b6ab33
Showing
7 changed files
with
132 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ NEW_RELIC_APP_ID=null | |
NEW_RELIC_LICENSE_KEY=null | ||
APP_ID='' | ||
MFE_CONFIG_API_URL='' | ||
SITEWIDE_BANNER_CONTENT = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import Cookies from 'js-cookie'; | ||
import { Alert } from 'react-bootstrap'; | ||
import SitewideBanner from './index'; | ||
|
||
describe('SitewideBanner', () => { | ||
it('renders correctly when visible', () => { | ||
const wrapper = shallow( | ||
<SitewideBanner | ||
message="Dummy Message" | ||
type="success" | ||
dismissible | ||
cookieName="bannerCookie" | ||
cookieExpiryDays={7} | ||
/>, | ||
); | ||
|
||
expect(wrapper.find(Alert).props().variant).toBe('success'); | ||
expect(wrapper.find(Alert).props().dismissible).toBe(true); | ||
const alertContent = wrapper.find(Alert) | ||
.dive() | ||
.find('div') | ||
.first() | ||
.html(); | ||
expect(alertContent).toContain('Dummy Message'); | ||
}); | ||
|
||
it('calls handleDismiss and sets cookie when dismissed', () => { | ||
const setCookieMock = jest.spyOn(Cookies, 'set'); | ||
const wrapper = shallow( | ||
<SitewideBanner | ||
message="This is a test message" | ||
type="warning" | ||
dismissible | ||
cookieName="bannerCookie" | ||
cookieExpiryDays={7} | ||
/>, | ||
); | ||
wrapper.find(Alert).simulate('close'); | ||
expect(wrapper.isEmptyRender()).toBe(true); | ||
expect(setCookieMock).toHaveBeenCalledWith('bannerCookie', 'true', { | ||
expires: 7, | ||
}); | ||
setCookieMock.mockRestore(); | ||
}); | ||
|
||
it('handles non-dismissible banner correctly', () => { | ||
const wrapper = shallow( | ||
<SitewideBanner message="Non-dismissible message" dismissible={false} />, | ||
); | ||
expect(wrapper.find(Alert).props().dismissible).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Cookies from 'js-cookie'; | ||
import { | ||
Alert, Container, | ||
} from 'react-bootstrap'; | ||
|
||
const SitewideBanner = ({ | ||
message, type, dismissible, cookieName, cookieExpiryDays, | ||
}) => { | ||
const [isVisible, setIsVisible] = useState(true); | ||
|
||
useEffect(() => { | ||
if (cookieName && Cookies.get(cookieName)) { | ||
setIsVisible(false); | ||
} | ||
}, [cookieName]); | ||
|
||
const handleDismiss = () => { | ||
setIsVisible(false); | ||
if (cookieName) { | ||
Cookies.set(cookieName, 'true', { expires: cookieExpiryDays }); | ||
} | ||
}; | ||
|
||
if (isVisible) { | ||
return ( | ||
<Alert | ||
variant={type} | ||
dismissible={dismissible} | ||
onClose={handleDismiss} | ||
className="mb-4" | ||
> | ||
<Container> | ||
<div dangerouslySetInnerHTML={{ __html: message }} /> | ||
</Container> | ||
</Alert> | ||
); | ||
} else { // eslint-disable-line no-else-return | ||
return null; | ||
} | ||
}; | ||
|
||
SitewideBanner.propTypes = { | ||
message: PropTypes.string.isRequired, | ||
type: PropTypes.oneOf(['primary', 'success', 'warning', 'danger', 'info', 'secondary', 'light', 'dark']), | ||
dismissible: PropTypes.bool, | ||
cookieName: PropTypes.string, | ||
cookieExpiryDays: PropTypes.number, | ||
}; | ||
|
||
SitewideBanner.defaultProps = { | ||
type: 'info', | ||
dismissible: false, | ||
cookieName: null, | ||
cookieExpiryDays: 7, | ||
}; | ||
|
||
export default SitewideBanner; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters