test.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from "react";
  15. import { shallow } from "enzyme";
  16. import sinon from "sinon";
  17. import moment from "moment";
  18. import TW from "@src/utils/TestWrapper";
  19. import EndpointDetailsContent from ".";
  20. import configLoader from "@src/utils/Config";
  21. const wrap = props =>
  22. new TW(
  23. shallow(
  24. <EndpointDetailsContent
  25. usage={{ replicas: [], migrations: [] }}
  26. {...props}
  27. />
  28. ),
  29. "edContent"
  30. );
  31. const item = {
  32. name: "endpoint_name",
  33. type: "openstack",
  34. description: "endpoint_description",
  35. created_at: new Date(2017, 10, 24, 13, 56),
  36. };
  37. const connectionInfo = {
  38. username: "username",
  39. password: "password123",
  40. details: "other details",
  41. boolean_true: true,
  42. boolean_false: false,
  43. nested: {
  44. nested_1: "nested_first",
  45. nested_2: "nested_second",
  46. },
  47. };
  48. describe("EndpointDetailsContent Component", () => {
  49. beforeAll(() => {
  50. configLoader.config = { passwordFields: [] };
  51. });
  52. it("renders endpoint details", () => {
  53. const wrapper = wrap({ item });
  54. expect(wrapper.find("name").prop("value")).toBe(item.name);
  55. expect(wrapper.find("description").prop("value")).toBe(item.description);
  56. expect(wrapper.find("created").prop("value")).toBe(
  57. moment(item.created_at)
  58. .add(-new Date().getTimezoneOffset(), "minutes")
  59. .format("DD/MM/YYYY HH:mm")
  60. );
  61. expect(wrapper.find("connLoading").length).toBe(0);
  62. });
  63. it("renders connection info loading", () => {
  64. const wrapper = wrap({ item, loading: true });
  65. expect(wrapper.find("name").prop("value")).toBe(item.name);
  66. expect(wrapper.find("connLoading").length).toBe(1);
  67. });
  68. it("renders simple connection info", () => {
  69. const wrapper = wrap({
  70. item,
  71. connectionInfo,
  72. passwordFields: ["password"],
  73. });
  74. expect(wrapper.find("connValue-username").prop("value")).toBe(
  75. connectionInfo.username
  76. );
  77. expect(wrapper.find("connPassword").prop("value")).toBe(
  78. connectionInfo.password
  79. );
  80. expect(wrapper.find("connValue-details").prop("value")).toBe(
  81. connectionInfo.details
  82. );
  83. });
  84. it("renders boolean as Yes and No", () => {
  85. const wrapper = wrap({ item, connectionInfo });
  86. expect(wrapper.find("connValue-boolean_true").prop("value")).toBe("Yes");
  87. expect(wrapper.find("connValue-boolean_false").prop("value")).toBe("No");
  88. });
  89. it("renders nested connection info", () => {
  90. const wrapper = wrap({ item, connectionInfo });
  91. expect(wrapper.find("connValue-nested_1").prop("value")).toBe(
  92. connectionInfo.nested.nested_1
  93. );
  94. expect(wrapper.find("connValue-nested_2").prop("value")).toBe(
  95. connectionInfo.nested.nested_2
  96. );
  97. });
  98. it("dispatches buttons clicks", () => {
  99. const onDeleteClick = sinon.spy();
  100. const onValidateClick = sinon.spy();
  101. const wrapper = wrap({ item, onDeleteClick, onValidateClick });
  102. wrapper.find("validateButton").click();
  103. wrapper.find("deleteButton").click();
  104. expect(onValidateClick.calledOnce).toBe(true);
  105. expect(onDeleteClick.calledOnce).toBe(true);
  106. });
  107. });