-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(paypal): set payment loading for generate express token (#2461)
- Loading branch information
Showing
3 changed files
with
150 additions
and
2 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
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,111 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { DaffStateError } from '@daffodil/core/state'; | ||
import { | ||
DaffPaymentReducerState, | ||
daffPaymentInitialState, | ||
} from '@daffodil/payment/state'; | ||
import { | ||
DaffPaypalExpressTokenResponse, | ||
DaffPaypalExpressTokenRequest, | ||
} from '@daffodil/paypal'; | ||
import { | ||
DaffGeneratePaypalExpressToken, | ||
DaffGeneratePaypalExpressTokenSuccess, | ||
DaffGeneratePaypalExpressTokenFailure, | ||
} from '@daffodil/paypal/state'; | ||
import { | ||
DaffPaypalExpressTokenRequestFactory, | ||
DaffPaypalExpressTokenResponseFactory, | ||
} from '@daffodil/paypal/testing'; | ||
|
||
import { daffPaypalPaymentReducer } from './reducer'; | ||
|
||
describe('@daffodil/paypal/state | daffPaypalPaymentReducer', () => { | ||
let paypalTokenResponseFactory: DaffPaypalExpressTokenResponseFactory; | ||
let paypalTokenRequestFactory: DaffPaypalExpressTokenRequestFactory; | ||
let paypalTokenResponse: DaffPaypalExpressTokenResponse; | ||
let paypalRequest: DaffPaypalExpressTokenRequest; | ||
|
||
beforeEach(() => { | ||
paypalTokenResponseFactory = TestBed.inject(DaffPaypalExpressTokenResponseFactory); | ||
paypalTokenRequestFactory = TestBed.inject(DaffPaypalExpressTokenRequestFactory); | ||
|
||
paypalTokenResponse = paypalTokenResponseFactory.create(); | ||
paypalRequest = paypalTokenRequestFactory.create(); | ||
}); | ||
|
||
describe('when an unknown action is triggered', () => { | ||
|
||
it('should return the current state', () => { | ||
const action = <any>{}; | ||
|
||
const result = daffPaypalPaymentReducer(daffPaymentInitialState, action); | ||
|
||
expect(result).toBe(daffPaymentInitialState); | ||
}); | ||
}); | ||
|
||
describe('when a GeneratePaypalExpressTokenAction is triggered', () => { | ||
let result: DaffPaymentReducerState; | ||
|
||
beforeEach(() => { | ||
const generatePaypalExpressTokenAction = new DaffGeneratePaypalExpressToken(paypalRequest); | ||
|
||
result = daffPaypalPaymentReducer(daffPaymentInitialState, generatePaypalExpressTokenAction); | ||
}); | ||
|
||
it('sets loading state to true', () => { | ||
expect(result.loading).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('when a GeneratePaypalExpressTokenSuccessAction is triggered', () => { | ||
let result: DaffPaymentReducerState; | ||
let state: DaffPaymentReducerState; | ||
|
||
beforeEach(() => { | ||
state = { | ||
...daffPaymentInitialState, | ||
loading: true, | ||
}; | ||
|
||
const generatePaypalExpressTokenSuccessAction = new DaffGeneratePaypalExpressTokenSuccess(paypalTokenResponse); | ||
result = daffPaypalPaymentReducer(state, generatePaypalExpressTokenSuccessAction); | ||
}); | ||
|
||
it('sets loading to false', () => { | ||
expect(result.loading).toEqual(false); | ||
}); | ||
|
||
it('resets errors', () => { | ||
expect(result.errors).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('when GeneratePaypalExpressTokenFailureAction is triggered', () => { | ||
const error: DaffStateError = { code: 'code', recoverable: false, message: 'error message' }; | ||
let result: DaffPaymentReducerState; | ||
let state: DaffPaymentReducerState; | ||
|
||
beforeEach(() => { | ||
state = { | ||
...daffPaymentInitialState, | ||
loading: true, | ||
errors: [{ code: 'firstErrorCode', message: 'firstErrorMessage' }], | ||
}; | ||
|
||
const generatePaypalExpressTokenFailureAction = new DaffGeneratePaypalExpressTokenFailure(error); | ||
|
||
result = daffPaypalPaymentReducer(state, generatePaypalExpressTokenFailureAction); | ||
}); | ||
|
||
it('sets loading to false', () => { | ||
expect(result.loading).toEqual(false); | ||
}); | ||
|
||
it('sets the error', () => { | ||
expect(result.errors).toEqual([error]); | ||
}); | ||
}); | ||
}); |
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,30 @@ | ||
import { | ||
daffPaymentInitialState, | ||
DaffPaymentReducerState, | ||
DaffPaymentStateReducerAdapter, | ||
} from '@daffodil/payment/state'; | ||
|
||
import { | ||
DaffPaypalActionTypes, | ||
DaffPaypalActions, | ||
} from '../../actions/paypal.actions'; | ||
|
||
export function daffPaypalPaymentReducer( | ||
state = daffPaymentInitialState, | ||
action: DaffPaypalActions, | ||
): DaffPaymentReducerState { | ||
const adapter = new DaffPaymentStateReducerAdapter(); | ||
switch (action.type) { | ||
case DaffPaypalActionTypes.GeneratePaypalExpressTokenAction: | ||
return adapter.generateToken(state); | ||
|
||
case DaffPaypalActionTypes.GeneratePaypalExpressTokenSuccessAction: | ||
return adapter.tokenGenerated(state); | ||
|
||
case DaffPaypalActionTypes.GeneratePaypalExpressTokenFailureAction: | ||
return adapter.storeError([action.payload], state); | ||
|
||
default: | ||
return state; | ||
} | ||
} |