From ab6455d48b033eba2bf02a3b65abea76444eeb72 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Mon, 15 Apr 2019 20:08:46 -0700 Subject: [PATCH] Allow an Alert to be non-dismissible and use it in DomainList Also improved DomainList tests. Signed-off-by: Mcat12 --- src/components/common/Alert.tsx | 22 +++++++++++-------- src/components/list/DomainList.tsx | 9 +++++--- .../list/__tests__/DomainList.test.tsx | 16 ++++++-------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/components/common/Alert.tsx b/src/components/common/Alert.tsx index a36f131..414252e 100644 --- a/src/components/common/Alert.tsx +++ b/src/components/common/Alert.tsx @@ -8,7 +8,7 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -import React, { FunctionComponent } from "react"; +import React from "react"; export type AlertType = "info" | "success" | "danger"; @@ -16,23 +16,27 @@ export interface AlertProps { type: AlertType; onClick: () => void; message: string; + dismissible: boolean; } -const Alert: FunctionComponent = (props: AlertProps) => { +const Alert = (props: AlertProps) => { + const dismissClass = props.dismissible ? "alert-dismissible" : ""; + return ( -
- +
+ {props.dismissible ? ( + + ) : null} {props.message}
); }; Alert.defaultProps = { - onClick: () => {} + onClick: () => {}, + dismissible: true }; export default Alert; diff --git a/src/components/list/DomainList.tsx b/src/components/list/DomainList.tsx index ab9e4cf..4863803 100644 --- a/src/components/list/DomainList.tsx +++ b/src/components/list/DomainList.tsx @@ -12,6 +12,7 @@ import React from "react"; import { WithNamespaces, withNamespaces } from "react-i18next"; import api from "../../util/api"; import { Button } from "reactstrap"; +import Alert from "../common/Alert"; export interface DomainListProps extends WithNamespaces { domains: string[]; @@ -54,9 +55,11 @@ const DomainList = ({ domains, onRemove, t }: DomainListProps) => { body = domains.map(mapDomainsToListItems); } else { body = ( -
- {t("There are no domains in this list")} -
+ ); } diff --git a/src/components/list/__tests__/DomainList.test.tsx b/src/components/list/__tests__/DomainList.test.tsx index eb9b828..c66681c 100644 --- a/src/components/list/__tests__/DomainList.test.tsx +++ b/src/components/list/__tests__/DomainList.test.tsx @@ -12,6 +12,7 @@ import React from "react"; import { shallow } from "enzyme"; import DomainList from "../DomainList"; import api from "../../../util/api"; +import Alert from "../../common/Alert"; const domains = ["domain1.com", "domain2.com", "domain3.com"]; @@ -26,9 +27,11 @@ it("shows a list of domains", () => { it("shows an alert if there are no domains", () => { const wrapper = shallow(); - expect(wrapper.find("li")).toHaveLength(0); - expect(wrapper.find("ul").childAt(0)).toHaveClassName("alert-info"); - expect(wrapper).toIncludeText("There are no domains in this list"); + expect(wrapper.find("li")).not.toExist(); + expect(wrapper.find(Alert)).toExist(); + expect(wrapper.find(Alert).props().message).toEqual( + "There are no domains in this list" + ); }); it("does not have a delete button when not logged in", () => { @@ -36,12 +39,7 @@ it("does not have a delete button when not logged in", () => { ); - expect( - wrapper - .find("ul") - .childAt(0) - .find("button") - ).not.toExist(); + expect(wrapper.find("Button")).not.toExist(); }); it("has a delete button when logged in", () => {