-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae70df8
commit 88f3c65
Showing
7 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
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,21 @@ | ||
import { Address } from '../value-object/address' | ||
import { CustomerFactory } from './customer-factory' | ||
|
||
describe('Customer factory unit test', () => { | ||
it('should create a customer', () => { | ||
const customer = CustomerFactory.create('john') | ||
expect(customer.id).toBeDefined() | ||
expect(customer.name).toBe('john') | ||
expect(customer.address).toBeUndefined() | ||
expect(customer.constructor.name).toBe('Customer') | ||
}) | ||
|
||
it('should create a customer with an address', () => { | ||
const address = new Address('Street 1', 123, '12345-678', 'São Paulo', 'SP') | ||
const customer = CustomerFactory.createWithAddress('john', address) | ||
expect(customer.id).toBeDefined() | ||
expect(customer.name).toBe('john') | ||
expect(customer.address).toBeDefined() | ||
expect(customer.constructor.name).toBe('Customer') | ||
}) | ||
}) |
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,15 @@ | ||
import { v4 as uuid } from 'uuid' | ||
import { Customer } from '../entity/customer' | ||
import { Address } from '../value-object/address' | ||
|
||
export class CustomerFactory { | ||
static create (name: string): Customer { | ||
return new Customer(uuid(), name) | ||
} | ||
|
||
static createWithAddress (name: string, address: Address): Customer { | ||
const customer = new Customer(uuid(), name) | ||
customer.changeAddress(address) | ||
return customer | ||
} | ||
} |
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,5 @@ | ||
export class ProductInterface { | ||
id: string | ||
name: string | ||
price: number | ||
} |
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
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,48 @@ | ||
import { ProductInterface } from './product-interface' | ||
|
||
export class ProductB implements ProductInterface { | ||
private readonly _id: string | ||
private _name: string | ||
private _price: number | ||
|
||
constructor (id: string, name: string, price: number) { | ||
this._id = id | ||
this._name = name | ||
this._price = price | ||
this.validate() | ||
} | ||
|
||
get id (): string { | ||
return this._id | ||
} | ||
|
||
get name (): string { | ||
return this._name | ||
} | ||
|
||
get price (): number { | ||
return this._price * 2 | ||
} | ||
|
||
validate (): void { | ||
if (this._id.length === 0) { | ||
throw new Error('id is required') | ||
} | ||
if (this._name.length === 0) { | ||
throw new Error('name is required') | ||
} | ||
if (this._price <= 0) { | ||
throw new Error('price is less than 0') | ||
} | ||
} | ||
|
||
changeName (name: string): void { | ||
this._name = name | ||
this.validate() | ||
} | ||
|
||
changePrice (price: number): void { | ||
this._price = price | ||
this.validate() | ||
} | ||
} |
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,19 @@ | ||
import { ProductFactory } from './product-factory' | ||
|
||
describe('Product factory unit test', () => { | ||
it('should create a product type a', () => { | ||
const product = ProductFactory.create('a', 'product a', 1) | ||
expect(product.id).toBeDefined() | ||
expect(product.name).toBe('product a') | ||
expect(product.price).toBe(1) | ||
expect(product.constructor.name).toBe('Product') | ||
}) | ||
|
||
it('should create a product type b', () => { | ||
const product = ProductFactory.create('b', 'product b', 1) | ||
expect(product.id).toBeDefined() | ||
expect(product.name).toBe('product b') | ||
expect(product.price).toBe(2) | ||
expect(product.constructor.name).toBe('ProductB') | ||
}) | ||
}) |
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,17 @@ | ||
import { Product } from '../entity/product' | ||
import { ProductInterface } from '../entity/product-interface' | ||
import { v4 as uuid } from 'uuid' | ||
import { ProductB } from '../entity/productB' | ||
|
||
export class ProductFactory { | ||
static create (type: string, name: string, price: number): ProductInterface { | ||
switch (type) { | ||
case 'a': | ||
return new Product(uuid(), name, price) | ||
case 'b': | ||
return new ProductB(uuid(), name, price) | ||
default: | ||
throw new Error('product type not supported') | ||
} | ||
} | ||
} |