Skip to content

Commit

Permalink
feat(cart-customer): only merge if a cart ID is in storage (#2502)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Jul 13, 2023
1 parent c7b85d7 commit 11b1bb1
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 81 deletions.
264 changes: 201 additions & 63 deletions libs/cart-customer/state/src/effects/auth.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ describe('@daffodil/cart-customer/state | DaffCartCustomerAuthEffects', () => {
let driverGetSpy: jasmine.Spy<DaffCartServiceInterface['get']>;
let driverMergeSpy: jasmine.Spy<DaffCartServiceInterface['merge']>;
let driverCreateSpy: jasmine.Spy<DaffCartServiceInterface['create']>;
let getCartIdSpy: jasmine.Spy;
let getCartIdSpy: jasmine.Spy<DaffCartStorageService['getCartId']>;
let removeCartIdSpy: jasmine.Spy<DaffCartStorageService['removeCartId']>;

const throwStorageError = message => {
throw new DaffStorageServiceError(message);
Expand Down Expand Up @@ -78,7 +79,8 @@ describe('@daffodil/cart-customer/state | DaffCartCustomerAuthEffects', () => {
driverMergeSpy = spyOn(driver, 'merge');
driverCreateSpy = spyOn(driver, 'create');
getCartIdSpy = spyOn(cartStorageService, 'getCartId');
getCartIdSpy.and.returnValue(String(stubCart.id));
removeCartIdSpy = spyOn(cartStorageService, 'removeCartId');
getCartIdSpy.and.returnValue(stubCart.id);
});

it('should be created', () => {
Expand All @@ -89,28 +91,71 @@ describe('@daffodil/cart-customer/state | DaffCartCustomerAuthEffects', () => {
let expected;
const authCompleteAction = new DaffAuthLoginSuccess(null);

describe('and the call to the driver is successful', () => {
describe('and there is a cart ID in storage', () => {
beforeEach(() => {
driverMergeSpy.and.returnValue(of({
response: stubCart,
errors: [],
}));
const resolveSuccessAction = new DaffResolveCartSuccess(stubCart);
actions$ = hot('--a', { a: authCompleteAction });
expected = cold('--b', { b: resolveSuccessAction });
getCartIdSpy.and.returnValue(stubCart.id);
});

it('should dispatch a DaffResolveCartSuccess action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
describe('and the call to the driver is successful', () => {
beforeEach(() => {
driverMergeSpy.and.returnValue(of({
response: stubCart,
errors: [],
}));
const resolveSuccessAction = new DaffResolveCartSuccess(stubCart);
actions$ = hot('--a', { a: authCompleteAction });
expected = cold('--b', { b: resolveSuccessAction });
});

it('should dispatch a DaffResolveCartSuccess action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
});
});

describe('and the call to the driver fails', () => {
beforeEach(() => {
const error: DaffStateError = { code: 'code', recoverable: false, message: 'Failed to load cart' };
const response = cold('#', {}, error);
driverMergeSpy.and.returnValue(response);
actions$ = hot('--a', { a: authCompleteAction });
});

describe('and the call to the driver is successful', () => {
beforeEach(() => {
driverGetSpy.and.returnValue(of({
response: stubCart,
errors: [],
}));
const resolveSuccessAction = new DaffResolveCartSuccess(stubCart);
actions$ = hot('--a', { a: authCompleteAction });
expected = cold('--b', { b: resolveSuccessAction });
});

it('should dispatch a DaffResolveCartSuccess action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
});
});

describe('and the call to the driver fails', () => {
beforeEach(() => {
const error: DaffStateError = { code: 'code', recoverable: false, message: 'Failed to load cart' };
const response = cold('#', {}, error);
driverGetSpy.and.returnValue(response);
const resolveFailureAction = new DaffResolveCartFailure([error]);
actions$ = hot('--a', { a: authCompleteAction });
expected = cold('--b', { b: resolveFailureAction });
});

it('should dispatch a DaffResolveCartFailure action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
});
});
});
});

describe('and the call to the driver fails', () => {
describe('and there is not a cart ID in storage', () => {
beforeEach(() => {
const error: DaffStateError = { code: 'code', recoverable: false, message: 'Failed to load cart' };
const response = cold('#', {}, error);
driverMergeSpy.and.returnValue(response);
actions$ = hot('--a', { a: authCompleteAction });
getCartIdSpy.and.returnValue(null);
});

describe('and the call to the driver is successful', () => {
Expand All @@ -127,6 +172,10 @@ describe('@daffodil/cart-customer/state | DaffCartCustomerAuthEffects', () => {
it('should dispatch a DaffResolveCartSuccess action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
});

it('should not try to merge the carts', () => {
expect(driverMergeSpy).not.toHaveBeenCalled();
});
});

describe('and the call to the driver fails', () => {
Expand All @@ -146,32 +195,75 @@ describe('@daffodil/cart-customer/state | DaffCartCustomerAuthEffects', () => {
});
});

describe('when DaffAuthLoginSuccess is triggered with a token', () => {
describe('when DaffAuthRegisterSuccess is triggered with a token', () => {
let expected;
const authCompleteAction = new DaffAuthRegisterSuccess('token');

describe('and the call to the driver is successful', () => {
describe('and there is a cart ID in storage', () => {
beforeEach(() => {
driverMergeSpy.and.returnValue(of({
response: stubCart,
errors: [],
}));
const resolveSuccessAction = new DaffResolveCartSuccess(stubCart);
actions$ = hot('--a', { a: authCompleteAction });
expected = cold('--b', { b: resolveSuccessAction });
getCartIdSpy.and.returnValue(stubCart.id);
});

it('should dispatch a DaffResolveCartSuccess action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
describe('and the call to the driver is successful', () => {
beforeEach(() => {
driverMergeSpy.and.returnValue(of({
response: stubCart,
errors: [],
}));
const resolveSuccessAction = new DaffResolveCartSuccess(stubCart);
actions$ = hot('--a', { a: authCompleteAction });
expected = cold('--b', { b: resolveSuccessAction });
});

it('should dispatch a DaffResolveCartSuccess action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
});
});

describe('and the call to the driver fails', () => {
beforeEach(() => {
const error: DaffStateError = { code: 'code', recoverable: false, message: 'Failed to load cart' };
const response = cold('#', {}, error);
driverMergeSpy.and.returnValue(response);
actions$ = hot('--a', { a: authCompleteAction });
});

describe('and the call to the driver is successful', () => {
beforeEach(() => {
driverGetSpy.and.returnValue(of({
response: stubCart,
errors: [],
}));
const resolveSuccessAction = new DaffResolveCartSuccess(stubCart);
actions$ = hot('--a', { a: authCompleteAction });
expected = cold('--b', { b: resolveSuccessAction });
});

it('should dispatch a DaffResolveCartSuccess action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
});
});

describe('and the call to the driver fails', () => {
beforeEach(() => {
const error: DaffStateError = { code: 'code', recoverable: false, message: 'Failed to load cart' };
const response = cold('#', {}, error);
driverGetSpy.and.returnValue(response);
const resolveFailureAction = new DaffResolveCartFailure([error]);
actions$ = hot('--a', { a: authCompleteAction });
expected = cold('--b', { b: resolveFailureAction });
});

it('should dispatch a DaffResolveCartFailure action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
});
});
});
});

describe('and the call to the driver fails', () => {
describe('and there is not a cart ID in storage', () => {
beforeEach(() => {
const error: DaffStateError = { code: 'code', recoverable: false, message: 'Failed to load cart' };
const response = cold('#', {}, error);
driverMergeSpy.and.returnValue(response);
actions$ = hot('--a', { a: authCompleteAction });
getCartIdSpy.and.returnValue(null);
});

describe('and the call to the driver is successful', () => {
Expand All @@ -188,6 +280,10 @@ describe('@daffodil/cart-customer/state | DaffCartCustomerAuthEffects', () => {
it('should dispatch a DaffResolveCartSuccess action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
});

it('should not try to merge the carts', () => {
expect(driverMergeSpy).not.toHaveBeenCalled();
});
});

describe('and the call to the driver fails', () => {
Expand All @@ -211,28 +307,71 @@ describe('@daffodil/cart-customer/state | DaffCartCustomerAuthEffects', () => {
let expected;
const authCompleteAction = new DaffResetPasswordSuccess('token');

describe('and the call to the driver is successful', () => {
describe('and there is a cart ID in storage', () => {
beforeEach(() => {
driverMergeSpy.and.returnValue(of({
response: stubCart,
errors: [],
}));
const resolveSuccessAction = new DaffResolveCartSuccess(stubCart);
actions$ = hot('--a', { a: authCompleteAction });
expected = cold('--b', { b: resolveSuccessAction });
getCartIdSpy.and.returnValue(stubCart.id);
});

it('should dispatch a DaffResolveCartSuccess action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
describe('and the call to the driver is successful', () => {
beforeEach(() => {
driverMergeSpy.and.returnValue(of({
response: stubCart,
errors: [],
}));
const resolveSuccessAction = new DaffResolveCartSuccess(stubCart);
actions$ = hot('--a', { a: authCompleteAction });
expected = cold('--b', { b: resolveSuccessAction });
});

it('should dispatch a DaffResolveCartSuccess action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
});
});

describe('and the call to the driver fails', () => {
beforeEach(() => {
const error: DaffStateError = { code: 'code', recoverable: false, message: 'Failed to load cart' };
const response = cold('#', {}, error);
driverMergeSpy.and.returnValue(response);
actions$ = hot('--a', { a: authCompleteAction });
});

describe('and the call to the driver is successful', () => {
beforeEach(() => {
driverGetSpy.and.returnValue(of({
response: stubCart,
errors: [],
}));
const resolveSuccessAction = new DaffResolveCartSuccess(stubCart);
actions$ = hot('--a', { a: authCompleteAction });
expected = cold('--b', { b: resolveSuccessAction });
});

it('should dispatch a DaffResolveCartSuccess action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
});
});

describe('and the call to the driver fails', () => {
beforeEach(() => {
const error: DaffStateError = { code: 'code', recoverable: false, message: 'Failed to load cart' };
const response = cold('#', {}, error);
driverGetSpy.and.returnValue(response);
const resolveFailureAction = new DaffResolveCartFailure([error]);
actions$ = hot('--a', { a: authCompleteAction });
expected = cold('--b', { b: resolveFailureAction });
});

it('should dispatch a DaffResolveCartFailure action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
});
});
});
});

describe('and the call to the driver fails', () => {
describe('and there is not a cart ID in storage', () => {
beforeEach(() => {
const error: DaffStateError = { code: 'code', recoverable: false, message: 'Failed to load cart' };
const response = cold('#', {}, error);
driverMergeSpy.and.returnValue(response);
actions$ = hot('--a', { a: authCompleteAction });
getCartIdSpy.and.returnValue(null);
});

describe('and the call to the driver is successful', () => {
Expand All @@ -249,6 +388,10 @@ describe('@daffodil/cart-customer/state | DaffCartCustomerAuthEffects', () => {
it('should dispatch a DaffResolveCartSuccess action', () => {
expect(effects.mergeAfterLogin$).toBeObservable(expected);
});

it('should not try to merge the carts', () => {
expect(driverMergeSpy).not.toHaveBeenCalled();
});
});

describe('and the call to the driver fails', () => {
Expand All @@ -268,21 +411,6 @@ describe('@daffodil/cart-customer/state | DaffCartCustomerAuthEffects', () => {
});
});

describe('when LogoutSuccessAction is triggered', () => {
let expected;
const logoutSuccessAction = new DaffAuthLogoutSuccess();

beforeEach(() => {
const cartCreateAction = new DaffCartCreate();
actions$ = hot('--a', { a: logoutSuccessAction });
expected = cold('--b', { b: cartCreateAction });
});

it('should dispatch cart create', () => {
expect(effects.createAfterLogout$).toBeObservable(expected);
});
});

describe('when ResolveCartFailureAction is triggered', () => {
let expected;

Expand All @@ -298,6 +426,11 @@ describe('@daffodil/cart-customer/state | DaffCartCustomerAuthEffects', () => {
it('should dispatch cart create', () => {
expect(effects.createWhenUnathorized$).toBeObservable(expected);
});

it('should remove the cart ID from storage', () => {
expect(effects.createWhenUnathorized$).toBeObservable(expected);
expect(removeCartIdSpy).toHaveBeenCalledWith();
});
});

describe('and the error is not a DaffUnauthorizedForCartError', () => {
Expand Down Expand Up @@ -329,6 +462,11 @@ describe('@daffodil/cart-customer/state | DaffCartCustomerAuthEffects', () => {
it('should dispatch cart create', () => {
expect(effects.createWhenUnathorized$).toBeObservable(expected);
});

it('should remove the cart ID from storage', () => {
expect(effects.createWhenUnathorized$).toBeObservable(expected);
expect(removeCartIdSpy).toHaveBeenCalledWith();
});
});

describe('and the error is not a DaffUnauthorizedForCartError', () => {
Expand Down
Loading

0 comments on commit 11b1bb1

Please sign in to comment.