DashboardLicence.spec.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Copyright (C) 2023 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 { DateTime } from "luxon";
  15. import React from "react";
  16. import { Licence, LicenceServerStatus } from "@src/@types/Licence";
  17. import { render } from "@testing-library/react";
  18. import TestUtils from "@tests/TestUtils";
  19. import DashboardLicence from "./DashboardLicence";
  20. describe("DashboardLicence", () => {
  21. const futureLicence: Licence = {
  22. applianceId: "test-id",
  23. earliestLicenceExpiryDate: DateTime.now().plus({ years: 1 }).toJSDate(),
  24. latestLicenceExpiryDate: DateTime.now().plus({ years: 1 }).toJSDate(),
  25. currentPerformedReplicas: 5,
  26. currentPerformedMigrations: 3,
  27. lifetimePerformedMigrations: 4,
  28. lifetimePerformedReplicas: 6,
  29. currentAvailableReplicas: 10,
  30. currentAvailableMigrations: 5,
  31. lifetimeAvailableReplicas: 15,
  32. lifetimeAvailableMigrations: 10,
  33. };
  34. const status: LicenceServerStatus = {
  35. hostname: "test-hostname",
  36. multi_appliance: false,
  37. supported_licence_versions: ["v2"],
  38. server_local_time: DateTime.now().toISO()!,
  39. };
  40. let defaultProps: DashboardLicence["props"];
  41. beforeEach(() => {
  42. defaultProps = {
  43. licence: futureLicence,
  44. licenceServerStatus: status,
  45. licenceError: null,
  46. loading: false,
  47. onAddClick: jest.fn(),
  48. };
  49. });
  50. it("renders licence status text when licence and licenceServerStatus are provided and the licence has not expired", () => {
  51. render(<DashboardLicence {...defaultProps} />);
  52. const futureDate = DateTime.now().plus({ years: 1 });
  53. expect(
  54. TestUtils.select("DashboardLicence__TopInfoDateTop-")?.textContent,
  55. ).toBe(`${futureDate.toFormat("LLL")} '${futureDate.toFormat("yy")}`);
  56. expect(
  57. TestUtils.select("DashboardLicence__TopInfoDateBottom-")?.textContent,
  58. ).toBe(futureDate.toFormat("dd"));
  59. expect(
  60. TestUtils.selectAll("DashboardLicence__ChartHeaderCurrent-")[0]
  61. .textContent,
  62. ).toBe("5 Used Replicas ");
  63. expect(
  64. TestUtils.selectAll("DashboardLicence__ChartHeaderTotal-")[0].textContent,
  65. ).toBe("Total 10");
  66. expect(
  67. TestUtils.selectAll("DashboardLicence__ChartHeaderCurrent-")[1]
  68. .textContent,
  69. ).toBe("3 Used Migrations ");
  70. expect(
  71. TestUtils.selectAll("DashboardLicence__ChartHeaderTotal-")[1].textContent,
  72. ).toBe("Total 5");
  73. });
  74. it("renders licence error when licenceError prop is provided and there's no licence", () => {
  75. const newProps = {
  76. ...defaultProps,
  77. licence: null,
  78. licenceError: "An error occurred.",
  79. };
  80. render(<DashboardLicence {...newProps} />);
  81. expect(TestUtils.select("DashboardLicence__LicenceError-")).toBeTruthy();
  82. expect(
  83. TestUtils.select("DashboardLicence__LicenceError-")?.textContent,
  84. ).toBe("An error occurred.");
  85. });
  86. it("renders expired licence details when licence has expired", () => {
  87. const newProps = {
  88. ...defaultProps,
  89. licence: {
  90. ...futureLicence,
  91. earliestLicenceExpiryDate: DateTime.now().minus({ days: 2 }).toJSDate(),
  92. },
  93. };
  94. render(<DashboardLicence {...newProps} />);
  95. expect(TestUtils.select("DashboardLicence__LicenceError-")).toBeTruthy();
  96. expect(
  97. TestUtils.select("DashboardLicence__LicenceError-")?.textContent,
  98. ).toContain(
  99. "Please contact your Coriolis representative with the Appliance ID",
  100. );
  101. expect(
  102. TestUtils.select("DashboardLicence__ApplianceId-")?.textContent,
  103. ).toBe("Appliance ID:test-id-licencev2");
  104. expect(TestUtils.select("Button__")?.textContent).toBe("Add Licence");
  105. });
  106. it("renders loading status when loading prop is true and there's no licence", () => {
  107. const newProps = {
  108. ...defaultProps,
  109. licence: null,
  110. loading: true,
  111. };
  112. render(<DashboardLicence {...newProps} />);
  113. expect(TestUtils.select("StatusImage__Wrapper-")).toBeTruthy();
  114. });
  115. it("does not render anything when no props are provided", () => {
  116. const newProps = {
  117. ...defaultProps,
  118. licence: null,
  119. };
  120. render(<DashboardLicence {...newProps} />);
  121. expect(document.body.innerHTML).toBe("<div></div>");
  122. });
  123. it("hides licenceLogoRef if buttonWrapperRef width is less than 370", async () => {
  124. const newProps = {
  125. ...defaultProps,
  126. licence: {
  127. ...futureLicence,
  128. earliestLicenceExpiryDate: DateTime.now().minus({ days: 2 }).toJSDate(),
  129. },
  130. };
  131. render(<DashboardLicence {...newProps} />);
  132. const button = TestUtils.select(
  133. "DashboardLicence__AddLicenceButtonWrapper",
  134. );
  135. const logo = TestUtils.select("DashboardLicence__Logo");
  136. button!.getBoundingClientRect = jest.fn().mockReturnValue({ width: 400 });
  137. window.dispatchEvent(new Event("resize"));
  138. expect(button).toBeTruthy();
  139. expect(logo).toBeTruthy();
  140. expect(logo!.style.display).toBe("block");
  141. button!.getBoundingClientRect = jest.fn().mockReturnValue({ width: 360 });
  142. window.dispatchEvent(new Event("resize"));
  143. expect(logo!.style.display).toBe("none");
  144. });
  145. it("renders singular label when current is 1", () => {
  146. const newProps = {
  147. ...defaultProps,
  148. licence: {
  149. ...futureLicence,
  150. currentPerformedReplicas: 1,
  151. },
  152. };
  153. render(<DashboardLicence {...newProps} />);
  154. expect(
  155. TestUtils.selectAll("DashboardLicence__ChartHeaderCurrent-")[0]
  156. .textContent,
  157. ).toBe("1 Used Replica ");
  158. });
  159. });