/
create-integration.component.spec.ts
154 lines (130 loc) · 6.18 KB
/
create-integration.component.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
/*
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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 { ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
import { HttpTestingController } from '@angular/common/http/testing';
import { InteractivityChecker } from '@angular/cdk/a11y';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { MatErrorHarness } from '@angular/material/form-field/testing';
import { CreateIntegrationComponent } from './create-integration.component';
import { CreateIntegrationHarness } from './create-integration.harness';
import { IntegrationsModule } from '../integrations.module';
import { CONSTANTS_TESTING, GioTestingModule } from '../../../shared/testing';
import { SnackBarService } from '../../../services-ngx/snack-bar.service';
import { CreateIntegrationPayload } from '../integrations.model';
describe('CreateIntegrationComponent', () => {
let fixture: ComponentFixture<CreateIntegrationComponent>;
let componentHarness: CreateIntegrationHarness;
let httpTestingController: HttpTestingController;
const fakeSnackBarService = {
error: jest.fn(),
};
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [CreateIntegrationComponent],
imports: [GioTestingModule, IntegrationsModule, BrowserAnimationsModule, NoopAnimationsModule],
providers: [
{
provide: SnackBarService,
useValue: fakeSnackBarService,
},
],
})
.overrideProvider(InteractivityChecker, {
useValue: {
isFocusable: () => true, // This traps focus checks and so avoid warnings when dealing with
isTabbable: () => true, // This traps focus checks and so avoid warnings when dealing with
},
})
.compileComponents();
});
beforeEach(async () => {
fixture = TestBed.createComponent(CreateIntegrationComponent);
httpTestingController = TestBed.inject(HttpTestingController);
componentHarness = await TestbedHarnessEnvironment.harnessForFixture(fixture, CreateIntegrationHarness);
fixture.detectChanges();
});
afterEach(() => {
httpTestingController.verify();
});
describe('form', () => {
it('should not submit integration with too short name', async () => {
await componentHarness.setName('');
await componentHarness.setDescription('Some description');
fixture.detectChanges();
const error: MatErrorHarness = await componentHarness.matErrorMessage();
expect(await error.getText()).toEqual('Integration name is required.');
await componentHarness.clickOnSubmit();
httpTestingController.expectNone(`${CONSTANTS_TESTING.env.v2BaseURL}/integrations`);
});
it('should not submit integration with too long name', async () => {
await componentHarness.setName('test too long name 01234567890123456789012345678901234567890123456789');
await componentHarness.setDescription('Test Description');
fixture.detectChanges();
const error: MatErrorHarness = await componentHarness.matErrorMessage();
expect(await error.getText()).toEqual('Integration name has to be less than 50 characters long.');
await componentHarness.clickOnSubmit();
httpTestingController.expectNone(`${CONSTANTS_TESTING.env.v2BaseURL}/integrations`);
});
it('should not submit integration with too long description', async () => {
await componentHarness.setName('test0');
await componentHarness.setDescription(
'TOO long description: loa hdvoiah dfopivioa fdo[ivu[au f[09vu a[09eu v9[ua09efu 0v9u e09fv u09qw uef09v uq0w9duf v0 qu0efdu 0vwu df09vu 0wduf09v wu0dfu v0 wud0fv uqw0 uf90v uw9efuv9wu efvu wqpefuvqwu e0fu v0wu ef0vu w0euf 0vqwu 0efu v0qwuef uvqw uefvru wfeuvwufvu w0 ufev',
);
await componentHarness.clickOnSubmit();
httpTestingController.expectNone(`${CONSTANTS_TESTING.env.v2BaseURL}/integrations`);
});
it('should create integration with valid name', async () => {
const expectedPayload: CreateIntegrationPayload = {
name: 'TEST123',
description: '',
provider: 'amazon',
};
await componentHarness.setName('TEST123');
await componentHarness.clickOnSubmit();
expectIntegrationPostRequest(expectedPayload);
});
it('should create integration with description', async () => {
const expectedPayload: CreateIntegrationPayload = {
name: 'TEST123',
description: 'Test Description',
provider: 'amazon',
};
await componentHarness.setName('TEST123');
await componentHarness.setDescription('Test Description');
await componentHarness.clickOnSubmit();
expectIntegrationPostRequest(expectedPayload);
});
it('should handle error with message', async () => {
await componentHarness.setName('TEST123');
await componentHarness.clickOnSubmit();
expectIntegrationWithError();
fixture.detectChanges();
expect(fakeSnackBarService.error).toHaveBeenCalledWith('An error occurred. Integration not created');
});
});
function expectIntegrationPostRequest(payload: CreateIntegrationPayload): void {
const req = httpTestingController.expectOne(`${CONSTANTS_TESTING.env.v2BaseURL}/integrations`);
req.flush([]);
expect(req.request.method).toEqual('POST');
expect(req.request.body).toEqual(payload);
}
function expectIntegrationWithError(): void {
const req = httpTestingController.expectOne(`${CONSTANTS_TESTING.env.v2BaseURL}/integrations`);
expect(req.request.method).toEqual('POST');
req.flush({}, { status: 400, statusText: 'Bad Request' });
}
});