Skip to content

Commit

Permalink
Clean up jobs table tests
Browse files Browse the repository at this point in the history
* Removes deprecated waitForElement call
* Removes unnecessary mock context
* Uses waitFor with unsynchronized expectations
  * Now that our table has state changes due to the useMlLink hook, we
    need to synchronize our test with our assertions lest we see a bunch
    of console warnings about unexpected component updates.
  • Loading branch information
rylnd committed Sep 30, 2020
1 parent 4903e35 commit 57060e2
Showing 1 changed file with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,21 @@

import { shallow, mount } from 'enzyme';
import React from 'react';
import { render, waitForElement, waitFor } from '@testing-library/react';
import { render, waitFor } from '@testing-library/react';
import { JobsTableComponent } from './jobs_table';
import { mockSecurityJobs } from '../api.mock';
import { cloneDeep } from 'lodash/fp';
import { SecurityJob } from '../types';
import { createKibanaContextProviderMock } from '../../../lib/kibana/kibana_react.mock';

jest.mock('../../../lib/kibana');

const MockKibanaContextProvider = createKibanaContextProviderMock();

export async function getRenderedHref(Component: React.FC, selector: string) {
const el = render(
<MockKibanaContextProvider>
<Component />
</MockKibanaContextProvider>
);
const el = render(<Component />);

await waitForElement(() => el.container.querySelector(selector));
await waitFor(() => el.container.querySelector(selector));

const a = el.container.querySelector(selector);
return a ? a.getAttribute('href') : '';
return a?.getAttribute('href') ?? '';
}

describe('JobsTableComponent', () => {
Expand Down Expand Up @@ -61,7 +54,9 @@ describe('JobsTableComponent', () => {
),
'[data-test-subj="jobs-table-link"]'
);
expect(href).toEqual('/app/ml/jobs?mlManagement=(jobId:linux_anomalous_network_activity_ecs)');
await waitFor(() =>
expect(href).toEqual('/app/ml/jobs?mlManagement=(jobId:linux_anomalous_network_activity_ecs)')
);
});

test('should render the hyperlink with URI encodings which points specifically to the job id', async () => {
Expand All @@ -76,7 +71,9 @@ describe('JobsTableComponent', () => {
),
'[data-test-subj="jobs-table-link"]'
);
expect(href).toEqual("/app/ml/jobs?mlManagement=(jobId:'job%20id%20with%20spaces')");
await waitFor(() =>
expect(href).toEqual("/app/ml/jobs?mlManagement=(jobId:'job%20id%20with%20spaces')")
);
});

test('should call onJobStateChange when the switch is clicked to be true/open', async () => {
Expand All @@ -99,25 +96,29 @@ describe('JobsTableComponent', () => {
});
});

test('should have a switch when it is not in the loading state', () => {
test('should have a switch when it is not in the loading state', async () => {
const wrapper = mount(
<JobsTableComponent
isLoading={false}
jobs={securityJobs}
onJobStateChange={onJobStateChangeMock}
/>
);
expect(wrapper.find('[data-test-subj="job-switch"]').exists()).toBe(true);
await waitFor(() => {
expect(wrapper.find('[data-test-subj="job-switch"]').exists()).toBe(true);
});
});

test('should not have a switch when it is in the loading state', () => {
test('should not have a switch when it is in the loading state', async () => {
const wrapper = mount(
<JobsTableComponent
isLoading={true}
jobs={securityJobs}
onJobStateChange={onJobStateChangeMock}
/>
);
expect(wrapper.find('[data-test-subj="job-switch"]').exists()).toBe(false);
await waitFor(() => {
expect(wrapper.find('[data-test-subj="job-switch"]').exists()).toBe(false);
});
});
});

0 comments on commit 57060e2

Please sign in to comment.