File tree Expand file tree Collapse file tree 5 files changed +56
-2
lines changed Expand file tree Collapse file tree 5 files changed +56
-2
lines changed Original file line number Diff line number Diff line change @@ -3,4 +3,4 @@ import { createContext } from 'react';
33import { EditorStore } from './store' ;
44
55export type EditorContext = EditorStore ;
6- export const EditorContext = createContext < EditorContext > ( { } as EditorContext ) ;
6+ export const EditorContext = createContext < EditorContext > ( null ) ;
Original file line number Diff line number Diff line change @@ -4,8 +4,10 @@ import {
44 QueryCallbacksFor ,
55 wrapConnectorHooks ,
66 ChainableConnectors ,
7+ ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT ,
78} from '@craftjs/utils' ;
89import { useContext , useMemo } from 'react' ;
10+ import invariant from 'tiny-invariant' ;
911
1012import { EditorContext } from './EditorContext' ;
1113import { QueryMethods } from './query' ;
@@ -35,8 +37,10 @@ export type useInternalEditorReturnType<C = null> = useCollectorReturnType<
3537export function useInternalEditor < C > (
3638 collector ?: EditorCollector < C >
3739) : useInternalEditorReturnType < C > {
38- const handlers = useEventHandler ( ) ;
3940 const store = useContext ( EditorContext ) ;
41+ invariant ( store , ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT ) ;
42+
43+ const handlers = useEventHandler ( ) ;
4044 const collected = useCollector ( store , collector ) ;
4145
4246 const connectors = useMemo (
Original file line number Diff line number Diff line change 1+ import {
2+ ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT ,
3+ ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT ,
4+ } from '@craftjs/utils' ;
5+ import { render } from '@testing-library/react' ;
6+ import * as React from 'react' ;
7+
8+ import { useEditor } from '../useEditor' ;
9+ import { useNode } from '../useNode' ;
10+
11+ /**
12+ * since we are mocking useInternalEditor in useEditor.test.tsx we are using a dedicated test suite here
13+ */
14+ describe ( 'Using useEditor outside of EditorContext' , ( ) => {
15+ it ( 'should throw an error when we use useEditor outside of <Editor />' , ( ) => {
16+ const TestComponent = ( ) => {
17+ useEditor ( ) ;
18+ return null ;
19+ } ;
20+
21+ expect ( ( ) => {
22+ render ( < TestComponent /> ) ;
23+ } ) . toThrowError ( ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT ) ;
24+ } ) ;
25+ } ) ;
26+
27+ describe ( 'Using useNode outside of EditorContext' , ( ) => {
28+ it ( 'should throw an error when we use useNode outside of <Editor />' , ( ) => {
29+ const TestComponent = ( ) => {
30+ useNode ( ) ;
31+ return null ;
32+ } ;
33+
34+ expect ( ( ) => {
35+ render ( < TestComponent /> ) ;
36+ } ) . toThrowError ( ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT ) ;
37+ } ) ;
38+ } ) ;
Original file line number Diff line number Diff line change 1+ import { ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT } from '@craftjs/utils' ;
12import { useMemo , useContext } from 'react' ;
3+ import invariant from 'tiny-invariant' ;
24
35import { NodeContext , NodeContextType } from './NodeContext' ;
46
@@ -26,6 +28,8 @@ export function useInternalNode<S = null>(
2628 collect ?: ( node : Node ) => S
2729) : useInternalNodeReturnType < S > {
2830 const context = useContext ( NodeContext ) ;
31+ invariant ( context , ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT ) ;
32+
2933 const { id, related, connectors } = context ;
3034
3135 const { actions : EditorActions , query, ...collected } = useInternalEditor (
Original file line number Diff line number Diff line change @@ -43,3 +43,11 @@ export const ERROR_DESERIALIZE_COMPONENT_NOT_IN_RESOLVER = `An Error occurred wh
4343Available components in resolver: %availableComponents%
4444
4545More info: https://craft.js.org/r/docs/api/editor#props` ;
46+
47+ export const ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT = `You can only use useEditor in the context of <Editor />.
48+
49+ Please only use useEditor in components that are children of the <Editor /> component.` ;
50+
51+ export const ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT = `You can only use useNode in the context of <Editor />.
52+
53+ Please only use useNode in components that are children of the <Editor /> component.` ;
You can’t perform that action at this time.
0 commit comments