-
Notifications
You must be signed in to change notification settings - Fork 18
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/connect-wallet/src/middleware/loggerMiddleware.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
packages/connect-wallet/src/middleware/loggerMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ——'); | ||
} | ||
|
||
return returnedValue; | ||
}; | ||
|
||
export default logger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.