Skip to content

Commit

Permalink
Improved fix for #378
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-konshin committed Jun 11, 2021
1 parent dfc1be6 commit fc056a9
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/demo-page/src/pages/pageProps.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import {NextPage} from 'next';
import Link from 'next/link';
import {State} from '../components/reducer';
import {wrapper} from '../components/store';

const PropsPage: NextPage<State> = props => {
return (
<div className="pageProps">
<p>Using Next.js default prop in a wrapped component.</p>
<pre>{JSON.stringify(props)}</pre>
<nav>
<Link href="/">
<a>Navigate to index</a>
</Link>
</nav>
</div>
);
};

PropsPage.getInitialProps = wrapper.getInitialPageProps(store => async () => ({
prop: 'foo',
}));

export default PropsPage;
8 changes: 8 additions & 0 deletions packages/demo-page/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ describe('Basic integration', () => {
await expect(page).toMatch('"page": "was set in index page');
await expect(page).toMatch('"custom": "custom"');
});

it('initial page props', async () => {
await openPage('/pageProps');

await page.waitForSelector('div.pageProps');

await expect(page).toMatch('{"prop":"foo"}');
});
});
25 changes: 25 additions & 0 deletions packages/demo/src/pages/pageProps.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import {NextPage} from 'next';
import Link from 'next/link';
import {State} from '../components/reducer';
import {wrapper} from '../components/store';

const PropsPage: NextPage<State> = props => {
return (
<div className="pageProps">
<p>Using Next.js default prop in a wrapped component.</p>
<pre>{JSON.stringify(props)}</pre>
<nav>
<Link href="/">
<a>Navigate to index</a>
</Link>
</nav>
</div>
);
};

PropsPage.getInitialProps = wrapper.getInitialPageProps(store => async () => ({
prop: 'foo',
}));

export default PropsPage;
8 changes: 8 additions & 0 deletions packages/demo/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ describe('Using App wrapper', () => {
await expect(page).toMatch('"page": "static"'); // redux
await expect(page).toMatch('"app": "was set in _app"'); // redux
});

it('initial page props', async () => {
await openPage('/pageProps');

await page.waitForSelector('div.pageProps');

await expect(page).toMatch('{"appProp":"/pageProps","prop":"foo"}');
});
});
10 changes: 10 additions & 0 deletions packages/wrapper/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ export const createWrapper = <S extends Store>(makeStore: MakeStore<S>, config:
delete resultProps.pageProps.initialState;
}

console.log({initialProps, resultProps});

This comment has been minimized.

Copy link
@Alexander2317

Alexander2317 Jun 13, 2021

Hi)
Remove console.log, please)


// unwrap getInitialPageProps
if (resultProps?.pageProps?.initialProps) {
resultProps.pageProps = {...resultProps.pageProps, ...resultProps.pageProps.initialProps};
delete resultProps.pageProps.initialProps;
}

console.log({initialProps, resultProps});

This comment has been minimized.

Copy link
@Alexander2317

Alexander2317 Jun 13, 2021

Remove console.log, please)


return (
<Provider store={this.store}>
<Component {...initialProps} {...resultProps} />
Expand Down
8 changes: 8 additions & 0 deletions packages/wrapper/tests/server.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ describe('withRedux', () => {
child(<WrappedApp initialProps={{pageProps: {fromApp: true}}} pageProps={{getStaticProp: true}} />),
).toEqual('{"props":{"pageProps":{"fromApp":true,"getStaticProp":true}},"state":{"reduxStatus":"init"}}');
});
test('for page case (new Next versions)', () => {
const WrappedPage: any = createWrapper(makeStore).withRedux(DummyComponent);
expect(
child(<WrappedPage pageProps={{initialProps: {fromPage: true}}} somePropFromNextJs={true} />),
).toEqual(
'{"props":{"pageProps":{"fromPage":true},"somePropFromNextJs":true},"state":{"reduxStatus":"init"}}',
);
});
});
test('wrapped component should not have getInitialProps if source component did not have it', () => {
expect(createWrapper(makeStore).withRedux(DummyComponent).getInitialProps).toBeUndefined();
Expand Down

0 comments on commit fc056a9

Please sign in to comment.