Skip to content

Instantly share code, notes, and snippets.

@xndyz
Last active May 7, 2018 17:29
Show Gist options
  • Select an option

  • Save xndyz/565ad8ca4e5ddb1844c81c6a66883175 to your computer and use it in GitHub Desktop.

Select an option

Save xndyz/565ad8ca4e5ddb1844c81c6a66883175 to your computer and use it in GitHub Desktop.
Slate testing example
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();
});
});
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