Skip to content

Commit cf4861d

Browse files
committed
introduce CustomerRepository
1 parent 41bb204 commit cf4861d

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/customer-repository/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

src/customer-repository/index.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
});

0 commit comments

Comments
 (0)