Skip to content

Commit

Permalink
feat(cart): add daffCartDriverHandleCartNotFound (#2489)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Jun 29, 2023
1 parent 18bca84 commit 15cb6cd
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
112 changes: 112 additions & 0 deletions libs/cart/driver/src/operators/handle-cart-not-found.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import {
Observable,
Subject,
catchError,
of,
} from 'rxjs';

import { DaffCartStorageService } from '@daffodil/cart';
import { DaffCartNotFoundError } from '@daffodil/cart/driver';

import { daffCartDriverHandleCartNotFound } from './handle-cart-not-found';

describe('@daffodil/cart/driver | daffCartDriverHandleCartNotFound', () => {
let source: Subject<any>;
let result: Observable<any>;
let cartStorageSpy: jasmine.SpyObj<DaffCartStorageService>;

beforeEach(() => {
source = new Subject();
cartStorageSpy = jasmine.createSpyObj('DaffCartStorageService', ['removeCartId']);
result = source.pipe(
daffCartDriverHandleCartNotFound(cartStorageSpy),
);
});

describe('when the source emits a value', () => {
let value: number;

beforeEach(() => {
value = 5;
});

it('should pass that value through', (done) => {
result.pipe(
catchError((err) => {
fail('should not throw');
return of();
}),
).subscribe((res) => {
expect(res).toEqual(value);
done();
});
source.next(5);
});
});

describe('when the source throws a generic error', () => {
let error: Error;

beforeEach(() => {
error = new Error('');
source.error(error);
});

it('should throw that error', (done) => {
result.pipe(
catchError((err) => {
expect(err).toEqual(error);
done();
return of();
}),
).subscribe((res) => {
fail('should not emit');
});
});

it('should not remove cart ID from storage', (done) => {
result.pipe(
catchError((err) => {
expect(cartStorageSpy.removeCartId).not.toHaveBeenCalled();
done();
return of();
}),
).subscribe((res) => {
fail('should not emit');
});
});
});

describe('when the source throws a cart not found error', () => {
let error: DaffCartNotFoundError;

beforeEach(() => {
error = new DaffCartNotFoundError('');
source.error(error);
});

it('should throw that error', (done) => {
result.pipe(
catchError((err) => {
expect(err).toEqual(error);
done();
return of();
}),
).subscribe((res) => {
fail('should not emit');
});
});

it('should remove cart ID from storage', (done) => {
result.pipe(
catchError((err) => {
expect(cartStorageSpy.removeCartId).toHaveBeenCalledWith();
done();
return of();
}),
).subscribe((res) => {
fail('should not emit');
});
});
});
});
28 changes: 28 additions & 0 deletions libs/cart/driver/src/operators/handle-cart-not-found.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
Observable,
catchError,
pipe,
throwError,
} from 'rxjs';

import { DaffCartStorageService } from '@daffodil/cart';

import { DaffCartNotFoundError } from '../errors/public_api';

/**
* If a {@link DaffCartNotFoundError} is thrown into the stream,
* the cart ID is removed from storage.
* All errors are thrown back into the stream.
*/
export const daffCartDriverHandleCartNotFound = <T>(storage: DaffCartStorageService) => pipe<Observable<T>, Observable<T>>(
catchError((error) => {
switch (true) {
case error instanceof DaffCartNotFoundError:
storage.removeCartId();
return throwError(() => error);

default:
return throwError(() => error);
}
}),
);
1 change: 1 addition & 0 deletions libs/cart/driver/src/operators/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { daffCartDriverHandleCartNotFound } from './handle-cart-not-found';
1 change: 1 addition & 0 deletions libs/cart/driver/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './interfaces/public_api';
export * from './injection-tokens/public_api';
export * from './errors/public_api';
export * from './operators/public_api';

0 comments on commit 15cb6cd

Please sign in to comment.