-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent unnecessary rerenders for PlaidEmbeddedLink (#343)
- Loading branch information
1 parent
0d81131
commit e025b96
Showing
3 changed files
with
81 additions
and
4 deletions.
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,53 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { PlaidEmbeddedLink, PlaidLinkOptions } from './'; | ||
|
||
import useScript from 'react-script-hook'; | ||
jest.mock('react-script-hook'); | ||
const mockedUseScript = useScript as jest.Mock; | ||
|
||
const ScriptLoadingState = { | ||
LOADING: [true, null], | ||
LOADED: [false, null], | ||
ERROR: [false, 'SCRIPT_LOAD_ERROR'], | ||
}; | ||
|
||
describe('PlaidEmbeddedLink', () => { | ||
const config: PlaidLinkOptions = { | ||
token: 'test-token', | ||
onSuccess: jest.fn(), | ||
onExit: jest.fn(), | ||
onLoad: jest.fn(), | ||
onEvent: jest.fn(), | ||
}; | ||
const createEmbeddedSpy = jest.fn(() => ({ | ||
destroy: jest.fn(), | ||
})); | ||
|
||
beforeEach(() => { | ||
mockedUseScript.mockImplementation(() => ScriptLoadingState.LOADED); | ||
window.Plaid = { | ||
createEmbedded: createEmbeddedSpy, | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should not rerender if config did not change', () => { | ||
const styles = { height: '350px', width: '350px', backgroundColor: 'white' }; | ||
const { rerender } = render(<PlaidEmbeddedLink {...config} style={styles} />); | ||
expect(createEmbeddedSpy).toHaveBeenCalledTimes(1); | ||
rerender(<PlaidEmbeddedLink {...config} style={{...styles, backgroundColor: 'light-blue'}} />); | ||
expect(createEmbeddedSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should rerender if config did change', () => { | ||
const { rerender } = render(<PlaidEmbeddedLink {...config} />); | ||
expect(createEmbeddedSpy).toHaveBeenCalledTimes(1); | ||
const newConfig = { ...config, token: 'new-test-token' }; | ||
rerender(<PlaidEmbeddedLink {...newConfig} />); | ||
expect(createEmbeddedSpy).toHaveBeenCalledTimes(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