'var in local state sessionStorage shows as null in jest enzyme test case
I am testing the component as below, you can see the local state has selectedOffer from sessionStorage. which errors in console if you look at the last couple lines in the test case. How to set the test up so to be able to test this component?
UPDATE: so I commented out the line that errors, please scroll to bottom to see my update.
the component:
class PaymentForm extends React.Component {
constructor(props) {
super(props);
this.state = {
user: JSON.parse(getCurrentUser()),
selectedOffer: JSON.parse(sessionStorage.getItem('selectedOffer')),
};
render() {
const selectedOffer = JSON.parse(sessionStorage.getItem('selectedOffer'));
return (
<div>
<p>${selectedOffer.price} USD, billed </p>
<Tabs/>
</div>
)
}
}
const mapStateToProps = state => ({
isResubscribe: state.appState.isResubscribe,
});
export default withRouter(connect(mapStateToProps)(PaymentForm));
my test case:
describe('PaymentForm', function () {
const baseProps = {
partnerId: 'abc',
};
let store;
beforeAll(() => {
store = configureStore({
reducer: rootReducer,
});
});
it('renders Tabscomponent if connected to the redux store', () => {
const wrapper = mount(
<BrowserRouter>
<Provider store={store}>
<PaymentForm {...baseProps} />
</Provider>
</BrowserRouter>
);
expect(wrapper.find('Tabs')).toHaveLength(3);
});
});
Console errors: TypeError: Cannot read property 'price' of null
599 | )
600 | }
> 601 | ${selectedOffer.price} USD, billed
UPDATE:
After commented the line errors in above test case, Whats interesting is the test first did run the checking regarding rendered child component. and I had it correct. and then the console errors as below: Anyone has any idea of whats going on?
TypeError: Cannot read property 'length' of undefined
7 | export const createBraintreeInstance = () => {
8 | return new Promise((resolve, reject) => {
> 9 | Parse.Cloud.run('GetClientToken', {})
| ^
10 | .then((authorization) => {
11 | braintree.client.create({
12 | authorization: authorization
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
