diff --git a/src/tests/group.test.ts b/src/tests/group.test.ts index d53c41e..266d725 100644 --- a/src/tests/group.test.ts +++ b/src/tests/group.test.ts @@ -14,6 +14,7 @@ import type { MockStage } from './mocks/mouse'; // Test Component Wrappers import ConfigBinding from './wrappers/ConfigBinding.test.svelte'; +import ContainerContext from './wrappers/ContainerContext.test.svelte'; test('throws an error if not placed inside a Container (Layer, Group, Label) component', () => { expect(() => { @@ -179,30 +180,37 @@ test('Does not update config if instantiated with staticConfig prop', () => { }); test('sets the correct context', () => { - const rendered = render(Group, { - context: createMockParentContext(Container.Layer) - }); + let childContext: Map | null = null; + let handle: Konva.Group | null = null; - const component = rendered.component.$$; - const context = component.context; - const handle = rendered.component.handle; + render(ContainerContext, { + context: createMockParentContext(Container.Layer), + props: { + component: Group, + getHandle: (hnd) => (handle = hnd), + getComponentContext: (ctxMap) => (childContext = ctxMap) + } + }); - expect(get(context.get(CONTAINER_COMPONENT_KEYS[Container.Group]))).toStrictEqual(handle); + expect(get(childContext!.get(CONTAINER_COMPONENT_KEYS[Container.Group]))).toStrictEqual(handle!); }); test('nulls unused context', () => { - const rendered = render(Group, { - context: createMockParentContext(Container.Layer) - }); + let childContext: Map | null = null; - const component = rendered.component.$$; - const context = component.context; + render(ContainerContext, { + context: createMockParentContext(Container.Layer), + props: { + component: Group, + getComponentContext: (ctxMap) => (childContext = ctxMap) + } + }); const otherKeys = CONTAINER_COMPONENT_KEYS.slice(); otherKeys.splice(Container.Group, 1); otherKeys.forEach((e) => { - expect(context.get(e)).toBe(null); + expect(childContext!.get(e)).toBe(null); }); }); diff --git a/src/tests/label.test.ts b/src/tests/label.test.ts index 0481d55..2aae94b 100644 --- a/src/tests/label.test.ts +++ b/src/tests/label.test.ts @@ -14,6 +14,7 @@ import type { MockStage } from './mocks/mouse'; // Test Component Wrappers import ConfigBinding from './wrappers/ConfigBinding.test.svelte'; +import ContainerContext from './wrappers/ContainerContext.test.svelte'; test('throws an error if not placed inside a Container (Layer, Group, Label) component', () => { expect(() => { @@ -182,36 +183,37 @@ test('Does not update config if instantiated with staticConfig prop', async () = }); test('sets the correct context', () => { - const rendered = render(Label, { + let childContext: Map | null = null; + let handle: Konva.Label | null = null; + + render(ContainerContext, { context: createMockParentContext(Container.Layer), props: { - config: {} + component: Label, + getHandle: (hnd) => (handle = hnd), + getComponentContext: (ctxMap) => (childContext = ctxMap) } }); - const component = rendered.component.$$; - const context = component.context; - const handle = rendered.component.handle; - - expect(get(context.get(CONTAINER_COMPONENT_KEYS[Container.Label]))).toStrictEqual(handle); + expect(get(childContext!.get(CONTAINER_COMPONENT_KEYS[Container.Label]))).toStrictEqual(handle!); }); test('nulls unused context', () => { - const rendered = render(Label, { + let childContext: Map | null = null; + + render(ContainerContext, { context: createMockParentContext(Container.Layer), props: { - config: {} + component: Label, + getComponentContext: (ctxMap) => (childContext = ctxMap) } }); - const component = rendered.component.$$; - const context = component.context; - const otherKeys = CONTAINER_COMPONENT_KEYS.slice(); otherKeys.splice(Container.Label, 1); otherKeys.forEach((e) => { - expect(context.get(e)).toBe(null); + expect(childContext!.get(e)).toBe(null); }); }); diff --git a/src/tests/layer.test.ts b/src/tests/layer.test.ts index 0ee8cfa..572c622 100644 --- a/src/tests/layer.test.ts +++ b/src/tests/layer.test.ts @@ -14,6 +14,7 @@ import type { MockStage } from './mocks/mouse'; // Test Component Wrappers import ConfigBinding from './wrappers/ConfigBinding.test.svelte'; +import ContainerContext from './wrappers/ContainerContext.test.svelte'; test('throws an error if not placed inside a Stage component', () => { expect(() => { @@ -161,31 +162,38 @@ test('Does not update config if instantiated with staticConfig prop', async () = test('sets the correct context', () => { const div = document.createElement('div'); - const rendered = render(Layer, { - context: createMockParentContext(Container.Stage, div) - }); + let childContext: Map | null = null; + let handle: Konva.Layer | null = null; - const component = rendered.component.$$; - const context = component.context; - const handle = rendered.component.handle; + render(ContainerContext, { + context: createMockParentContext(Container.Stage, div), + props: { + component: Layer, + getHandle: (hnd) => (handle = hnd), + getComponentContext: (ctxMap) => (childContext = ctxMap) + } + }); - expect(get(context.get(CONTAINER_COMPONENT_KEYS[Container.Layer]))).toStrictEqual(handle); + expect(get(childContext!.get(CONTAINER_COMPONENT_KEYS[Container.Layer]))).toStrictEqual(handle!); }); test('nulls unused context', () => { const div = document.createElement('div'); - const rendered = render(Layer, { - context: createMockParentContext(Container.Stage, div) - }); + let childContext: Map | null = null; - const component = rendered.component.$$; - const context = component.context; + render(ContainerContext, { + context: createMockParentContext(Container.Stage, div), + props: { + component: Layer, + getComponentContext: (ctxMap) => (childContext = ctxMap) + } + }); const otherKeys = CONTAINER_COMPONENT_KEYS.slice(); otherKeys.splice(Container.Layer, 1); otherKeys.forEach((e) => { - expect(context.get(e)).toBe(null); + expect(childContext!.get(e)).toBe(null); }); }); diff --git a/src/tests/stage.test.ts b/src/tests/stage.test.ts index a19fa4f..000cae0 100644 --- a/src/tests/stage.test.ts +++ b/src/tests/stage.test.ts @@ -13,6 +13,7 @@ import type { MockStage } from './mocks/mouse'; // Test Component Wrappers import ConfigBinding from './wrappers/ConfigBinding.test.svelte'; +import ContainerContext from './wrappers/ContainerContext.test.svelte'; test('creates a div container and forwards rest props to div', () => { const rendered = render(Stage, { @@ -127,28 +128,37 @@ test('Does not update config if instantiated with staticConfig prop', async () = }); test('sets the correct context', () => { - const rendered = render(Stage, { - config: { width: 1000, height: 1000 } - }); - - const component = rendered.component.$$; + let childContext: Map | null = null; + let handle: Konva.Stage | null = null; - const context = component.context; - const handle = rendered.component.handle(); + render(ContainerContext, { + props: { + component: Stage, + config: { width: 1000, height: 1000 }, + getHandle: (hnd) => (handle = hnd()), + getComponentContext: (ctxMap) => (childContext = ctxMap) + } + }); - expect(get(context.get(CONTAINER_COMPONENT_KEYS[Container.Stage]))).toStrictEqual(handle); + expect(get(childContext!.get(CONTAINER_COMPONENT_KEYS[Container.Stage]))).toStrictEqual(handle!); }); test('nulls unused context', () => { - render(Stage, { - props: { config: { width: 1000, height: 1000 } } + let childContext: Map | null = null; + + render(ContainerContext, { + props: { + component: Stage, + config: { width: 1000, height: 1000 }, + getComponentContext: (ctxMap) => (childContext = ctxMap) + } }); const otherKeys = CONTAINER_COMPONENT_KEYS.slice(); otherKeys.splice(Container.Stage, 1); otherKeys.forEach((e) => { - expect(context.get(e)).toBe(null); + expect(childContext!.get(e)).toBe(null); }); }); diff --git a/src/tests/wrappers/ContainerContext.test.svelte b/src/tests/wrappers/ContainerContext.test.svelte new file mode 100644 index 0000000..56e6a1f --- /dev/null +++ b/src/tests/wrappers/ContainerContext.test.svelte @@ -0,0 +1,31 @@ + + + + + + diff --git a/src/tests/wrappers/ContextReporter.test.svelte b/src/tests/wrappers/ContextReporter.test.svelte new file mode 100644 index 0000000..fd56162 --- /dev/null +++ b/src/tests/wrappers/ContextReporter.test.svelte @@ -0,0 +1,23 @@ + +