File tree 2 files changed +50
-0
lines changed
2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { Customer } from '../customer' ;
2
+
3
+ let _data = [ ] ;
4
+
5
+ export class CustomerRepository {
6
+ static initialize ( ) {
7
+ _data = { } ;
8
+ _data . customers = new Map ( ) ;
9
+ }
10
+
11
+ static register ( id ) {
12
+ if ( ! _data . customers . has ( id ) ) {
13
+ _data . customers . set ( id , new Customer ( id ) ) ;
14
+ }
15
+
16
+ return this . find ( id ) ;
17
+ }
18
+
19
+ static find ( id ) {
20
+ return _data . customers . get ( id ) ;
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ import { CustomerRepository } from './index' ;
2
+
3
+ describe ( 'CustomerRepository' , ( ) => {
4
+ beforeEach ( ( ) => {
5
+ CustomerRepository . initialize ( ) ;
6
+ } ) ;
7
+
8
+ describe ( 'register' , ( ) => {
9
+ it ( 'should create a new customer' , ( ) => {
10
+ const customer = CustomerRepository . register ( '123' ) ;
11
+ expect ( customer . id ) . toBe ( '123' ) ;
12
+ } ) ;
13
+
14
+ it ( 'should return the existing customer' , ( ) => {
15
+ const customer = CustomerRepository . register ( '123' ) ;
16
+ const customer2 = CustomerRepository . register ( '123' ) ;
17
+ expect ( customer ) . toBe ( customer2 ) ;
18
+ } ) ;
19
+ } ) ;
20
+
21
+ describe ( 'find' , ( ) => {
22
+ it ( 'should find a customer' , ( ) => {
23
+ const customer = CustomerRepository . register ( '123' ) ;
24
+ const foundCustomer = CustomerRepository . find ( '123' ) ;
25
+ expect ( customer ) . toBe ( foundCustomer ) ;
26
+ } ) ;
27
+ } ) ;
28
+ } ) ;
You can’t perform that action at this time.
0 commit comments