-
Notifications
You must be signed in to change notification settings - Fork 261
/
claim-task-cloud.directive.spec.ts
176 lines (146 loc) · 6.24 KB
/
claim-task-cloud.directive.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, ContentChildren, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { setupTestBed } from '@alfresco/adf-core';
import { TaskCloudService } from '../services/task-cloud.service';
import { of, throwError } from 'rxjs';
import { ClaimTaskCloudDirective } from './claim-task-cloud.directive';
import { taskClaimCloudMock } from '../task-header/mocks/fake-claim-task.mock';
import { ProcessServiceCloudTestingModule } from '../../testing/process-service-cloud.testing.module';
import { TranslateModule } from '@ngx-translate/core';
describe('ClaimTaskCloudDirective', () => {
@Component({
selector: 'adf-cloud-claim-test-component',
template: '<button adf-cloud-claim-task [taskId]="taskMock" [appName]="appNameMock" (error)="onError($event)"></button>'
})
class TestComponent {
taskMock = 'test1234';
appNameMock = 'simple-app';
@ViewChild(ClaimTaskCloudDirective, { static: false })
claimTaskDirective: ClaimTaskCloudDirective;
onError(error: Error) {
return error;
}
}
let fixture: ComponentFixture<TestComponent>;
let taskCloudService: TaskCloudService;
setupTestBed({
imports: [
TranslateModule.forRoot(),
ProcessServiceCloudTestingModule
],
declarations: [
TestComponent
]
});
beforeEach(() => {
taskCloudService = TestBed.inject(TaskCloudService);
fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
});
it('should directive claim task', () => {
spyOn(taskCloudService, 'claimTask').and.returnValue(of(taskClaimCloudMock));
const button = fixture.nativeElement.querySelector('button');
button.click();
expect(taskCloudService.claimTask).toHaveBeenCalled();
});
it('should emit error on api fail', async () => {
const error = { message: 'task key not found' };
spyOn(taskCloudService, 'claimTask').and.returnValue(throwError(error));
spyOn(fixture.componentInstance, 'onError').and.callThrough();
const button = fixture.nativeElement.querySelector('button');
button.click();
await fixture.whenStable();
expect(taskCloudService.claimTask).toHaveBeenCalled();
expect(fixture.componentInstance.onError).toHaveBeenCalledWith(error);
});
});
describe('Claim Task Directive validation errors', () => {
@Component({
selector: 'adf-cloud-claim-no-fields-validation-component',
template: '<button adf-cloud-claim-task></button>'
})
class ClaimTestMissingInputDirectiveComponent {
appName = 'simple-app';
appNameUndefined = undefined;
appNameNull = null;
@ContentChildren(ClaimTaskCloudDirective)
claimTaskValidationDirective: ClaimTaskCloudDirective;
}
@Component({
selector: 'adf-cloud-claim-no-taskid-validation-component',
template: '<button adf-cloud-claim-task [appName]="appName"></button>'
})
class ClaimTestMissingTaskIdDirectiveComponent {
appName = 'simple-app';
@ContentChildren(ClaimTaskCloudDirective)
claimTaskValidationDirective: ClaimTaskCloudDirective;
}
@Component({
selector: 'adf-cloud-claim-undefined-appname-component',
template: '<button adf-cloud-claim-task [taskId]="taskMock" [appName]="appNameUndefined"></button>'
})
class ClaimTestInvalidAppNameUndefineddDirectiveComponent {
appNameUndefined = undefined;
taskMock = 'test1234';
@ContentChildren(ClaimTaskCloudDirective)
claimTaskValidationDirective: ClaimTaskCloudDirective;
}
@Component({
selector: 'adf-cloud-claim-null-appname-component',
template: '<button adf-cloud-claim-task [taskId]="taskMock" [appName]="appNameNull"></button>'
})
class ClaimTestInvalidAppNameNulldDirectiveComponent {
appNameNull = null;
taskMock = 'test1234';
@ViewChild(ClaimTaskCloudDirective, { static: false })
claimTaskValidationDirective: ClaimTaskCloudDirective;
}
let fixture: ComponentFixture<any>;
setupTestBed({
imports: [
TranslateModule.forRoot(),
ProcessServiceCloudTestingModule
],
declarations: [
ClaimTestMissingTaskIdDirectiveComponent,
ClaimTestInvalidAppNameUndefineddDirectiveComponent,
ClaimTestInvalidAppNameNulldDirectiveComponent,
ClaimTestMissingInputDirectiveComponent
]
});
beforeEach(() => {
fixture = TestBed.createComponent(ClaimTestMissingInputDirectiveComponent);
});
it('should throw error when missing input', () => {
fixture = TestBed.createComponent(ClaimTestMissingInputDirectiveComponent);
expect(() => fixture.detectChanges()).toThrowError();
});
it('should throw error when taskId is not set', () => {
fixture = TestBed.createComponent(ClaimTestMissingTaskIdDirectiveComponent);
expect( () => fixture.detectChanges()).toThrowError('Attribute taskId is required');
});
it('should throw error when appName is undefined', () => {
fixture = TestBed.createComponent(ClaimTestInvalidAppNameUndefineddDirectiveComponent);
expect( () => fixture.detectChanges()).toThrowError('Attribute appName is required');
});
it('should throw error when appName is null', () => {
fixture = TestBed.createComponent(ClaimTestInvalidAppNameUndefineddDirectiveComponent);
expect( () => fixture.detectChanges()).toThrowError('Attribute appName is required');
});
});