-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fddc77
commit 637e4a3
Showing
6 changed files
with
184 additions
and
26 deletions.
There are no files selected for viewing
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
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
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
89 changes: 89 additions & 0 deletions
89
src/library-authoring/add-content/AddContentWorkflow.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,89 @@ | ||
/** | ||
* Test the whole workflow of adding content, editing it, saving it | ||
*/ | ||
import { snakeCaseObject } from '@edx/frontend-platform'; | ||
import { | ||
fireEvent, | ||
render, | ||
waitFor, | ||
screen, | ||
initializeMocks, | ||
} from '../../testUtils'; | ||
import mockResult from '../__mocks__/library-search.json'; | ||
import editorCmsApi from '../../editors/data/services/cms/api'; | ||
import * as textEditorHooks from '../../editors/containers/TextEditor/hooks'; | ||
import { | ||
mockContentLibrary, | ||
mockCreateLibraryBlock, | ||
mockLibraryBlockTypes, | ||
mockXBlockFields, | ||
} from '../data/api.mocks'; | ||
import { mockBroadcastChannel, mockClipboardEmpty } from '../../generic/data/api.mock'; | ||
import { mockContentSearchConfig, mockSearchResult } from '../../search-manager/data/api.mock'; | ||
import LibraryLayout from '../LibraryLayout'; | ||
|
||
mockContentSearchConfig.applyMock(); | ||
mockLibraryBlockTypes.applyMock(); | ||
mockClipboardEmpty.applyMock(); | ||
mockBroadcastChannel(); | ||
mockContentLibrary.applyMock(); | ||
mockCreateLibraryBlock.applyMock(); | ||
mockSearchResult(mockResult); | ||
// Mocking the redux APIs in the src/editors/ folder is a bit more involved: | ||
jest.spyOn(editorCmsApi as any, 'fetchBlockById').mockImplementation( | ||
async (args: { blockId: string }) => ( | ||
{ status: 200, data: snakeCaseObject(await mockXBlockFields(args.blockId)) } | ||
), | ||
); | ||
jest.spyOn(textEditorHooks, 'getContent').mockImplementation(() => () => '<p>Edited HTML content</p>'); | ||
jest.mock('frontend-components-tinymce-advanced-plugins', () => ({ a11ycheckerCss: '' })); | ||
|
||
const { libraryId } = mockContentLibrary; | ||
const renderOpts = { | ||
// Mount the <LibraryLayout /> on this route, to simulate how it's mounted in the real app: | ||
path: '/library/:libraryId/*', | ||
// And set the current URL to the following: | ||
routerProps: { initialEntries: [`/library/${libraryId}/components`] }, | ||
}; | ||
|
||
describe('AddContentWorkflow test', () => { | ||
beforeEach(() => { | ||
initializeMocks(); | ||
}); | ||
|
||
it('can create an HTML component', async () => { | ||
render(<LibraryLayout />, renderOpts); | ||
|
||
// Click "New [Component]" | ||
const newComponentButton = await screen.findByRole('button', { name: /New/ }); | ||
fireEvent.click(newComponentButton); | ||
|
||
// Click "Text" to create a text component | ||
fireEvent.click(await screen.findByRole('button', { name: /Text/ })); | ||
|
||
// Then the editor should open | ||
expect(await screen.findByRole('heading', { name: /New Text Component/ })).toBeInTheDocument(); | ||
|
||
// Edit the title | ||
fireEvent.click(screen.getByRole('button', { name: /Edit Title/ })); | ||
const titleInput = screen.getByPlaceholderText('Title'); | ||
fireEvent.change(titleInput, { target: { value: 'A customized title' } }); | ||
fireEvent.blur(titleInput); | ||
await waitFor(() => expect(screen.queryByRole('heading', { name: /New Text Component/ })).not.toBeInTheDocument()); | ||
expect(screen.getByRole('heading', { name: /A customized title/ })); | ||
|
||
// Note that TinyMCE doesn't really load properly in our test environment | ||
// so we can't really edit the text, but we have getContent() mocked to simulate | ||
// using TinyMCE to enter some new HTML. | ||
|
||
// Mock the save() REST API method: | ||
const saveSpy = jest.spyOn(editorCmsApi as any, 'saveBlock').mockImplementationOnce(async () => ({ | ||
status: 200, data: { id: mockXBlockFields.usageKeyNewHtml }, | ||
})); | ||
|
||
// Click Save | ||
const saveButton = screen.getByLabelText('Save changes and return to learning context'); | ||
fireEvent.click(saveButton); | ||
expect(saveSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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
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,42 @@ | ||
/* istanbul ignore file */ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import fetchMock from 'fetch-mock-jest'; | ||
import type { MultiSearchResponse } from 'meilisearch'; | ||
import * as api from './api'; | ||
|
||
/** | ||
* Mock getContentSearchConfig() | ||
*/ | ||
export async function mockContentSearchConfig(): ReturnType<typeof api.getContentSearchConfig> { | ||
return { | ||
url: 'http://mock.meilisearch.local', | ||
indexName: 'studio', | ||
apiKey: 'test-key', | ||
}; | ||
} | ||
mockContentSearchConfig.searchEndpointUrl = 'http://mock.meilisearch.local/multi-search'; | ||
mockContentSearchConfig.applyMock = () => ( | ||
jest.spyOn(api, 'getContentSearchConfig').mockImplementation(mockContentSearchConfig) | ||
); | ||
|
||
/** | ||
* Mock all future Meilisearch searches with the given response. | ||
* | ||
* For a given test suite, this mock will stay in effect until you call it with | ||
* a different mock response, or you call `fetchMock.mockReset()` | ||
*/ | ||
export function mockSearchResult(mockResponse: MultiSearchResponse) { | ||
fetchMock.post(mockContentSearchConfig.searchEndpointUrl, (_url, req) => { | ||
const requestData = JSON.parse(req.body?.toString() ?? ''); | ||
const query = requestData?.queries[0]?.q ?? ''; | ||
// We have to replace the query (search keywords) in the mock results with the actual query, | ||
// because otherwise Instantsearch will update the UI and change the query, | ||
// leading to unexpected results in the test cases. | ||
const newMockResponse = { ...mockResponse }; | ||
newMockResponse.results[0].query = query; | ||
// And fake the required '_formatted' fields; it contains the highlighting <mark>...</mark> around matched words | ||
// eslint-disable-next-line no-underscore-dangle, no-param-reassign | ||
mockResponse.results[0]?.hits.forEach((hit) => { hit._formatted = { ...hit }; }); | ||
return newMockResponse; | ||
}, { overwriteRoutes: true }); | ||
} |