forked from opensearch-project/dashboards-observability
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for notebooks (opensearch-project#277)
* 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
1 parent
f737a9b
commit ba52143
Showing
15 changed files
with
2,993 additions
and
5 deletions.
There are no files selected for viewing
1,139 changes: 1,139 additions & 0 deletions
1,139
public/components/notebooks/components/__tests__/__snapshots__/note_table.test.tsx.snap
Large diffs are not rendered by default.
Oops, something went wrong.
611 changes: 611 additions & 0 deletions
611
public/components/notebooks/components/__tests__/__snapshots__/notebook.test.tsx.snap
Large diffs are not rendered by default.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
public/components/notebooks/components/__tests__/note_table.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
106
public/components/notebooks/components/__tests__/notebook.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.