Skip to content

Commit

Permalink
feat(paypal): set payment loading for generate express token (#2461)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Jun 7, 2023
1 parent 4b5cd50 commit 4d9003f
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 2 deletions.
11 changes: 9 additions & 2 deletions libs/paypal/state/src/payment-state.module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { NgModule } from '@angular/core';
import { combineReducers } from '@ngrx/store';

import { daffComposeReducers } from '@daffodil/core/state';
import {
DaffPaymentActions,
daffPaymentProvideAvailableProcessors,
daffPaymentProvideExtraReducers,
daffPaymentReducerFactory,
DaffPaymentReducerState,
DaffPaymentStateModule,
} from '@daffodil/payment/state';
import { DAFF_PAYPAL_PAYMENT_KIND } from '@daffodil/paypal';
import { DaffPaypalExpressPaymentDriver } from '@daffodil/paypal/driver';

import { DaffPaypalActionTypes } from './actions/paypal.actions';
import {
DaffPaypalActions,
DaffPaypalActionTypes,
} from './actions/paypal.actions';
import { daffPaypalPaymentReducer } from './reducers/payment/reducer';

@NgModule({
imports: [
Expand All @@ -23,7 +30,7 @@ import { DaffPaypalActionTypes } from './actions/paypal.actions';
action: DaffPaypalActionTypes.ApplyPaymentAction,
}),
...daffPaymentProvideExtraReducers(combineReducers({
payment: daffPaymentReducerFactory([DaffPaypalActionTypes.ApplyPaymentAction]),
payment: daffComposeReducers<DaffPaymentReducerState, DaffPaypalActions | DaffPaymentActions>([daffPaymentReducerFactory([DaffPaypalActionTypes.ApplyPaymentAction]), daffPaypalPaymentReducer]),
})),
],
})
Expand Down
111 changes: 111 additions & 0 deletions libs/paypal/state/src/reducers/payment/reducer.spec.ts
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]);
});
});
});
30 changes: 30 additions & 0 deletions libs/paypal/state/src/reducers/payment/reducer.ts
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;
}
}

0 comments on commit 4d9003f

Please sign in to comment.