import React from 'react'; import { act, render, waitFor } from '@testing-library/react'; import '@testing-library/jest-dom'; import { EventrixProvider } from '../context'; import Eventrix from '../../Eventrix'; import useEvent from './useEvent'; import useEventrixState from './useEventrixState'; import EventrixScope from '../context/EventrixScope'; describe('useEventrixState', () => { const OtherStateComponent = () => { const [test, setTest] = useEventrixState('test'); useEvent('changeTest', (newState) => setTest(newState)); return
{!!test &&
{test}
}
; }; const FooInFooComponent = () => { const [foo = {}] = useEventrixState('foo'); return (
{!!foo.description &&
{foo.description}
}
); }; const FooBarComponent = () => { const [fooBar] = useEventrixState('foo.bar'); return
{!!fooBar &&
{fooBar}
}
; }; const FooComponent = () => { const [foo = {}, setFoo] = useEventrixState('foo'); useEvent('changeFoo', (newState) => setFoo(newState)); return (
{!!foo.title &&
{foo.title}
}
); }; const TestContainer = ({ eventrix, children }: any) => {children}; it('should change state when eventrix state has changed', async () => { const initialState = { foo: { title: 'fooTitle1', description: 'fooDescription1', bar: 'fooBar1', }, test: 'test1', }; const eventrixInstance = new Eventrix(initialState); const secondFooData = { title: 'secondTitle', description: 'secondDescription', bar: 'initialBar', }; const secondTestData = 'secondTest'; const { getByTestId } = render( , ); expect(getByTestId('fooTitle').textContent).toEqual(initialState.foo.title); expect(getByTestId('fooDescription').textContent).toEqual(initialState.foo.description); expect(getByTestId('fooBarData').textContent).toEqual(initialState.foo.bar); expect(getByTestId('testData').textContent).toEqual(initialState.test); act(() => { eventrixInstance.emit('changeTest', secondTestData); }); await waitFor(() => getByTestId('testDataValue')); expect(getByTestId('fooTitle').textContent).toEqual(initialState.foo.title); expect(getByTestId('fooDescription').textContent).toEqual(initialState.foo.description); expect(getByTestId('fooBarData').textContent).toEqual(initialState.foo.bar); expect(getByTestId('testData').textContent).toEqual(secondTestData); act(() => { eventrixInstance.emit('changeFoo', secondFooData); }); await waitFor(() => getByTestId('fooTitleValue')); await waitFor(() => getByTestId('fooDescriptionValue')); await waitFor(() => getByTestId('fooBarDataValue')); expect(getByTestId('fooTitle').textContent).toEqual(secondFooData.title); expect(getByTestId('fooDescription').textContent).toEqual(secondFooData.description); expect(getByTestId('fooBarData').textContent).toEqual(secondFooData.bar); expect(getByTestId('testData').textContent).toEqual(secondTestData); }); it('should change state when eventrix state has changed with scope', async () => { const initialState = { testScope: { foo: { title: 'fooTitle1', description: 'fooDescription1', bar: 'fooBar1', }, test: 'test1', }, }; const initialData = initialState.testScope; const eventrixInstance = new Eventrix(initialState); const secondFooData = { title: 'secondTitle', description: 'secondDescription', bar: 'initialBar', }; const secondTestData = 'secondTest'; const { getByTestId } = render( , ); expect(getByTestId('fooTitle').textContent).toEqual(initialData.foo.title); expect(getByTestId('fooDescription').textContent).toEqual(initialData.foo.description); expect(getByTestId('fooBarData').textContent).toEqual(initialData.foo.bar); expect(getByTestId('testData').textContent).toEqual(initialData.test); act(() => { eventrixInstance.emit('changeTest', secondTestData); }); await waitFor(() => getByTestId('testDataValue')); expect(getByTestId('fooTitle').textContent).toEqual(initialData.foo.title); expect(getByTestId('fooDescription').textContent).toEqual(initialData.foo.description); expect(getByTestId('fooBarData').textContent).toEqual(initialData.foo.bar); expect(getByTestId('testData').textContent).toEqual(secondTestData); act(() => { eventrixInstance.emit('changeFoo', secondFooData); }); await waitFor(() => getByTestId('fooTitleValue')); await waitFor(() => getByTestId('fooDescriptionValue')); await waitFor(() => getByTestId('fooBarDataValue')); expect(getByTestId('fooTitle').textContent).toEqual(secondFooData.title); expect(getByTestId('fooDescription').textContent).toEqual(secondFooData.description); expect(getByTestId('fooBarData').textContent).toEqual(secondFooData.bar); expect(getByTestId('testData').textContent).toEqual(secondTestData); }); it('should change state when eventrix state has changed with deep scope', async () => { const initialState = { scopeLvl1: { testScope: { foo: { title: 'fooTitle1', description: 'fooDescription1', bar: 'fooBar1', }, test: 'test1', }, }, }; const initialData = initialState.scopeLvl1.testScope; const eventrixInstance = new Eventrix(initialState); const secondFooData = { title: 'secondTitle', description: 'secondDescription', bar: 'initialBar', }; const secondTestData = 'secondTest'; const { getByTestId } = render( , ); expect(getByTestId('fooTitle').textContent).toEqual(initialData.foo.title); expect(getByTestId('fooDescription').textContent).toEqual(initialData.foo.description); expect(getByTestId('fooBarData').textContent).toEqual(initialData.foo.bar); expect(getByTestId('testData').textContent).toEqual(initialData.test); act(() => { eventrixInstance.emit('changeTest', secondTestData); }); await waitFor(() => getByTestId('testDataValue')); expect(getByTestId('fooTitle').textContent).toEqual(initialData.foo.title); expect(getByTestId('fooDescription').textContent).toEqual(initialData.foo.description); expect(getByTestId('fooBarData').textContent).toEqual(initialData.foo.bar); expect(getByTestId('testData').textContent).toEqual(secondTestData); act(() => { eventrixInstance.emit('changeFoo', secondFooData); }); await waitFor(() => getByTestId('fooTitleValue')); await waitFor(() => getByTestId('fooDescriptionValue')); await waitFor(() => getByTestId('fooBarDataValue')); expect(getByTestId('fooTitle').textContent).toEqual(secondFooData.title); expect(getByTestId('fooDescription').textContent).toEqual(secondFooData.description); expect(getByTestId('fooBarData').textContent).toEqual(secondFooData.bar); expect(getByTestId('testData').textContent).toEqual(secondTestData); }); });