]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20254 Migrate PluginRiskConsent test to RTL
authorJeremy Davis <jeremy.davis@sonarsource.com>
Wed, 30 Aug 2023 09:20:49 +0000 (11:20 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 30 Aug 2023 20:03:07 +0000 (20:03 +0000)
server/sonar-web/src/main/js/app/components/__tests__/PluginRiskConsent-test.tsx
server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/PluginRiskConsent-test.tsx.snap [deleted file]

index b451345e101950e31e10e8108412dc4ed0ddcb32..4d5a81df5a45241523cb3f409ebdbb80a08a2c12 100644 (file)
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
-import { shallow } from 'enzyme';
+import userEvent from '@testing-library/user-event';
 import * as React from 'react';
 import { setSimpleSettingValue } from '../../../api/settings';
-import { Button } from '../../../components/controls/buttons';
 import { mockLoggedInUser, mockRouter } from '../../../helpers/testMocks';
+import { renderComponent } from '../../../helpers/testReactTestingUtils';
+import { byRole } from '../../../helpers/testSelector';
 import { PluginRiskConsent, PluginRiskConsentProps } from '../PluginRiskConsent';
 
 jest.mock('../../../api/settings', () => ({
@@ -35,36 +36,57 @@ jest.mock('react', () => {
   };
 });
 
+const originalLocation = window.location;
+
+beforeAll(() => {
+  let href = '';
+  const location = {
+    ...window.location,
+    get href() {
+      return href;
+    },
+    set href(v: string) {
+      href = v;
+    },
+  };
+  Object.defineProperty(window, 'location', {
+    writable: true,
+    value: location,
+  });
+});
+
 afterAll(() => {
   jest.clearAllMocks();
-});
 
-it('should render correctly', () => {
-  expect(shallowRender()).toMatchSnapshot('default');
+  Object.defineProperty(window, 'location', {
+    writable: true,
+    value: originalLocation,
+  });
 });
 
 it('should redirect non-admin users', () => {
   const replace = jest.fn();
-  const wrapper = shallowRender({
+  renderPluginRiskConsent({
     currentUser: mockLoggedInUser(),
     router: mockRouter({ replace }),
   });
-  expect(wrapper.type()).toBeNull();
   expect(replace).toHaveBeenCalled();
 });
 
 it('should handle acknowledgement and redirect', async () => {
-  const wrapper = shallowRender();
+  const user = userEvent.setup();
+  renderPluginRiskConsent();
 
-  wrapper.find(Button).first().simulate('click');
+  await user.click(ui.acknowledgeButton.get());
 
   await new Promise(setImmediate);
 
   expect(setSimpleSettingValue).toHaveBeenCalled();
+  expect(window.location.href).toBe('/');
 });
 
-function shallowRender(props: Partial<PluginRiskConsentProps> = {}) {
-  return shallow(
+function renderPluginRiskConsent(props: Partial<PluginRiskConsentProps> = {}) {
+  return renderComponent(
     <PluginRiskConsent
       currentUser={mockLoggedInUser({ permissions: { global: ['admin'] } })}
       router={mockRouter()}
@@ -72,3 +94,7 @@ function shallowRender(props: Partial<PluginRiskConsentProps> = {}) {
     />
   );
 }
+
+const ui = {
+  acknowledgeButton: byRole('button', { name: 'plugin_risk_consent.action' }),
+};
diff --git a/server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/PluginRiskConsent-test.tsx.snap b/server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/PluginRiskConsent-test.tsx.snap
deleted file mode 100644 (file)
index ca59e6d..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-// Jest Snapshot v1, https://goo.gl/fbAQLP
-
-exports[`should render correctly: default 1`] = `
-<div
-  className="plugin-risk-consent-page"
->
-  <Helmet
-    defer={false}
-    encodeSpecialCharacters={true}
-    prioritizeSeoTags={false}
-    title="plugin_risk_consent.page"
-  />
-  <div
-    className="plugin-risk-consent-content boxed-group"
-  >
-    <div
-      className="boxed-group-inner text-center"
-    >
-      <h1
-        className="big-spacer-bottom"
-      >
-        plugin_risk_consent.title
-      </h1>
-      <p
-        className="big big-spacer-bottom"
-      >
-        plugin_risk_consent.description
-      </p>
-      <p
-        className="big huge-spacer-bottom"
-      >
-        plugin_risk_consent.description2
-      </p>
-      <Button
-        className="big-spacer-bottom"
-        onClick={[Function]}
-      >
-        plugin_risk_consent.action
-      </Button>
-    </div>
-  </div>
-</div>
-`;