Skip to content

Commit

Permalink
Make authenticate() resolve to WebID, not action
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-f committed Oct 20, 2016
1 parent b7a2fe2 commit f6cc3ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ export function authenticate () {
return dispatch => {
dispatch(request())
return solid.login()
.then(webId => dispatch(success(webId)))
.catch(error => dispatch(failure(error)))
.then(webId => {
dispatch(success(webId))
return webId
})
.catch(error => {
dispatch(failure(error))
throw error
})
}
}

Expand Down
22 changes: 13 additions & 9 deletions test/actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,40 @@ describe('auth actions', () => {
return {loginSpy, InjectedActions}
}

it('dispatches a request and success action when logging in works', (done) => {
it('dispatches a request and success action when logging in works', () => {
const webId = 'https://example.com/profile/card#me'
const {loginSpy, InjectedActions} = substituteSolidLogin(() => Promise.resolve(webId))
const dispatch = action => Promise.resolve(action)
const dispatchSpy = spy(dispatch)
InjectedActions.authenticate()(dispatchSpy)
.then(() => {
return InjectedActions.authenticate()(dispatchSpy)
.then(authWebId => {
expect(dispatchSpy.calledWith({type: AUTH_REQUEST})).toBe(true)
expect(loginSpy.called).toBe(true)
expect(dispatchSpy.calledWith({
type: AUTH_SUCCESS,
webId
})).toBe(true)
done()
expect(authWebId).toEqual(webId)
})
})

it('dispatches a request and failure action when logging in fails', (done) => {
const {loginSpy, InjectedActions} = substituteSolidLogin(() => Promise.reject(new Error('oops!')))
it('dispatches a request and failure action when logging in fails', () => {
const error = new Error('oops!')
const {loginSpy, InjectedActions} = substituteSolidLogin(() => Promise.reject(error))
const dispatch = action => Promise.resolve(action)
const dispatchSpy = spy(dispatch)
InjectedActions.authenticate()(dispatchSpy)
.then(error => {
return InjectedActions.authenticate()(dispatchSpy)
.then(() => {
throw new Error('Expected promise to fail')
})
.catch(err => {
expect(dispatchSpy.calledWith({type: AUTH_REQUEST})).toBe(true)
expect(loginSpy.called).toBe(true)
expect(dispatchSpy.calledWith({
type: AUTH_FAILURE,
error
}))
done()
expect(err).toEqual(error)
})
})
})
Expand Down

0 comments on commit f6cc3ed

Please sign in to comment.