it('dispatches fetchUserAccount and fetchUserNotifications actions if userId changes', () => { const userId = 45; const { params, store } = signInUserWithUserId(userId); const dispatchSpy = sinon.spy(store, 'dispatch'); const errorHandler = createStubErrorHandler(); const root = renderUserProfileEdit({ errorHandler, params, store }); dispatchSpy.resetHistory(); // We set `user` to `null` because that's what `mapStateToProps()` would do // because the user is not loaded yet. const newUserId = userId + 9999; root.setProps({ userId: newUserId, user: null }); sinon.assert.callCount(dispatchSpy, 2); sinon.assert.calledWith( dispatchSpy, fetchUserAccount({ errorHandlerId: errorHandler.id, userId: newUserId, }), ); sinon.assert.calledWith( dispatchSpy, fetchUserNotifications({ errorHandlerId: errorHandler.id, userId: newUserId, }), ); expect(root.find('.UserProfileEdit-location')).toHaveProp('value', ''); });
componentWillReceiveProps({ isOwner, location: newLocation, match: { params: newParams }, reviews, user, }: InternalProps) { const { dispatch, errorHandler, location: oldLocation, match: { params: oldParams }, } = this.props; if (oldParams.username !== newParams.username) { dispatch( fetchUserAccount({ errorHandlerId: errorHandler.id, username: newParams.username, }), ); } else if ( user && isOwner && (oldLocation.query.page !== newLocation.query.page || !reviews) ) { dispatch( fetchUserReviews({ errorHandlerId: errorHandler.id, page: this.getReviewsPage(newLocation), userId: user.id, }), ); } }
constructor(props: InternalProps) { super(props); const { dispatch, errorHandler, isOwner, location, match: { params }, reviews, user, } = props; if (errorHandler.hasError()) { log.warn('Not loading data because of an error.'); return; } if (!user) { dispatch( fetchUserAccount({ errorHandlerId: errorHandler.id, username: params.username, }), ); } else if (isOwner && !reviews) { dispatch( fetchUserReviews({ errorHandlerId: errorHandler.id, page: this.getReviewsPage(location), userId: user.id, }), ); } }
it('dispatches fetchUserAccount action if userId is not found', () => { const { store } = signInUserWithUserId(100); const dispatchSpy = sinon.spy(store, 'dispatch'); const userId = 200; const root = renderUserProfile({ params: { userId }, store }); sinon.assert.calledWith( dispatchSpy, fetchUserAccount({ errorHandlerId: root.instance().props.errorHandler.id, userId, }), ); });
it('dispatches an action to fetch a user profile by username', () => { const { store } = dispatchClientMetadata(); const dispatchSpy = sinon.spy(store, 'dispatch'); const userId = 'this-is-a-username'; const root = renderUserProfile({ params: { userId }, store }); sinon.assert.calledWith( dispatchSpy, fetchUserAccount({ errorHandlerId: root.instance().props.errorHandler.id, userId, }), ); });
it('dispatches an error', async () => { const error = new Error('a bad API error'); mockApi.expects('userAccount').returns(Promise.reject(error)); sagaTester.dispatch( fetchUserAccount({ errorHandlerId: errorHandler.id, userId: 123, }), ); const errorAction = errorHandler.createErrorAction(error); await sagaTester.waitFor(errorAction.type); expect(sagaTester.getCalledActions()[2]).toEqual(errorAction); });
it('dispatches fetchUserAccount action if userId param changes', () => { const { params, store } = signInUserWithUserId(100); const dispatchSpy = sinon.spy(store, 'dispatch'); const root = renderUserProfile({ params, store }); dispatchSpy.resetHistory(); const userId = 200; root.setProps({ match: { params: { userId } } }); sinon.assert.calledWith( dispatchSpy, fetchUserAccount({ errorHandlerId: root.instance().props.errorHandler.id, userId, }), ); });
it('calls the API to fetch user', async () => { const user = createUserAccountResponse(); mockApi .expects('userAccount') .once() .returns(Promise.resolve(user)); sagaTester.dispatch( fetchUserAccount({ errorHandlerId: errorHandler.id, userId: 123, }), ); const expectedCalledAction = loadUserAccount({ user }); await sagaTester.waitFor(expectedCalledAction.type); mockApi.verify(); const calledAction = sagaTester.getCalledActions()[2]; expect(calledAction).toEqual(expectedCalledAction); });