1
1
import { AsyncReturnType } from 'type-fest' ;
2
2
3
+ import { UncompleteTaskApplicationCommand } from '@/commands/uncomplete-task.application-command' ;
4
+ import { TaskWasUncompleted } from '@/events/task-was-uncompleted-event.domain-event' ;
3
5
import { CompleteTaskApplicationCommand } from '@/module/commands/complete-task.application-command' ;
4
6
import { TaskWasCompleted } from '@/module/events/task-was-completed.domain-event' ;
5
7
6
8
import { EventStreamName } from '../shared/application/event-stream-name.value-object' ;
7
9
import { learningMaterialsTasksTestModule } from './learning-materials-tasks.test-module' ;
8
10
11
+ enum CommandType {
12
+ COMPLETE_TASK = 'Complete Task' ,
13
+ UNCOMPLETE_TASK = 'Uncomplete Task' ,
14
+ }
15
+
9
16
describe ( 'learning materials tasks' , ( ) => {
10
17
let module : AsyncReturnType < typeof learningMaterialsTasksTestModule > ;
11
- const commandBuilder = ( taskId = 'VmkxXnPG02CaUNV8Relzk' , learningMaterialsId = 'ZpMpw2eh1llFCGKZJEN6r' ) => ( {
12
- class : CompleteTaskApplicationCommand ,
13
- type : 'CompleteTask' ,
18
+ const commandBuilder = (
19
+ type : string ,
20
+ taskId = 'VmkxXnPG02CaUNV8Relzk' ,
21
+ learningMaterialsId = 'ZpMpw2eh1llFCGKZJEN6r' ,
22
+ ) => ( {
23
+ class : type === CommandType . COMPLETE_TASK ? CompleteTaskApplicationCommand : UncompleteTaskApplicationCommand ,
24
+ type,
14
25
data : { taskId, learningMaterialsId } ,
15
26
} ) ;
16
27
28
+ beforeEach ( async ( ) => {
29
+ module = await learningMaterialsTasksTestModule ( ) ;
30
+ } ) ;
31
+
32
+ afterEach ( async ( ) => {
33
+ await module . close ( ) ;
34
+ } ) ;
35
+
17
36
it ( 'should change state of the task to complete' , async ( ) => {
18
37
// Given
19
- const command = commandBuilder ( ) ;
38
+ const command = commandBuilder ( CommandType . COMPLETE_TASK ) ;
20
39
21
40
// When
22
41
await module . executeCommand ( ( ) => command ) ;
@@ -34,7 +53,7 @@ describe('learning materials tasks', () => {
34
53
35
54
it ( 'should not change task state if task is already completed' , async ( ) => {
36
55
// Given
37
- const command = commandBuilder ( ) ;
56
+ const command = commandBuilder ( CommandType . COMPLETE_TASK ) ;
38
57
39
58
// When
40
59
await module . executeCommand ( ( ) => command ) ;
@@ -43,11 +62,34 @@ describe('learning materials tasks', () => {
43
62
await expect ( ( ) => module . executeCommand ( ( ) => command ) ) . rejects . toThrow ( ) ;
44
63
} ) ;
45
64
46
- beforeEach ( async ( ) => {
47
- module = await learningMaterialsTasksTestModule ( ) ;
65
+ it ( 'should change state of the task to uncomplete when task was completed already' , async ( ) => {
66
+ // Given
67
+ const completeCommand = commandBuilder ( CommandType . COMPLETE_TASK ) ;
68
+ const uncompleteCommand = commandBuilder ( CommandType . UNCOMPLETE_TASK ) ;
69
+
70
+ await module . executeCommand ( ( ) => completeCommand ) ;
71
+
72
+ // When
73
+ await module . executeCommand ( ( ) => uncompleteCommand ) ;
74
+
75
+ // Then
76
+ module . expectEventPublishedLastly < TaskWasUncompleted > ( {
77
+ type : 'TaskWasUncompleted' ,
78
+ data : {
79
+ learningMaterialsId : uncompleteCommand . data . learningMaterialsId ,
80
+ taskId : uncompleteCommand . data . taskId ,
81
+ } ,
82
+ streamName : EventStreamName . from ( 'LearningMaterialsTasks' , uncompleteCommand . data . learningMaterialsId ) ,
83
+ } ) ;
48
84
} ) ;
49
85
50
- afterEach ( async ( ) => {
51
- await module . close ( ) ;
86
+ it ( 'should not change state of the task to uncomplete if task was not completed before' , async ( ) => {
87
+ // Given
88
+ const uncompleteCommand = commandBuilder ( CommandType . UNCOMPLETE_TASK ) ;
89
+
90
+ // When&Then
91
+ await expect ( ( ) => module . executeCommand ( ( ) => uncompleteCommand ) ) . rejects . toThrow (
92
+ 'Can not uncomplete task that was not completed yet.' ,
93
+ ) ;
52
94
} ) ;
53
95
} ) ;
0 commit comments