-
Notifications
You must be signed in to change notification settings - Fork 146
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
Replace isomorphic jest mocks with msw handlers #944
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
49d1a79
replace most manual mocks
yunakim714 b6ed666
more replacements
yunakim714 6f001df
more replacements in addresses test
yunakim714 0913bc9
lint
yunakim714 86af04c
resolve flaky cart test
yunakim714 3c07e7e
remove some jest mocks
bfeister b76bc32
replace all isomorphic mocks
yunakim714 310c5fa
cleanup
yunakim714 6b8242f
fix auth modal mocks
yunakim714 aab758a
remove timers from auth hooks test, fix password test
yunakim714 88f48ec
remove timer from create account test
yunakim714 6b1ee3c
add timeout
yunakim714 ce9f9f8
cleanup
yunakim714 93ec4be
Merge branch 'develop' into replace-isomorphic-jest-mocks
yunakim714 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,17 @@ import {renderWithProviders, createPathWithDefaults} from '../utils/test-utils' | |
import {AuthModal, useAuthModal} from './use-auth-modal' | ||
import {BrowserRouter as Router, Route} from 'react-router-dom' | ||
import Account from '../pages/account' | ||
import {rest} from 'msw' | ||
|
||
jest.mock('../commerce-api/einstein') | ||
|
||
const mockPasswordToken = { | ||
email: '[email protected]', | ||
expiresInMinutes: 10, | ||
login: '[email protected]', | ||
resetToken: 'testresettoken' | ||
} | ||
|
||
const mockRegisteredCustomer = { | ||
authType: 'registered', | ||
customerId: 'registeredCustomerId', | ||
|
@@ -26,75 +34,15 @@ const mockRegisteredCustomer = { | |
} | ||
|
||
const mockLogin = jest.fn() | ||
jest.useFakeTimers() | ||
|
||
jest.mock('../commerce-api/auth', () => { | ||
return jest.fn().mockImplementation(() => { | ||
return { | ||
login: mockLogin.mockImplementation(async () => { | ||
throw new Error('invalid credentials') | ||
}), | ||
getLoggedInToken: jest.fn().mockImplementation(async () => { | ||
return {customer_id: 'mockcustomerid'} | ||
}) | ||
login: mockLogin | ||
} | ||
}) | ||
}) | ||
|
||
jest.mock('commerce-sdk-isomorphic', () => { | ||
const sdk = jest.requireActual('commerce-sdk-isomorphic') | ||
return { | ||
...sdk, | ||
ShopperLogin: class ShopperLoginMock extends sdk.ShopperLogin { | ||
async getAccessToken() { | ||
return { | ||
access_token: 'accesstoken', | ||
refresh_token: 'refreshtoken', | ||
customer_id: 'customerId' | ||
} | ||
} | ||
authenticateCustomer() { | ||
return {url: '/callback'} | ||
} | ||
}, | ||
ShopperCustomers: class ShopperCustomersMock extends sdk.ShopperCustomers { | ||
async registerCustomer() { | ||
return mockRegisteredCustomer | ||
} | ||
async getCustomer(args) { | ||
if (args.parameters.customerId === 'customerid') { | ||
return { | ||
authType: 'guest', | ||
customerId: 'customerid' | ||
} | ||
} | ||
return mockRegisteredCustomer | ||
} | ||
async authorizeCustomer() { | ||
return { | ||
headers: { | ||
get(key) { | ||
return {authorization: 'guestToken'}[key] | ||
} | ||
}, | ||
json: async () => ({ | ||
authType: 'guest', | ||
customerId: 'customerid' | ||
}) | ||
} | ||
} | ||
async getResetPasswordToken() { | ||
return { | ||
email: '[email protected]', | ||
expiresInMinutes: 10, | ||
login: '[email protected]', | ||
resetToken: 'testresettoken' | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
jest.mock('../commerce-api/utils', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you forget to remove this? |
||
const originalModule = jest.requireActual('../commerce-api/utils') | ||
return { | ||
|
@@ -141,13 +89,21 @@ MockedComponent.propTypes = { | |
// Set up and clean up | ||
beforeEach(() => { | ||
authModal = undefined | ||
jest.useFakeTimers() | ||
global.server.use( | ||
rest.post('*/customers', (req, res, ctx) => { | ||
return res(ctx.delay(0), ctx.status(200), ctx.json(mockRegisteredCustomer)) | ||
}), | ||
rest.get('*/customers/:customerId', (req, res, ctx) => { | ||
return res(ctx.delay(0), ctx.status(200), ctx.json(mockRegisteredCustomer)) | ||
}), | ||
rest.post('*/customers/password/actions/create-reset-token', (req, res, ctx) => { | ||
return res(ctx.delay(0), ctx.status(200), ctx.json(mockPasswordToken)) | ||
}) | ||
) | ||
}) | ||
afterEach(() => { | ||
localStorage.clear() | ||
jest.resetModules() | ||
jest.runOnlyPendingTimers() | ||
jest.useRealTimers() | ||
}) | ||
|
||
test('Renders login modal by default', async () => { | ||
|
@@ -166,9 +122,8 @@ test('Renders login modal by default', async () => { | |
|
||
test('Allows customer to sign in to their account', async () => { | ||
mockLogin.mockImplementationOnce(async () => { | ||
return {url: '/callback'} | ||
return {url: '/callback', customerId: 'registeredCustomerId'} | ||
}) | ||
|
||
// render our test component | ||
renderWithProviders(<MockedComponent />) | ||
|
||
|
@@ -180,6 +135,7 @@ test('Allows customer to sign in to their account', async () => { | |
user.type(screen.getByLabelText('Email'), '[email protected]') | ||
user.type(screen.getByLabelText('Password'), 'Password!1') | ||
user.click(screen.getByText(/sign in/i)) | ||
|
||
// wait for successful toast to appear | ||
await waitFor(() => { | ||
expect(screen.getByText(/Welcome Tester/i)).toBeInTheDocument() | ||
|
@@ -188,6 +144,10 @@ test('Allows customer to sign in to their account', async () => { | |
}) | ||
|
||
test('Renders error when given incorrect log in credentials', async () => { | ||
mockLogin.mockImplementationOnce(async () => { | ||
throw new Error('invalid credentials') | ||
}) | ||
|
||
// render our test component | ||
renderWithProviders(<MockedComponent />) | ||
|
||
|
@@ -208,22 +168,23 @@ test('Renders error when given incorrect log in credentials', async () => { | |
|
||
test('Allows customer to generate password token', async () => { | ||
// render our test component | ||
renderWithProviders(<MockedComponent />) | ||
renderWithProviders(<MockedComponent initialView="password" />) | ||
|
||
// open the modal | ||
const trigger = screen.getByText(/open modal/i) | ||
user.click(trigger) | ||
|
||
// switch to 'reset password' view | ||
user.click(screen.getByText(/forgot password/i)) | ||
expect(authModal.isOpen).toBe(true) | ||
|
||
// enter credentials and submit | ||
user.type(screen.getByLabelText('Email'), '[email protected]') | ||
user.click(within(screen.getByTestId('sf-auth-modal-form')).getByText(/reset password/i)) | ||
const withinForm = within(screen.getByTestId('sf-auth-modal-form')) | ||
user.type(withinForm.getByLabelText('Email'), '[email protected]') | ||
user.click(withinForm.getByText(/reset password/i)) | ||
|
||
// wait for success state | ||
expect(await screen.findByText(/password reset/i)).toBeInTheDocument() | ||
expect(screen.getByText(/[email protected]/i)).toBeInTheDocument() | ||
await waitFor(() => { | ||
expect(screen.getByText(/password reset/i)).toBeInTheDocument() | ||
expect(screen.getByText(/[email protected]/i)).toBeInTheDocument() | ||
}) | ||
}) | ||
|
||
test('Allows customer to open generate password token modal from everywhere', () => { | ||
|
@@ -248,10 +209,10 @@ test('Allows customer to open generate password token modal from everywhere', () | |
}) | ||
|
||
test('Allows customer to create an account', async () => { | ||
jest.setTimeout(30000) | ||
mockLogin.mockImplementationOnce(async () => { | ||
return {url: '/callback'} | ||
return {url: '/callback', customerId: 'registeredCustomerId'} | ||
}) | ||
|
||
// render our test component | ||
renderWithProviders(<MockedComponent />) | ||
|
||
|
@@ -272,6 +233,7 @@ test('Allows customer to create an account', async () => { | |
user.paste(withinForm.getAllByLabelText(/password/i)[0], 'Password!1') | ||
user.click(withinForm.getByText(/create account/i)) | ||
|
||
// wait for redirecting to account page | ||
expect(await screen.findByText(/welcome tester/i, {}, {timeout: 30000})).toBeInTheDocument() | ||
await waitFor(() => { | ||
expect(screen.getAllByText(/welcome tester/i).length).toEqual(2) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,29 +16,6 @@ let mockCustomer = {} | |
|
||
jest.setTimeout(30000) | ||
|
||
jest.mock('commerce-sdk-isomorphic', () => { | ||
const sdk = jest.requireActual('commerce-sdk-isomorphic') | ||
return { | ||
...sdk, | ||
ShopperCustomers: class ShopperCustomersMock extends sdk.ShopperCustomers { | ||
async createCustomerAddress(address) { | ||
mockCustomer.addresses = [address.body] | ||
return {} | ||
} | ||
|
||
async updateCustomerAddress(address) { | ||
mockCustomer.addresses[0] = address.body | ||
return {} | ||
} | ||
|
||
async removeCustomerAddress() { | ||
mockCustomer.addresses = undefined | ||
return {} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
const mockToastSpy = jest.fn() | ||
jest.mock('@chakra-ui/toast', () => { | ||
return { | ||
|
@@ -71,17 +48,29 @@ beforeEach(() => { | |
lastName: 'Keane', | ||
login: '[email protected]' | ||
} | ||
global.server.use( | ||
rest.get('*/customers/:customerId', (req, res, ctx) => { | ||
return res(ctx.delay(0), ctx.json(mockCustomer)) | ||
}), | ||
rest.post('*/customers/:customerId/addresses', (req, res, ctx) => { | ||
mockCustomer.addresses = [req.body] | ||
return res(ctx.delay(0), ctx.status(200), ctx.json(req.body)) | ||
}), | ||
rest.patch('*/customers/:customerId/addresses/:addressName', (req, res, ctx) => { | ||
mockCustomer.addresses[0] = req.body | ||
return res(ctx.delay(0), ctx.status(200), ctx.json(req.body)) | ||
}), | ||
rest.delete('*/customers/:customerId/addresses/:addressName', (req, res, ctx) => { | ||
mockCustomer.addresses = undefined | ||
return res(ctx.delay(0), ctx.status(200)) | ||
}) | ||
) | ||
}) | ||
afterEach(() => { | ||
localStorage.clear() | ||
}) | ||
|
||
test('Allows customer to add/edit/remove addresses', async () => { | ||
global.server.use( | ||
rest.get('*/customers/:customerId', (req, res, ctx) => { | ||
return res(ctx.delay(0), ctx.json(mockCustomer)) | ||
}) | ||
) | ||
renderWithProviders(<MockedComponent />) | ||
await waitFor(() => expect(screen.getByText('registeredCustomerId')).toBeInTheDocument()) | ||
|
||
|
Oops, something went wrong.
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.
👏