|
1 | 1 | import React from 'react'; |
2 | | -import { |
3 | | - render, screen, initializeMocks, fireEvent, |
4 | | -} from '@src/testUtils'; |
5 | | -import { SwitchEditorCardInternal as SwitchEditorCard, mapDispatchToProps } from './SwitchEditorCard'; |
6 | | -import { thunkActions } from '../../../../../../data/redux'; |
7 | | - |
8 | | -describe('SwitchEditorCard', () => { |
9 | | - const mockSwitchEditor = jest.fn().mockName('switchEditor'); |
| 2 | +import { screen, initializeMocks, within } from '@src/testUtils'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { editorRender } from '@src/editors/editorTestRender'; |
| 5 | +import { mockWaffleFlags } from '@src/data/apiHooks.mock'; |
| 6 | +import { thunkActions } from '@src/editors/data/redux'; |
| 7 | +import SwitchEditorCard from './SwitchEditorCard'; |
| 8 | + |
| 9 | +const switchEditorSpy = jest.spyOn(thunkActions.problem, 'switchEditor'); |
| 10 | + |
| 11 | +describe('SwitchEditorCard - markdown', () => { |
10 | 12 | const baseProps = { |
11 | | - switchEditor: mockSwitchEditor, |
12 | 13 | problemType: 'stringresponse', |
13 | 14 | editorType: 'markdown', |
14 | | - isMarkdownEditorEnabled: false, |
15 | 15 | }; |
16 | 16 |
|
17 | 17 | beforeEach(() => { |
18 | 18 | initializeMocks(); |
19 | 19 | }); |
20 | 20 |
|
21 | | - test('renders SwitchEditorCard', () => { |
22 | | - render(<SwitchEditorCard {...baseProps} />); |
| 21 | + test('renders SwitchEditorCard', async () => { |
| 22 | + // Markdown Editor support is on for this course: |
| 23 | + mockWaffleFlags({ useReactMarkdownEditor: true }); |
| 24 | + // The markdown editor is not currently active (default) |
| 25 | + |
| 26 | + editorRender(<SwitchEditorCard {...baseProps} />); |
| 27 | + const user = userEvent.setup(); |
23 | 28 | expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); |
24 | 29 | const switchButton = screen.getByRole('button', { name: 'Switch to markdown editor' }); |
25 | 30 | expect(switchButton).toBeInTheDocument(); |
26 | | - fireEvent.click(switchButton); |
| 31 | + await user.click(switchButton); |
| 32 | + // A confirmation dialog is shown: |
27 | 33 | expect(screen.getByRole('dialog')).toBeInTheDocument(); |
28 | 34 | }); |
29 | 35 |
|
30 | | - test('calls switchEditor function when confirm button is clicked', () => { |
31 | | - render(<SwitchEditorCard {...baseProps} />); |
| 36 | + test('calls switchEditor function when confirm button is clicked', async () => { |
| 37 | + // Markdown Editor support is on for this course: |
| 38 | + mockWaffleFlags({ useReactMarkdownEditor: true }); |
| 39 | + // The markdown editor is not currently active (default) |
| 40 | + |
| 41 | + editorRender(<SwitchEditorCard {...baseProps} />); |
| 42 | + const user = userEvent.setup(); |
32 | 43 | const switchButton = screen.getByRole('button', { name: 'Switch to markdown editor' }); |
33 | 44 | expect(switchButton).toBeInTheDocument(); |
34 | | - fireEvent.click(switchButton); |
35 | | - const confirmButton = screen.getByRole('button', { name: 'Switch to markdown editor' }); |
| 45 | + await user.click(switchButton); |
| 46 | + |
| 47 | + const modal = screen.getByRole('dialog'); |
| 48 | + const confirmButton = within(modal).getByRole('button', { name: 'Switch to markdown editor' }); |
36 | 49 | expect(confirmButton).toBeInTheDocument(); |
37 | | - fireEvent.click(confirmButton); |
38 | | - expect(mockSwitchEditor).toHaveBeenCalledWith('markdown'); |
| 50 | + expect(switchEditorSpy).not.toHaveBeenCalled(); |
| 51 | + await user.click(confirmButton); |
| 52 | + expect(switchEditorSpy).toHaveBeenCalledWith('markdown'); |
| 53 | + // Markdown editor would now be active. |
39 | 54 | }); |
40 | 55 |
|
41 | 56 | test('renders nothing for advanced problemType', () => { |
42 | | - const { container } = render(<SwitchEditorCard {...baseProps} problemType="advanced" />); |
| 57 | + const { container } = editorRender(<SwitchEditorCard {...baseProps} problemType="advanced" />); |
43 | 58 | const reduxWrapper = (container.firstChild as HTMLElement | null); |
44 | 59 | expect(reduxWrapper?.innerHTML).toBe(''); |
45 | 60 | }); |
46 | 61 |
|
47 | | - test('snapshot: SwitchEditorCard returns null when editor is Markdown', () => { |
48 | | - const { container } = render(<SwitchEditorCard {...baseProps} editorType="markdown" isMarkdownEditorEnabled />); |
49 | | - const reduxWrapper = (container.firstChild as HTMLElement | null); |
50 | | - expect(reduxWrapper?.innerHTML).toBe(''); |
51 | | - }); |
| 62 | + test('returns null when editor is already Markdown', () => { |
| 63 | + // Markdown Editor support is on for this course: |
| 64 | + mockWaffleFlags({ useReactMarkdownEditor: true }); |
| 65 | + // The markdown editor *IS* currently active (default) |
52 | 66 |
|
53 | | - describe('mapDispatchToProps', () => { |
54 | | - test('updateField from actions.problem.updateField', () => { |
55 | | - expect(mapDispatchToProps.switchEditor).toEqual(thunkActions.problem.switchEditor); |
| 67 | + const { container } = editorRender(<SwitchEditorCard {...baseProps} />, { |
| 68 | + initialState: { |
| 69 | + problem: { |
| 70 | + isMarkdownEditorEnabled: true, |
| 71 | + }, |
| 72 | + }, |
56 | 73 | }); |
| 74 | + const reduxWrapper = (container.firstChild as HTMLElement | null); |
| 75 | + expect(reduxWrapper?.innerHTML).toBe(''); |
57 | 76 | }); |
58 | 77 | }); |
0 commit comments