RedHatInsights / ocp-advisor-frontend

Insights for OpenShift: OCP Advisor frontend component
https://console.redhat.com/openshift/insights/advisor
Apache License 2.0
3 stars 23 forks source link

Fix missing await and add tests #757

Closed ikerreyes closed 4 months ago

ikerreyes commented 4 months ago

The modal to disable clusters was not waiting for an async operation to complete. Thus, follow up actions (getting the fresh data from backend) were triggered before all changes were submitted. As such,the view was not consistent.

Test for this have been added.

Fixes RHINENG-10897

The test added here require a revisit, as the implementation is not the best one. I have tried something more elaborated, but did not work as expected. Advice will be appreciated.


describe.only('call order checking', () => {
  it('single host', () => {
    let number_of_calls = 0;
    let calls_so_far = 0;

    cy.mount(
      <MemoryRouter>
        <Intl>
          <Provider store={getStore()}>
            <DisableRule
              isModalOpen={true}
              rule={{ rule_id: 'foo|BAR', disabled: false }}
              host={'7795cbcd-0353-4e59-b920-fc1c39a27014'}
              afterFn={() => {
                // trick, as an expect here does not raise an error in the tests
                calls_so_far = number_of_calls;
                // expect(number_of_calls).to.equal(1);
              }}
            />
          </Provider>
        </Intl>
      </MemoryRouter>
    );

    cy.intercept(
      'PUT',
      '/api/insights-results-aggregator/v1/clusters/**/rules/**/error_key/**/disable',
      (req) => {
        req.on('after:response', () => {
          number_of_calls++;
        });
        req.reply({
          statusCode: 200,
          delay: 2 * 1000, // 2 secs
        });
      }
    ).as('disableRequest');

    cy.ouiaId(SAVE_BUTTON).click();
    cy.wait('@disableRequest').then(() => {
     expect(calls_so_far).to.equal(1);
    });
  });

  it('multiple hosts', () => {
    let number_of_calls = 0;
    let calls_so_far = 0;

    // beforeEach(() => {
    cy.mount(
      <MemoryRouter>
        <Intl>
          <Provider store={getStore()}>
            <DisableRule
              isModalOpen={true}
              rule={{ rule_id: 'foo|BAR', disabled: false }}
              hosts={[
                {
                  id: '084ac7a7-1c7d-49ff-b56e-f94881da242d',
                },
                {
                  id: '7795cbcd-0353-4e59-b920-fc1c39a27014',
                },
              ]}
              afterFn={() => {
                // trick, as an expect here does not raise an error in the tests
                calls_so_far = number_of_calls;
                // expect(number_of_calls).to.equal(2);
              }}
            />
          </Provider>
        </Intl>
      </MemoryRouter>
    );

    cy.intercept(
      'PUT',
      '/api/insights-results-aggregator/v1/clusters/**/rules/**/error_key/**/disable',
      (req) => {
        req.on('after:response', () => {
          number_of_calls++;
        });
        req.reply({
          statusCode: 200,
          delay: 2 * 1000, // 2 secs
        });
      }
    ).as('disableRequest');

    cy.ouiaId(SAVE_BUTTON).click();
    cy.wait(['@disableRequest', '@disableRequest']).then(() => {
      expect(calls_so_far).to.equal(2);
    });
  });

  it('no hosts', () => {
    let number_of_calls = 0;
    let calls_so_far = 0;

    cy.mount(
      <MemoryRouter>
        <Intl>
          <Provider store={getStore()}>
            <DisableRule
              isModalOpen={true}
              rule={{ rule_id: 'abc', disabled: false }}
              afterFn={() => {
                // trick, as an expect here does not raise an error in the tests
                calls_so_far = number_of_calls;
                // expect(number_of_calls).to.equal(1);
              }}
            />
          </Provider>
        </Intl>
      </MemoryRouter>
    );

    cy.intercept('POST', '/api/insights-results-aggregator/v2/ack', (req) => {
      req.on('after:response', () => {
        number_of_calls++;
      });
      req.reply({
        statusCode: 200,
        delay: 2 * 1000, // 2 secs
      });
    }).as('ackRequest');

    cy.ouiaId(SAVE_BUTTON).click();
    cy.wait('@ackRequest').then(() => {
      expect(calls_so_far).to.equal(1);
    });
  });
});```
Fewwy commented 4 months ago

/retest

codecov-commenter commented 4 months ago

Codecov Report

Attention: Patch coverage is 3.84615% with 25 lines in your changes missing coverage. Please review.

Project coverage is 7.69%. Comparing base (d6fd289) to head (014f683).

Files Patch % Lines
src/Components/Modals/DisableRule.cy.js 0.00% 25 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #757 +/- ## ========================================= - Coverage 7.74% 7.69% -0.05% ========================================= Files 68 68 Lines 3860 3885 +25 Branches 837 837 ========================================= Hits 299 299 - Misses 3561 3586 +25 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

ikerreyes commented 4 months ago

/retest

gkarat commented 4 months ago

:tada: This PR is included in version 1.43.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: