File tree Expand file tree Collapse file tree 5 files changed +58
-2
lines changed Expand file tree Collapse file tree 5 files changed +58
-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 EventHandlerConnectors ,
7+ ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT ,
78} from '@craftjs/utils' ;
89import { useContext , useEffect , useMemo } from 'react' ;
10+ import invariant from 'tiny-invariant' ;
911
1012import { EditorContext } from './EditorContext' ;
1113import { QueryMethods } from './query' ;
@@ -34,6 +36,8 @@ export function useInternalEditor<C>(
3436) : useInternalEditorReturnType < C > {
3537 const handler = useEventHandler ( ) ;
3638 const store = useContext ( EditorContext ) ;
39+ invariant ( store , ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT ) ;
40+
3741 const collected = useCollector ( store , collector ) ;
3842
3943 const connectorsUsage = 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 { wrapConnectorHooks } from '@craftjs/utils' ;
1+ import {
2+ wrapConnectorHooks ,
3+ ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT ,
4+ } from '@craftjs/utils' ;
25import { useMemo , useContext } from 'react' ;
6+ import invariant from 'tiny-invariant' ;
37
48import { NodeContext } from './NodeContext' ;
59
@@ -8,6 +12,8 @@ import { Node } from '../interfaces';
812
913export function useInternalNode < S = null > ( collect ?: ( node : Node ) => S ) {
1014 const context = useContext ( NodeContext ) ;
15+ invariant ( context , ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT ) ;
16+
1117 const { id, related } = context ;
1218
1319 const {
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