Skip to content

Commit

Permalink
Merge f7db5d6 into 32eb92b
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-konshin committed Apr 9, 2020
2 parents 32eb92b + f7db5d6 commit 2eca1a8
Show file tree
Hide file tree
Showing 55 changed files with 2,177 additions and 2,470 deletions.
10 changes: 9 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ deploy:
script: yarn publish:release ${TRAVIS_TAG} --yes
skip_cleanup: true
on:
tags: true
all_branches: true
condition: $TRAVIS_TAG != "" && $TRAVIS_TAG != *"-"*
repo: kirill-konshin/next-redux-wrapper
- provider: script
script: yarn publish:release ${TRAVIS_TAG} --yes --dist-tag next
skip_cleanup: true
on:
all_branches: true
condition: $TRAVIS_TAG == *"-"*
repo: kirill-konshin/next-redux-wrapper

after_success: yarn test:coverage
689 changes: 376 additions & 313 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "5.0.0",
"version": "6.0.0",
"useWorkspaces": true,
"npmClient": "yarn"
}
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
{
"name": "next-redux-wrapper-mono",
"private": true,
"version": "5.0.0",
"version": "6.0.0",
"description": "Redux wrapper for Next.js",
"scripts": {
"clean": "lerna run clean && lerna clean --yes && rimraf node_modules",
"start": "yarn build:quick && lerna run start --parallel",
"start": "yarn build:quick && yarn start:quick",
"start:quick": "lerna run start --parallel",
"build": "lerna run build --stream",
"build:quick": "yarn build --scope=next-redux-wrapper",
"test": "lerna run test --stream",
"test:quick": "lerna run test:quick --stream",
"test": "lerna run test --parallel",
"test:quick": "lerna run test:quick --parallel",
"test:coverage": "cat packages/*/coverage/lcov.info | coveralls",
"publish:release": "lerna publish --tag-version-prefix=\"\" --force-publish=* --no-push --no-git-tag-version",
"lint": "eslint --cache --cache-location .eslint/cache --fix",
"lint:all": "yarn lint 'packages/*/**/*.{ts,tsx}' 'packages/*/*.js'",
"lint:staged": "lint-staged --debug"
},
"devDependencies": {
"coveralls": "3.0.9",
"coveralls": "3.0.11",
"eslint": "6.8.0",
"eslint-config-ringcentral-typescript": "3.0.0",
"husky": "4.2.3",
"lerna": "3.20.2",
"lint-staged": "10.0.7",
"lint-staged": "10.1.1",
"rimraf": "3.0.2",
"typescript": "3.7.5"
"typescript": "3.8.3"
},
"resolutions": {
"@types/react": "16.9.21"
"@types/react": "16.9.31"
},
"workspaces": [
"packages/*"
Expand Down
7 changes: 7 additions & 0 deletions packages/configs/jest-puppeteer.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
server: {
command: 'yarn serve',
debug: true,
launchTimeout: 30000,
},
};
1 change: 0 additions & 1 deletion packages/configs/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {
collectCoverage: true,
coveragePathIgnorePatterns: ['./node_modules', './.next'],
testPathIgnorePatterns: ['./node_modules'],
transform: {
Expand Down
6 changes: 6 additions & 0 deletions packages/configs/jest.config.puppeteer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const config = require('./jest.config');
module.exports = {
...config,
preset: 'jest-puppeteer',
coveragePathIgnorePatterns: config.coveragePathIgnorePatterns.concat('./jest-puppeteer.config.js'),
};
2 changes: 1 addition & 1 deletion packages/configs/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "next-redux-wrapper-configs",
"private": true,
"version": "5.0.0"
"version": "6.0.0"
}
2 changes: 2 additions & 0 deletions packages/demo-page/jest-puppeteer.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = require('next-redux-wrapper-configs/jest-puppeteer.config');
module.exports.server.port = 4000;
1 change: 1 addition & 0 deletions packages/demo-page/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('next-redux-wrapper-configs/jest.config.puppeteer');
2 changes: 2 additions & 0 deletions packages/demo-page/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
49 changes: 49 additions & 0 deletions packages/demo-page/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "next-redux-wrapper-demo-page",
"private": true,
"version": "6.0.0",
"description": "Demo of redux wrapper for Next.js",
"scripts": {
"clean": "rimraf .next coverage",
"test": "jest",
"start": "next --port=4000",
"build": "next build",
"serve": "next start --port=4000"
},
"dependencies": {
"jsondiffpatch": "0.4.1",
"next-redux-wrapper": "^6.0.0",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-redux": "7.2.0",
"redux": "4.0.5"
},
"devDependencies": {
"@types/expect-puppeteer": "4.4.0",
"@types/jest": "25.1.4",
"@types/jest-environment-puppeteer": "4.3.1",
"@types/puppeteer": "2.0.1",
"@types/react": "16.9.31",
"@types/react-dom": "16.9.6",
"@types/react-redux": "7.1.7",
"@types/webpack-env": "1.15.1",
"jest": "25.2.4",
"jest-puppeteer": "4.4.0",
"next": "9.3.4",
"next-redux-wrapper-configs": "^6.0.0",
"puppeteer": "2.1.1",
"rimraf": "3.0.2",
"ts-jest": "25.3.0",
"typescript": "3.8.3"
},
"author": "Kirill Konshin",
"repository": {
"type": "git",
"url": "git://github.com/kirill-konshin/next-redux-wrapper.git"
},
"bugs": {
"url": "https://github.com/kirill-konshin/next-redux-wrapper/issues"
},
"homepage": "https://github.com/kirill-konshin/next-redux-wrapper",
"license": "MIT"
}
29 changes: 29 additions & 0 deletions packages/demo-page/src/components/reducer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {AnyAction} from 'redux';
import {HYDRATE} from 'next-redux-wrapper';
import {diff} from 'jsondiffpatch';

export interface State {
page: string;
}

const reducer = (state: State = {page: 'init'}, action: AnyAction) => {
switch (action.type) {
case HYDRATE:
const stateDiff = diff(state, action.payload) as any;
const wasBumpedOnClient = stateDiff.page[0].endsWith('X');
console.log('HYDRATE action handler', {stateDiff, wasBumpedOnClient});
return {
...state,
...action.payload,
page: wasBumpedOnClient ? state.page : action.payload.page,
};
case 'PAGE':
return {...state, page: action.payload};
case 'BUMP':
return {...state, page: state.page + 'X'};
default:
return state;
}
};

export default reducer;
18 changes: 18 additions & 0 deletions packages/demo-page/src/components/store.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {createStore} from 'redux';
import {MakeStore, createWrapper, Context} from 'next-redux-wrapper';
import reducer, {State} from './reducer';

export const makeStore: MakeStore<State> = (context: Context) => {
const store = createStore(reducer);

if (module.hot) {
module.hot.accept('./reducer', () => {
console.log('Replacing reducer');
store.replaceReducer(require('./reducer').default);
});
}

return store;
};

export const wrapper = createWrapper<State>(makeStore, {debug: true});
30 changes: 30 additions & 0 deletions packages/demo-page/src/pages/_error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, {Component} from 'react';
import Link from 'next/link';
import {connect} from 'react-redux';
import {wrapper} from '../components/store';
import {State} from '../components/reducer';

class ErrorPage extends Component<State> {
public static getInitialProps = wrapper.getInitialPageProps(({store, pathname}) => {
console.log('2. Page.getInitialProps uses the store to dispatch things');
store.dispatch({type: 'PAGE', payload: 'was set in error page ' + pathname});
});

render() {
const {page} = this.props;
return (
<>
<p>
This is an error page, it also has access to store: <code>{page}</code>
</p>
<nav>
<Link href="/">
<a>Navigate to index</a>
</Link>
</nav>
</>
);
}
}

export default wrapper.withRedux(connect((state: State) => state)(ErrorPage));
35 changes: 35 additions & 0 deletions packages/demo-page/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import {NextPage} from 'next';
import Link from 'next/link';
import {useSelector} from 'react-redux';
import {wrapper} from '../components/store';
import {State} from '../components/reducer';

export interface ConnectedPageProps {
custom: string;
}

// Page itself is not connected to Redux Store, it has to render Provider to allow child components to connect to Redux Store
const Page: NextPage<ConnectedPageProps> = ({custom}) => {
const {page} = useSelector<State, State>(state => state);
return (
<div className="index">
<pre>{JSON.stringify({page, custom}, null, 2)}</pre>
<Link href="/other">
<a>Navigate</a>
</Link>
{' | '}
<Link href="/error">
<a>Navigate to error</a>
</Link>
</div>
);
};

export const getServerSideProps = wrapper.getServerSideProps(({store, req}) => {
console.log('2. Page.getServerSideProps uses the store to dispatch things');
store.dispatch({type: 'PAGE', payload: 'was set in index page ' + req.url});
return {props: {custom: 'custom'}};
});

export default wrapper.withRedux(Page);
31 changes: 31 additions & 0 deletions packages/demo-page/src/pages/other.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import {NextPage} from 'next';
import Link from 'next/link';
import {useSelector, useDispatch} from 'react-redux';
import {State} from '../components/reducer';
import {wrapper} from '../components/store';

export const getStaticProps = wrapper.getStaticProps(({store, previewData}) => {
console.log('2. Page.getStaticProps uses the store to dispatch things');
store.dispatch({type: 'PAGE', payload: 'was set in other page ' + JSON.stringify({previewData})});
});

const OtherPage: NextPage<State> = () => {
const {page} = useSelector<State, State>(state => state);
const dispatch = useDispatch();
const bump = () => dispatch({type: 'BUMP'});
return (
<div className="other">
<p>Using Next.js default prop in a wrapped component.</p>
<pre>{JSON.stringify({page}, null, 2)}</pre>
<nav>
<button onClick={bump}>bump</button>
<Link href="/">
<a>Navigate to index</a>
</Link>
</nav>
</div>
);
};

export default wrapper.withRedux(OtherPage);
29 changes: 29 additions & 0 deletions packages/demo-page/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import config from '../jest-puppeteer.config';

const openPage = (url = '/') => page.goto(`http://localhost:${config.server.port}${url}`);

describe('Basic integration', () => {
it('shows the page', async () => {
await openPage();

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

await expect(page).toMatch('"page": "was set in index page /"');
await expect(page).toMatch('"custom": "custom"');
});

it('clicks the button', async () => {
await openPage('/other');

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

await expect(page).toMatch('"page": "was set in other page {}"');

await expect(page).toClick('a', {text: 'Navigate to index'});

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

await expect(page).toMatch('"page": "was set in index page /"');
await expect(page).toMatch('"custom": "custom"');
});
});
20 changes: 20 additions & 0 deletions packages/demo-page/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "../configs/tsconfig.json",
"include": [
"tests",
"src/pages"
],
"compilerOptions": {
"allowJs": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"module": "esnext",
"outDir": "lib",
"isolatedModules": true,
"jsx": "preserve"
},
"exclude": [
"node_modules"
]
}
2 changes: 2 additions & 0 deletions packages/demo-saga/jest-puppeteer.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = require('next-redux-wrapper-configs/jest-puppeteer.config');
module.exports.server.port = 5000;
1 change: 1 addition & 0 deletions packages/demo-saga/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('next-redux-wrapper-configs/jest.config.puppeteer');
2 changes: 2 additions & 0 deletions packages/demo-saga/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
Loading

0 comments on commit 2eca1a8

Please sign in to comment.