Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

➖ Removes redux-logger and makes use of custom logger middleware #47

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/proud-mangos-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/connect-wallet': patch
---

Removes redux-logger to address an ESM based-packed issue with webpack bundlers.
2 changes: 0 additions & 2 deletions packages/connect-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"react-redux": "^8.0.5",
"react-use-measure": "^2.1.1",
"redux": "^5.0.0-alpha.2",
"redux-logger": "^3.0.6",
"redux-persist": "^6.0.0",
"siwe": "^1.1.6",
"ua-parser-js": "^1.0.32"
Expand All @@ -59,7 +58,6 @@
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^13.4.0",
"@types/qrcode": "^1.5.0",
"@types/redux-logger": "^3.0.9",
"@types/ua-parser-js": "^0.7.36",
"@wagmi/core": "0.8.10",
"happy-dom": "^8.7.1",
Expand Down
99 changes: 99 additions & 0 deletions packages/connect-wallet/src/middleware/loggerMiddleware.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {vi} from 'vitest';

import {addWallet, setPendingWallet} from '../slices/walletSlice';
import {preloadedState, storeWithLogger as store} from '../test/configureStore';
import {ALTERNATE_WALLET, DEFAULT_WALLET} from '../test/fixtures/wallet';

const addedWalletState = {
...preloadedState,
wallet: {
...preloadedState.wallet,
activeWallet: DEFAULT_WALLET,
connectedWallets: [DEFAULT_WALLET],
},
};

const pendingAltWalletState = {
...addedWalletState,
wallet: {
...addedWalletState.wallet,
pendingWallet: ALTERNATE_WALLET,
},
};

const addedTwoWalletsWithPendingWalletState = {
...pendingAltWalletState,
wallet: {
...pendingAltWalletState.wallet,
activeWallet: ALTERNATE_WALLET,
connectedWallets: [DEFAULT_WALLET, ALTERNATE_WALLET],
},
};

const addedTwoWalletsState = {
...addedTwoWalletsWithPendingWalletState,
wallet: {
...addedTwoWalletsWithPendingWalletState.wallet,
activeWallet: ALTERNATE_WALLET,
connectedWallets: [DEFAULT_WALLET, ALTERNATE_WALLET],
pendingWallet: undefined,
},
};

describe('loggerMiddleware', () => {
const spyLog = vi.spyOn(console, 'log');

it('With multiple actions being dispatched', () => {
const steps = [
{
action: addWallet,
payload: DEFAULT_WALLET,
expectedPrevState: preloadedState,
expectedNextState: addedWalletState,
},
{
action: setPendingWallet,
payload: ALTERNATE_WALLET,
expectedPrevState: addedWalletState,
expectedNextState: pendingAltWalletState,
},
{
action: addWallet,
payload: ALTERNATE_WALLET,
expectedPrevState: pendingAltWalletState,
expectedNextState: addedTwoWalletsWithPendingWalletState,
},
{
action: setPendingWallet,
payload: undefined,
expectedPrevState: addedTwoWalletsWithPendingWalletState,
expectedNextState: addedTwoWalletsState,
},
];

steps.forEach(({action, payload, expectedNextState, expectedPrevState}) => {
vi.clearAllMocks();
store.dispatch(action(payload as any));

// Test the first message logged
expect(spyLog.mock.calls[0][0]).toContain(
`@shopify/connect-wallet%c: ${action.type}`,
);

// Test the previous state
expect(spyLog.mock.calls[1][0]).toContain('previous state:');
expect(spyLog.mock.calls[1][2]).toEqual(expectedPrevState);

// Test the action and payload
expect(spyLog.mock.calls[2][0]).toContain('action:');
expect(spyLog.mock.calls[2][2]).toEqual({
type: action.type,
payload,
});

// Test the next state
expect(spyLog.mock.calls[3][0]).toContain('next state:');
expect(spyLog.mock.calls[3][2]).toEqual(expectedNextState);
});
});
});
52 changes: 52 additions & 0 deletions packages/connect-wallet/src/middleware/loggerMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-disable no-console */
import type {Middleware} from 'redux';

import type {AppDispatch, RootState} from '../store/configureStore';

// Different CSS styles for the logger.
const ACTION_CSS = `color: #3B82F6; font-weight: bold`;
const HEADER_CSS = ['color: #059669;', 'color: gray; font-weight: lighter;'];
const NEXT_STATE_CSS = `color: #059669; font-weight: bold`;
const PREV_STATE_CSS = `color: #8B5CF6; font-weight: bold`;

const logger: Middleware<AppDispatch, RootState> =
({getState}) =>
(next) =>
(action) => {
// Timestamp
const date = new Date();
const formattedTimestamp = date.toLocaleTimeString('default', {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
});

// Previous State
const previousState = getState();

// Ensure we return the event so that the store actually receives the dispatched event.
const returnedValue = next(action);

// Get our next state after the action has been dispatched to our store.
const nextState = getState();

// Begin logging
console.groupCollapsed(
`%c@shopify/connect-wallet%c: ${action.type} @${formattedTimestamp}`,
...HEADER_CSS,
);

console.log('%cprevious state:', PREV_STATE_CSS, previousState);
console.log('%caction:', ACTION_CSS, action);
console.log('%cnext state:', NEXT_STATE_CSS, nextState);

try {
console.groupEnd();
} catch (error) {
console.log('—— log end ——');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 this looks much simpler than what was there before. I'm surprised that the tests didn't break.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha what makes you think the tests would've broken? Previously we were just pushing logs into an array that would only ever have a single entry (from what I can tell), so it makes sense to me that it wouldn't have broken.


return returnedValue;
};

export default logger;
2 changes: 1 addition & 1 deletion packages/connect-wallet/src/store/configureStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {logger} from 'redux-logger';
import {configureStore} from '@reduxjs/toolkit';
import {
FLUSH,
Expand All @@ -10,6 +9,7 @@ import {
REHYDRATE,
} from 'redux-persist';

import logger from '../middleware/loggerMiddleware';
import {initialState} from '../slices/walletSlice';

import {rootReducer} from './combineReducers';
Expand Down
26 changes: 21 additions & 5 deletions packages/connect-wallet/src/test/configureStore.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// import {logger} from 'redux-logger';
import {configureStore} from '@reduxjs/toolkit';

import logger from '../middleware/loggerMiddleware';
import {initialState} from '../slices/walletSlice';
import {rootReducer} from '../store/combineReducers';
import {listenerMiddleware} from '../store/listenerMiddleware';

const preloadedState = {
export const preloadedState = {
wallet: initialState,
};

Expand All @@ -23,9 +23,25 @@ export const store = configureStore({
listenerMiddleware.middleware,
);

// Uncomment the following line if you need to enable logging
// during test development.
// middlewares.push(logger);
return middlewares;
},
});

export const storeWithLogger = configureStore({
reducer: rootReducer,
preloadedState,
middleware: (getDefaultMiddleware) => {
/**
* We prepend the listenerMiddleware here based on the comments
* in the middleware sample on the RTK docus.
*
* https://redux-toolkit.js.org/api/createListenerMiddleware#middleware
*/
const middlewares = getDefaultMiddleware().prepend(
listenerMiddleware.middleware,
);

middlewares.push(logger);

return middlewares;
},
Expand Down
26 changes: 0 additions & 26 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4318,13 +4318,6 @@
"@types/scheduler" "*"
csstype "^3.0.2"

"@types/redux-logger@^3.0.9":
version "3.0.9"
resolved "https://registry.yarnpkg.com/@types/redux-logger/-/redux-logger-3.0.9.tgz#9193b3d51bb6ab98d25514ba7764e4f98a64d3ec"
integrity sha512-cwYhVbYNgH01aepeMwhd0ABX6fhVB2rcQ9m80u8Fl50ZODhsZ8RhQArnLTkE7/Zrfq4Sz/taNoF7DQy9pCZSKg==
dependencies:
redux "^4.0.0"

"@types/scheduler@*":
version "0.16.2"
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.2.tgz#1a62f89525723dde24ba1b01b092bf5df8ad4d39"
Expand Down Expand Up @@ -7385,11 +7378,6 @@ dedent@^0.7.0:
resolved "https://registry.yarnpkg.com/dedent/-/dedent-0.7.0.tgz#2495ddbaf6eb874abb0e1be9df22d2e5a544326c"
integrity sha512-Q6fKUPqnAHAyhiUgFU7BUzLiv0kd8saH9al7tnu5Q/okj6dnupxyTgFIBjVzJATdfIAm9NAsvXNzjaKa+bxVyA==

deep-diff@^0.3.5:
version "0.3.8"
resolved "https://registry.yarnpkg.com/deep-diff/-/deep-diff-0.3.8.tgz#c01de63efb0eec9798801d40c7e0dae25b582c84"
integrity sha512-yVn6RZmHiGnxRKR9sJb3iVV2XTF1Ghh2DiWRZ3dMnGc43yUdWWF/kX6lQyk3+P84iprfWKU/8zFTrlkvtFm1ug==

deep-eql@^4.1.2:
version "4.1.3"
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-4.1.3.tgz#7c7775513092f7df98d8df9996dd085eb668cc6d"
Expand Down Expand Up @@ -13295,13 +13283,6 @@ redent@^3.0.0:
indent-string "^4.0.0"
strip-indent "^3.0.0"

redux-logger@^3.0.6:
version "3.0.6"
resolved "https://registry.yarnpkg.com/redux-logger/-/redux-logger-3.0.6.tgz#f7555966f3098f3c88604c449cf0baf5778274bf"
integrity sha512-JoCIok7bg/XpqA1JqCqXFypuqBbQzGQySrhFzewB7ThcnysTO30l4VCst86AuB9T9tuT03MAA56Jw2PNhRSNCg==
dependencies:
deep-diff "^0.3.5"

redux-persist@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/redux-persist/-/redux-persist-6.0.0.tgz#b4d2972f9859597c130d40d4b146fecdab51b3a8"
Expand All @@ -13317,13 +13298,6 @@ redux@5.0.0-alpha.2, redux@^5.0.0-alpha.2:
resolved "https://registry.yarnpkg.com/redux/-/redux-5.0.0-alpha.2.tgz#73f9da989cd13cc685adc56857647584a278727e"
integrity sha512-elxHLhB3SFrwq0N9f4P/jC25JqmPcilnI4NnvZvUOOrSTc2dIl9w10wgc4m1OGEPXdumqOGlpykQeD1rhEwIjg==

redux@^4.0.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/redux/-/redux-4.2.0.tgz#46f10d6e29b6666df758780437651eeb2b969f13"
integrity sha512-oSBmcKKIuIR4ME29/AeNUnl5L+hvBq7OaJWzaptTQJAntaPvxIJqfnjbaEiCzzaIz+XmVILfqAM3Ob0aXLPfjA==
dependencies:
"@babel/runtime" "^7.9.2"

regenerate-unicode-properties@^10.1.0:
version "10.1.0"
resolved "https://registry.yarnpkg.com/regenerate-unicode-properties/-/regenerate-unicode-properties-10.1.0.tgz#7c3192cab6dd24e21cb4461e5ddd7dd24fa8374c"
Expand Down