Skip to content

Commit

Permalink
Port useful table tests over to modal tests
Browse files Browse the repository at this point in the history
These verify that we've wired up our table actions to our API calls. A
little brittle/tied to implementation, but I'd rather have them than
not.
  • Loading branch information
rylnd committed Jul 28, 2020
1 parent 15a7776 commit 6c24872
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,38 @@

import React from 'react';
import { mount } from 'enzyme';
import { act } from 'react-dom/test-utils';

import { getListResponseMock } from '../../../../../lists/common/schemas/response/list_schema.mock';
import { exportList, useDeleteList, useFindLists, ListSchema } from '../../../shared_imports';
import { TestProviders } from '../../../common/mock';
import { ValueListsModal } from './modal';

jest.mock('../../../shared_imports', () => {
const actual = jest.requireActual('../../../shared_imports');

return {
...actual,
exportList: jest.fn(),
useDeleteList: jest.fn(),
useFindLists: jest.fn(),
};
});

describe('ValueListsModal', () => {
beforeEach(() => {
// Do not resolve the export in tests as it causes unexpected state updates
(exportList as jest.Mock).mockImplementation(() => new Promise(() => {}));
(useFindLists as jest.Mock).mockReturnValue({
start: jest.fn(),
result: { data: Array<ListSchema>(3).fill(getListResponseMock()), total: 3 },
});
(useDeleteList as jest.Mock).mockReturnValue({
start: jest.fn(),
result: getListResponseMock(),
});
});

it('renders nothing if showModal is false', () => {
const container = mount(
<TestProviders>
Expand Down Expand Up @@ -47,15 +74,60 @@ describe('ValueListsModal', () => {
container.unmount();
});

it('renders ValueListsForm and ValueListsTable', () => {
it('renders ValueListsForm and an EuiTable', () => {
const container = mount(
<TestProviders>
<ValueListsModal showModal={true} onClose={jest.fn()} />
</TestProviders>
);

expect(container.find('ValueListsForm')).toHaveLength(1);
expect(container.find('ValueListsTable')).toHaveLength(1);
expect(container.find('EuiBasicTable')).toHaveLength(1);
container.unmount();
});

describe('modal table actions', () => {
it('calls exportList when export is clicked', () => {
const container = mount(
<TestProviders>
<ValueListsModal showModal={true} onClose={jest.fn()} />
</TestProviders>
);

act(() => {
container
.find('tbody tr')
.first()
.find('button[data-test-subj="action-export-value-list"]')
.simulate('click');
container.unmount();
});

expect(exportList).toHaveBeenCalledWith(expect.objectContaining({ listId: 'some-list-id' }));
});

it('calls deleteList when delete is clicked', () => {
const deleteListMock = jest.fn();
(useDeleteList as jest.Mock).mockReturnValue({
start: deleteListMock,
result: getListResponseMock(),
});
const container = mount(
<TestProviders>
<ValueListsModal showModal={true} onClose={jest.fn()} />
</TestProviders>
);

act(() => {
container
.find('tbody tr')
.first()
.find('button[data-test-subj="action-delete-value-list"]')
.simulate('click');
container.unmount();
});

expect(deleteListMock).toHaveBeenCalledWith(expect.objectContaining({ id: 'some-list-id' }));
});
});
});

This file was deleted.

This file was deleted.

0 comments on commit 6c24872

Please sign in to comment.