mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2024-11-14 17:35:35 +01:00
Merge branch '2-alert-context' into 'dev'
Resolve "Extract alert context from OpenVPN plugin" Closes #2 See merge request turris/reforis/foris-js!7
This commit is contained in:
commit
edc5c92421
2
package-lock.json
generated
2
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "foris",
|
||||
"version": "0.1.0-beta",
|
||||
"version": "0.1.0-beta.2",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "foris",
|
||||
"version": "0.1.0-beta",
|
||||
"version": "0.1.0-beta.2",
|
||||
"description": "Set of components and utils for Foris and its plugins.",
|
||||
"author": "CZ.NIC, z.s.p.o.",
|
||||
"repository": {
|
||||
|
|
35
src/alertContext/AlertContext.js
Normal file
35
src/alertContext/AlertContext.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/)
|
||||
*
|
||||
* This is free software, licensed under the GNU General Public License v3.
|
||||
* See /LICENSE for more information.
|
||||
*/
|
||||
|
||||
import React, { useState } from "react";
|
||||
import PropTypes from "prop-types";
|
||||
|
||||
import { Alert } from "bootstrap/Alert";
|
||||
|
||||
const AlertContext = React.createContext();
|
||||
|
||||
AlertContextProvider.propTypes = {
|
||||
children: PropTypes.oneOfType([
|
||||
PropTypes.arrayOf(PropTypes.node),
|
||||
PropTypes.node,
|
||||
]),
|
||||
};
|
||||
|
||||
function AlertContextProvider({ children }) {
|
||||
const [alert, setAlert] = useState(null);
|
||||
|
||||
return (
|
||||
<>
|
||||
{alert && <Alert type="danger" message={alert} onDismiss={() => setAlert(null)} />}
|
||||
<AlertContext.Provider value={setAlert}>
|
||||
{ children }
|
||||
</AlertContext.Provider>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export { AlertContext, AlertContextProvider };
|
48
src/alertContext/__tests__/AlertContext.test.js
Normal file
48
src/alertContext/__tests__/AlertContext.test.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/)
|
||||
*
|
||||
* This is free software, licensed under the GNU General Public License v3.
|
||||
* See /LICENSE for more information.
|
||||
*/
|
||||
|
||||
import React, { useContext } from "react";
|
||||
import { render, getByText, queryByText, fireEvent } from "customTestRender";
|
||||
|
||||
import { AlertContext, AlertContextProvider } from "../AlertContext";
|
||||
|
||||
function AlertTest() {
|
||||
const setAlert = useContext(AlertContext);
|
||||
return <button onClick={() => setAlert("Alert content")}>Set alert</button>;
|
||||
};
|
||||
|
||||
describe("AlertContext", () => {
|
||||
let componentContainer;
|
||||
|
||||
beforeEach(() => {
|
||||
const { container } = render(
|
||||
<AlertContextProvider>
|
||||
<AlertTest />
|
||||
</AlertContextProvider>
|
||||
);
|
||||
componentContainer = container;
|
||||
});
|
||||
|
||||
it("should render component without alert", () => {
|
||||
expect(componentContainer).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it("should render alert", () => {
|
||||
fireEvent.click(getByText(componentContainer, "Set alert"));
|
||||
expect(componentContainer).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it("should dismiss alert", () => {
|
||||
fireEvent.click(getByText(componentContainer, "Set alert"));
|
||||
// Alert is present
|
||||
expect(getByText(componentContainer, "Alert content")).toBeDefined();
|
||||
|
||||
fireEvent.click(componentContainer.querySelector(".close"));
|
||||
// Alert is gone
|
||||
expect(queryByText(componentContainer, "Alert content")).toBeNull();
|
||||
});
|
||||
});
|
|
@ -0,0 +1,28 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`AlertContext should render alert 1`] = `
|
||||
<div>
|
||||
<div
|
||||
class="alert alert-dismissible alert-danger"
|
||||
>
|
||||
<button
|
||||
class="close"
|
||||
type="button"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
Alert content
|
||||
</div>
|
||||
<button>
|
||||
Set alert
|
||||
</button>
|
||||
</div>
|
||||
`;
|
||||
|
||||
exports[`AlertContext should render component without alert 1`] = `
|
||||
<div>
|
||||
<button>
|
||||
Set alert
|
||||
</button>
|
||||
</div>
|
||||
`;
|
|
@ -51,3 +51,6 @@ export {
|
|||
validateMAC,
|
||||
validateMultipleEmails,
|
||||
} from "validations";
|
||||
|
||||
// Alert context
|
||||
export { AlertContext, AlertContextProvider } from "alertContext/AlertContext";
|
||||
|
|
Loading…
Reference in New Issue
Block a user