Skip to content

Commit e74c598

Browse files
Update implementation with feedback
1 parent d4a437b commit e74c598

File tree

10 files changed

+176
-131
lines changed

10 files changed

+176
-131
lines changed

examples/advanced-shared/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ const App = () => (
4242
<ThemingContainer scope="t1">
4343
<ThemeHook title="scope" />
4444
</ThemingContainer>
45-
{/* <ThemingContainer initialData={initialData}>
45+
<ThemingContainer initialData={initialData}>
4646
<ThemeHook title="local" />
47-
</ThemingContainer> */}
47+
</ThemingContainer>
4848
<ThemingContainer scope="t1">
4949
<ThemeHook title="scope sync" />
5050
</ThemingContainer>

src/__tests__/integration.test.js

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,8 @@ describe('Integration', () => {
469469
expect(handlers2.onDestroy).toHaveBeenCalledTimes(1);
470470
});
471471

472-
it('should throw an error if contained store is used without container', async () => {
473-
const rejectSpy = jest
474-
.spyOn(Promise, 'reject')
475-
.mockImplementation(() => {});
472+
it('should throw an error if contained store is used without container', () => {
473+
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
476474
const Store1 = createStore({
477475
name: 'one',
478476
initialState: { todos: [], loading: false },
@@ -481,13 +479,11 @@ describe('Integration', () => {
481479
});
482480

483481
const Subscriber = createSubscriber(Store1);
484-
render(<Subscriber>{() => null}</Subscriber>);
485-
await actTick();
486482

487-
expect(rejectSpy).toHaveBeenCalled();
488-
const [error] = rejectSpy.mock.calls[0];
489-
expect(error).toEqual(expect.any(Error));
490-
expect(error.message).toContain('should be contained');
483+
expect(() => render(<Subscriber>{() => null}</Subscriber>)).toThrow(
484+
/should be contained/
485+
);
486+
errorSpy.mockRestore();
491487
});
492488

493489
describe('dispatchTo', () => {
@@ -514,107 +510,131 @@ describe('Integration', () => {
514510
},
515511
});
516512

517-
const MainContainer = mainContainer ?? (({ children }) => children);
518-
const OtherContainer = otherContainer ?? (({ children }) => children);
519513
const MainSubscriber = createSubscriber(StoreMain);
520514
const OtherSubscriber = createSubscriber(StoreOther);
521515
const mainSpy = jest.fn().mockReturnValue(null);
522516
const otherSpy = jest.fn().mockReturnValue(null);
523517

524-
const App = ({ plus }) => (
525-
<MainContainer>
526-
<OtherContainer plus={plus}>
527-
<MainSubscriber>{mainSpy}</MainSubscriber>
528-
<OtherSubscriber>{otherSpy}</OtherSubscriber>
529-
</OtherContainer>
530-
</MainContainer>
518+
const Content = () => (
519+
<>
520+
<MainSubscriber>{mainSpy}</MainSubscriber>
521+
<OtherSubscriber>{otherSpy}</OtherSubscriber>
522+
</>
531523
);
532524
return {
533-
App,
525+
Content,
534526
StoreMain,
535527
mainReturn: (n = 0) => mainSpy.mock.calls[n],
536528
otherReturn: (n = 0) => otherSpy.mock.calls[n],
537529
};
538530
};
539531

540-
it('should allow dispatchTo global -> global', async () => {
541-
const { App, mainReturn, otherReturn } = createTestElements({
532+
it('should allow dispatchTo global -> global', () => {
533+
const { Content, mainReturn, otherReturn } = createTestElements({
542534
mainContainer: null,
543535
otherContainer: null,
544536
});
545537

546-
render(<App />);
538+
render(<Content />);
547539
const [, mainActions] = mainReturn(0);
548540
act(() => mainActions.setOther(1));
549541

550542
expect(otherReturn(1)).toEqual([{ count: 1 }, expect.any(Object)]);
551543
});
552544

553-
it('should allow dispatchTo contained -> contained', async () => {
545+
it('should allow dispatchTo contained -> contained', () => {
554546
const SharedContainer = createContainer();
555-
const { App, mainReturn, otherReturn } = createTestElements({
547+
const { Content, mainReturn, otherReturn } = createTestElements({
556548
mainContainer: SharedContainer,
557549
otherContainer: SharedContainer,
558550
});
559551

560-
render(<App />);
552+
render(
553+
<SharedContainer>
554+
<Content />
555+
</SharedContainer>
556+
);
561557
const [, mainActions] = mainReturn(0);
562558
act(() => mainActions.setOther(1));
563559

564560
expect(otherReturn(1)).toEqual([{ count: 1 }, expect.any(Object)]);
565561
});
566562

567-
it('should error when dispatchTo contained -> global', () => {
568-
const { App, mainReturn } = createTestElements({
569-
mainContainer: createContainer(),
563+
it('should allow dispatchTo contained -> global', () => {
564+
const MainContainer = createContainer();
565+
const { Content, mainReturn, otherReturn } = createTestElements({
566+
mainContainer: MainContainer,
570567
otherContainer: null,
571568
});
572569

573-
render(<App />);
570+
render(
571+
<MainContainer>
572+
<Content />
573+
</MainContainer>
574+
);
574575
const [, mainActions] = mainReturn(0);
576+
act(() => mainActions.setOther(1));
575577

576-
expect(() => mainActions.setOther(1)).toThrow(/Illegal/);
578+
expect(otherReturn(1)).toEqual([{ count: 1 }, expect.any(Object)]);
577579
});
578580

579-
it('should error when dispatchTo global -> contained', async () => {
580-
const { App, mainReturn } = createTestElements({
581-
mainContainer: createContainer(),
582-
otherContainer: null,
581+
it('should allow dispatchTo global -> contained if properly contained', () => {
582+
const OtherContainer = createContainer({ displayName: 'OtherContainer' });
583+
const { Content, mainReturn, otherReturn } = createTestElements({
584+
mainContainer: null,
585+
otherContainer: OtherContainer,
583586
});
584587

585-
render(<App />);
588+
render(
589+
<OtherContainer>
590+
<Content />
591+
</OtherContainer>
592+
);
586593
const [, mainActions] = mainReturn(0);
594+
act(() => mainActions.setOther(1));
587595

588-
expect(() => mainActions.setOther(1)).toThrow(/Illegal/);
596+
expect(otherReturn(1)).toEqual([{ count: 1 }, expect.any(Object)]);
589597
});
590598

591-
it('should error when dispatchTo contained -> other contained', async () => {
592-
const { App, mainReturn } = createTestElements({
593-
mainContainer: createContainer(),
594-
otherContainer: createContainer(),
599+
it('should allow dispatchTo contained -> other contained', async () => {
600+
const MainContainer = createContainer();
601+
const OtherContainer = createContainer();
602+
603+
const { Content, mainReturn, otherReturn } = createTestElements({
604+
mainContainer: MainContainer,
605+
otherContainer: OtherContainer,
595606
});
596607

597-
render(<App />);
608+
render(
609+
<OtherContainer>
610+
<MainContainer>
611+
<Content />
612+
</MainContainer>
613+
</OtherContainer>
614+
);
598615
const [, mainActions] = mainReturn(0);
616+
act(() => mainActions.setOther(1));
599617

600-
expect(() => mainActions.setOther(1)).toThrow(/Illegal/);
618+
expect(otherReturn(1)).toEqual([{ count: 1 }, expect.any(Object)]);
601619
});
602620

603-
it('should error when dispatchTo override -> contained', async () => {
604-
const { App, StoreMain, mainReturn } = createTestElements({
605-
mainContainer: null,
606-
otherContainer: null,
607-
});
621+
it('should allow dispatchTo override -> contained', async () => {
622+
const { Content, StoreMain, mainReturn, otherReturn } =
623+
createTestElements({
624+
mainContainer: null,
625+
otherContainer: null,
626+
});
608627
const OverrideContainer = createContainer(StoreMain);
609628

610629
render(
611630
<OverrideContainer>
612-
<App />
631+
<Content />
613632
</OverrideContainer>
614633
);
615634
const [, mainActions] = mainReturn(0);
635+
act(() => mainActions.setOther(1));
616636

617-
expect(() => mainActions.setOther(1)).toThrow(/Illegal/);
637+
expect(otherReturn(1)).toEqual([{ count: 1 }, expect.any(Object)]);
618638
});
619639
});
620640
});

src/components/__tests__/container.test.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env jest */
22

33
import React from 'react';
4-
import { render } from '@testing-library/react';
4+
import { render, act } from '@testing-library/react';
55

66
import { StoreMock, storeStateMock } from '../../__tests__/mocks';
77
import { defaultRegistry, StoreRegistry } from '../../store/registry';
@@ -82,7 +82,11 @@ describe('Container', () => {
8282
const children = <Subscriber>{() => null}</Subscriber>;
8383
render(<Container scope="s1">{children}</Container>);
8484

85-
expect(defaultRegistry.getStore).toHaveBeenCalledWith(Store, 's1', true);
85+
expect(defaultRegistry.getStore).toHaveBeenCalledWith(
86+
Store,
87+
's1',
88+
expect.any(Function)
89+
);
8690
expect(mockLocalRegistry.getStore).not.toHaveBeenCalled();
8791
});
8892

@@ -104,7 +108,11 @@ describe('Container', () => {
104108
</Container>
105109
);
106110

107-
expect(defaultRegistry.getStore).toHaveBeenCalledWith(Store, 's2', true);
111+
expect(defaultRegistry.getStore).toHaveBeenCalledWith(
112+
Store,
113+
's2',
114+
expect.any(Function)
115+
);
108116
});
109117

110118
it('should get local storeState if local matching', () => {
@@ -115,7 +123,7 @@ describe('Container', () => {
115123
expect(mockLocalRegistry.getStore).toHaveBeenCalledWith(
116124
Store,
117125
undefined,
118-
true
126+
expect.any(Function)
119127
);
120128
expect(defaultRegistry.getStore).not.toHaveBeenCalled();
121129
});
@@ -128,7 +136,7 @@ describe('Container', () => {
128136
expect(defaultRegistry.getStore).toHaveBeenCalledWith(
129137
Store,
130138
undefined,
131-
true
139+
expect.any(Function)
132140
);
133141
expect(mockLocalRegistry.getStore).not.toHaveBeenCalled();
134142
});
@@ -271,7 +279,7 @@ describe('Container', () => {
271279
const children = <Subscriber>{renderPropChildren}</Subscriber>;
272280
render(<Container defaultCount={5}>{children}</Container>);
273281
const [, { increase }] = renderPropChildren.mock.calls[0];
274-
increase();
282+
act(() => increase());
275283

276284
expect(actionInner).toHaveBeenCalledWith(expect.any(Object), {
277285
defaultCount: 5,
@@ -289,7 +297,7 @@ describe('Container', () => {
289297
);
290298
const [, { increase }] = renderPropChildren.mock.calls[0];
291299
rerender(<Container defaultCount={6}>{children}</Container>);
292-
increase();
300+
act(() => increase());
293301

294302
expect(actionInner).toHaveBeenCalledWith(expect.any(Object), {
295303
defaultCount: 6,

src/components/__tests__/hook.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ describe('Hook', () => {
6161
it('should get the storeState from registry', () => {
6262
const { getRender } = setup();
6363
getRender();
64-
expect(defaultRegistry.getStore).toHaveBeenCalledWith(StoreMock);
64+
expect(defaultRegistry.getStore).toHaveBeenCalledWith(
65+
StoreMock,
66+
undefined,
67+
expect.any(Function)
68+
);
6569
});
6670

6771
it('should render children with store data and actions', () => {

0 commit comments

Comments
 (0)