1
|
|
|
import {mock, instance, when, verify, deepEqual, anything} from 'ts-mockito'; |
2
|
|
|
import {QuoteRepository} from 'src/Infrastructure/Billing/Repository/QuoteRepository'; |
3
|
|
|
import {CreateQuoteItemsCommandHandler} from './CreateQuoteItemsCommandHandler'; |
4
|
|
|
import {CreateQuoteItemsCommand, ICreateQuote} from './CreateQuoteItemsCommand'; |
5
|
|
|
import {Quote} from 'src/Domain/Billing/Quote.entity'; |
6
|
|
|
import {QuoteItemRepository} from 'src/Infrastructure/Billing/Repository/QuoteItemRepository'; |
7
|
|
|
import {QuoteItem} from 'src/Domain/Billing/QuoteItem.entity'; |
8
|
|
|
import {User} from 'src/Domain/User/User.entity'; |
9
|
|
|
import {Customer} from 'src/Domain/Customer/Customer.entity'; |
10
|
|
|
import {QuoteNotFoundException} from 'src/Domain/Billing/Exception/QuoteNotFoundException'; |
11
|
|
|
|
12
|
|
|
describe('CreateQuoteItemsCommandHandler', () => { |
13
|
|
|
let quoteRepository: QuoteRepository; |
14
|
|
|
let quoteItemRepository: QuoteItemRepository; |
15
|
|
|
let handler: CreateQuoteItemsCommandHandler; |
16
|
|
|
|
17
|
|
|
const quote = new Quote( |
18
|
|
|
'a491ccc9-df7c-4fc6-8e90-db816208f689', |
19
|
|
|
'draft', |
20
|
|
|
instance(mock(User)), |
21
|
|
|
instance(mock(Customer)) |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
const items: ICreateQuote[] = [ |
25
|
|
|
{ |
26
|
|
|
title: 'Développement web', |
27
|
|
|
vat: 19.6, |
28
|
|
|
dailyRate: 800.5, |
29
|
|
|
quantity: 10 |
30
|
|
|
}, |
31
|
|
|
{ |
32
|
|
|
title: 'Développement mobile', |
33
|
|
|
vat: 20, |
34
|
|
|
dailyRate: 700, |
35
|
|
|
quantity: 10 |
36
|
|
|
} |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
const command = new CreateQuoteItemsCommand( |
40
|
|
|
'a491ccc9-df7c-4fc6-8e90-db816208f689', |
41
|
|
|
items |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
beforeEach(() => { |
45
|
|
|
quoteRepository = mock(QuoteRepository); |
46
|
|
|
quoteItemRepository = mock(QuoteItemRepository); |
47
|
|
|
|
48
|
|
|
handler = new CreateQuoteItemsCommandHandler( |
49
|
|
|
instance(quoteItemRepository), |
50
|
|
|
instance(quoteRepository) |
51
|
|
|
); |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
it('testQuoteItemsSuccessfullyCreated', async () => { |
55
|
|
|
when( |
56
|
|
|
quoteRepository.find('a491ccc9-df7c-4fc6-8e90-db816208f689') |
57
|
|
|
).thenResolve(quote); |
58
|
|
|
|
59
|
|
|
expect(await handler.execute(command)).toBeUndefined(); |
60
|
|
|
|
61
|
|
|
verify(quoteRepository.find('a491ccc9-df7c-4fc6-8e90-db816208f689')).once(); |
62
|
|
|
verify( |
63
|
|
|
quoteItemRepository.save( |
64
|
|
|
deepEqual(new QuoteItem('Développement web', 10, 80050, 1960, quote)) |
65
|
|
|
) |
66
|
|
|
).once(); |
67
|
|
|
verify( |
68
|
|
|
quoteItemRepository.save( |
69
|
|
|
deepEqual(new QuoteItem('Développement mobile', 10, 70000, 2000, quote)) |
70
|
|
|
) |
71
|
|
|
).once(); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
it('testQuoteNotFound', async () => { |
75
|
|
|
when( |
76
|
|
|
quoteRepository.find('a491ccc9-df7c-4fc6-8e90-db816208f689') |
77
|
|
|
).thenResolve(null); |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
await handler.execute(command); |
81
|
|
|
} catch (e) { |
82
|
|
|
expect(e).toBeInstanceOf(QuoteNotFoundException); |
83
|
|
|
expect(e.message).toBe('quote.errors.not_found'); |
84
|
|
|
verify(quoteItemRepository.save(anything())).never(); |
85
|
|
|
} |
86
|
|
|
}); |
87
|
|
|
}); |
88
|
|
|
|