Last active
May 7, 2018 17:29
-
-
Save xndyz/565ad8ca4e5ddb1844c81c6a66883175 to your computer and use it in GitHub Desktop.
Slate testing example
This file contains hidden or 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
| import {Value} from 'slate'; | |
| import Simulator from 'slate-simulator'; | |
| import {plugins} from '../'; | |
| import {mockPlugins} from '../../utilities'; | |
| import basicFixture from './fixtures/basic.json'; | |
| describe('Paragraph', () => { | |
| it('renders the placeholder', () => { | |
| const value = Value.fromJSON(basicFixture); | |
| const renderPlaceholderSpy = jest.fn(); | |
| const keyDownSpy = jest.fn(); | |
| const mockedPlugins = mockPlugins({ | |
| plugins, | |
| mocks: { | |
| renderPlaceholder: renderPlaceholderSpy, | |
| }, | |
| }); | |
| const simulator = new Simulator({ | |
| plugins: mockedPlugins, | |
| value, | |
| }); | |
| simulator.focus(); | |
| expect(renderPlaceholderSpy).toBeCalled(); | |
| }); | |
| }); |
This file contains hidden or 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
| export function mockPlugins({ | |
| plugins, | |
| mocks, | |
| }: { | |
| plugins: any[]; | |
| mocks: {[plugin: string]: jest.Mock<{}>}; | |
| }) { | |
| let mockedPlugins; | |
| Object.keys(mocks).forEach((mockName) => { | |
| mockedPlugins = plugins.map((plugin) => { | |
| let mockedPlugin; | |
| Object.values<Function>(plugin).forEach((plugin) => { | |
| if (plugin.name === mockName) { | |
| mockedPlugin = Object.assign( | |
| { | |
| [mockName]: mocks[mockName], | |
| }, | |
| plugin, | |
| ); | |
| } | |
| }); | |
| return mockedPlugin || plugin; | |
| }); | |
| }); | |
| return mockedPlugins; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment