-
-
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(cart): add
daffCartDriverHandleCartNotFound
(#2489)
- Loading branch information
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
libs/cart/driver/src/operators/handle-cart-not-found.spec.ts
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,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'); | ||
}); | ||
}); | ||
}); | ||
}); |
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,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); | ||
} | ||
}), | ||
); |
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 @@ | ||
export { daffCartDriverHandleCartNotFound } from './handle-cart-not-found'; |
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 |
---|---|---|
@@ -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'; |