Skip to content

Commit

Permalink
Add unit tests for notebooks (opensearch-project#277)
Browse files Browse the repository at this point in the history
* WIP add unit tests to notebooks

Signed-off-by: Joshua Li <[email protected]>

* WIP add unit tests to notebooks

Signed-off-by: Joshua Li <[email protected]>

* WIP

Signed-off-by: Joshua Li <[email protected]>

* WIP

Signed-off-by: Joshua Li <[email protected]>

* Update snapshots

Signed-off-by: Joshua Li <[email protected]>
  • Loading branch information
joshuali925 authored Nov 24, 2021
1 parent f737a9b commit ba52143
Show file tree
Hide file tree
Showing 15 changed files with 2,993 additions and 5 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { fireEvent, render } from '@testing-library/react';
import { configure, mount, shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import React from 'react';
import { NoteTable } from '../note_table';

describe('<NoteTable /> spec', () => {
configure({ adapter: new Adapter() });

it('renders the empty component', () => {
const fetchNotebooks = jest.fn();
const addSampleNotebooks = jest.fn();
const createNotebook = jest.fn();
const renameNotebook = jest.fn();
const cloneNotebook = jest.fn();
const deleteNotebook = jest.fn();
const setBreadcrumbs = jest.fn();
const setToast = jest.fn();
const utils = render(
<NoteTable
loading={false}
fetchNotebooks={fetchNotebooks}
addSampleNotebooks={addSampleNotebooks}
notebooks={[]}
createNotebook={createNotebook}
renameNotebook={renameNotebook}
cloneNotebook={cloneNotebook}
deleteNotebook={deleteNotebook}
parentBreadcrumb={{ href: 'parent-href', text: 'parent-text' }}
setBreadcrumbs={setBreadcrumbs}
setToast={setToast}
/>
);
expect(utils.container.firstChild).toMatchSnapshot();
});

it('renders the component', () => {
const fetchNotebooks = jest.fn();
const addSampleNotebooks = jest.fn();
const createNotebook = jest.fn();
const renameNotebook = jest.fn();
const cloneNotebook = jest.fn();
const deleteNotebook = jest.fn();
const setBreadcrumbs = jest.fn();
const setToast = jest.fn();
const notebooks = Array.from({ length: 5 }, (v, k) => ({
path: `path-${k}`,
id: `id-${k}`,
dateCreated: 'date-created',
dateModified: 'date-modified',
}));
const utils = render(
<NoteTable
loading={false}
fetchNotebooks={fetchNotebooks}
addSampleNotebooks={addSampleNotebooks}
notebooks={notebooks}
createNotebook={createNotebook}
renameNotebook={renameNotebook}
cloneNotebook={cloneNotebook}
deleteNotebook={deleteNotebook}
parentBreadcrumb={{ href: 'parent-href', text: 'parent-text' }}
setBreadcrumbs={setBreadcrumbs}
setToast={setToast}
/>
);
expect(utils.container.firstChild).toMatchSnapshot();

utils.getByText('Actions').click();
utils.getByText('Add samples').click();
utils.getAllByLabelText('Select this row')[0].click();
utils.getByText('Actions').click();
utils.getByText('Delete').click();
utils.getByText('Cancel').click();
utils.getAllByLabelText('Select this row')[0].click();
utils.getByText('Actions').click();
utils.getByText('Rename').click();
});
});
106 changes: 106 additions & 0 deletions public/components/notebooks/components/__tests__/notebook.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { fireEvent, render, waitFor } from '@testing-library/react';
import { configure, mount, shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import React from 'react';
import { HttpResponse } from '../../../../../../../src/core/public';
import httpClientMock from '../../../../../test/__mocks__/httpClientMock';
import { sampleNotebook1 } from '../helpers/__tests__/sampleDefaultNotebooks';
import { Notebook } from '../notebook';

jest.mock('../../../../../../../src/plugins/embeddable/public', () => ({
ViewMode: {
EDIT: 'edit',
VIEW: 'view',
},
}));

// @ts-ignore
global.fetch = jest.fn(() =>
Promise.resolve({
json: () =>
Promise.resolve({
status: {
statuses: [{ id: 'plugin:reportsDashboards' }],
},
}),
})
);

describe('<Notebook /> spec', () => {
configure({ adapter: new Adapter() });

it('renders the empty component', () => {
const setBreadcrumbs = jest.fn();
const renameNotebook = jest.fn();
const cloneNotebook = jest.fn();
const deleteNotebook = jest.fn();
const setToast = jest.fn();
const location = jest.fn();
const history = jest.fn() as any;
history.replace = jest.fn();
const utils = render(
<Notebook
openedNoteId="mock-id"
DashboardContainerByValueRenderer={jest.fn()}
http={httpClientMock}
parentBreadcrumb={{ href: 'parent-href', text: 'parent-text' }}
setBreadcrumbs={setBreadcrumbs}
renameNotebook={renameNotebook}
cloneNotebook={cloneNotebook}
deleteNotebook={deleteNotebook}
setToast={setToast}
location={location}
history={history}
/>
);
expect(utils.container.firstChild).toMatchSnapshot();

utils.getByText("Add code block").click();
utils.getByText("Add visualization").click();
});

it('renders the component', async () => {
const setBreadcrumbs = jest.fn();
const renameNotebook = jest.fn();
const cloneNotebook = jest.fn();
const deleteNotebook = jest.fn();
const setToast = jest.fn();
const location = jest.fn();
const history = jest.fn() as any;
history.replace = jest.fn();
httpClientMock.get = jest.fn(() =>
Promise.resolve(({
...sampleNotebook1,
path: sampleNotebook1.name,
savedVisualizations: Array.from({ length: 5 }, (v, k) => ({
label: `vis-${k}`,
key: `vis-${k}`,
})),
} as unknown) as HttpResponse)
);
const utils = render(
<Notebook
openedNoteId={sampleNotebook1.id}
DashboardContainerByValueRenderer={jest.fn()}
http={httpClientMock}
parentBreadcrumb={{ href: 'parent-href', text: 'parent-text' }}
setBreadcrumbs={setBreadcrumbs}
renameNotebook={renameNotebook}
cloneNotebook={cloneNotebook}
deleteNotebook={deleteNotebook}
setToast={setToast}
location={location}
history={history}
/>
);

await waitFor(() => {
expect(utils.container.firstChild).toMatchSnapshot();
});
});
});
Loading

0 comments on commit ba52143

Please sign in to comment.