-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquote-create-form.component.spec.ts
148 lines (114 loc) · 4.62 KB
/
quote-create-form.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
import {QuotesCreateFormComponent} from '@modules/quote-create-form/quote-create-form.component';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {QuotesCreateFormModule} from '@modules/quote-create-form/quote-create-form.module';
import {PageObject} from '@utils/test/pageObject';
import {Store} from '@ngrx/store';
import {anything, capture, instance, mock, verify} from 'ts-mockito';
import {DebugElement} from '@angular/core';
import {QuotesCreate} from '@store/actions/quotes.action';
describe('QuotesCreateFormComponent - quote create form component', () => {
let fixture: ComponentFixture<QuotesCreateFormComponent>;
let component: QuotesCreateFormComponent;
let pageObject: PageObject<QuotesCreateFormComponent>;
let storeMock;
beforeEach(() => {
storeMock = mock<Store<any>>(Store);
TestBed.configureTestingModule({
imports: [
QuotesCreateFormModule,
],
providers: [
{
provide: Store,
useValue: instance(storeMock)
}
]
}).compileComponents();
fixture = TestBed.createComponent(QuotesCreateFormComponent);
component = fixture.componentInstance;
pageObject = new PageObject<QuotesCreateFormComponent>(fixture);
fixture.detectChanges();
});
describe('Form validation', () => {
const getErrorElement = () => pageObject.getElementBySelector('.error');
let authorInput: DebugElement;
let textInput: DebugElement;
let submitButton: DebugElement;
beforeEach(() => {
authorInput = pageObject.getElementBySelector('.author-input');
textInput = pageObject.getElementBySelector('.text-input');
submitButton = pageObject.getElementBySelector('.submit');
});
it('Text of error validation is not displayed by default', () => {
const errorElement = pageObject.getElementBySelector('.error');
expect(errorElement).toBeFalsy('Error text must be not displayed');
});
describe('Validation error is displayed, when:', () => {
it('Author name length less than 2 characters', () => {
pageObject.setInputValue(authorInput, 'A');
expect(getErrorElement()).toBeTruthy();
});
it('Text less than 2 characters', () => {
pageObject.setInputValue(textInput, 'B');
expect(getErrorElement()).toBeTruthy();
});
it('Author name length greater than 64 characters', () => {
pageObject.setInputValue(authorInput, 'c'.repeat(65));
expect(getErrorElement()).toBeTruthy();
});
it('Text length greater than 256 characters', () => {
pageObject.setInputValue(textInput, 'd'.repeat(257));
expect(getErrorElement()).toBeTruthy();
});
it('Author is not filled', () => {
pageObject.setInputValue(authorInput, 'e');
pageObject.setInputValue(authorInput, '');
expect(getErrorElement()).toBeTruthy();
});
it('Text is not filled', () => {
pageObject.setInputValue(textInput, 'f');
pageObject.setInputValue(textInput, '');
expect(getErrorElement()).toBeTruthy();
});
});
describe('alidation error is not displayed, when:', () => {
beforeEach(() => {
pageObject.setInputValue(authorInput, 'a');
pageObject.setInputValue(authorInput, '');
});
it('Length of author name > 2 & < 64 and length of text > 2 & < 256', () => {
expect(getErrorElement()).toBeTruthy();
pageObject.setInputValue(authorInput, 'test');
pageObject.setInputValue(textInput, 'me please');
expect(getErrorElement()).toBeFalsy();
});
});
describe('Form submitting', () => {
const text = 'In God';
const author = 'We trust';
function fillAndSubmitForm(authorValue = author, textValue = text) {
pageObject.setInputValue(authorInput, authorValue);
pageObject.setInputValue(textInput, textValue);
pageObject.triggerClick(submitButton);
}
describe('When form is valid', () => {
it('The entered data is sent', () => {
fillAndSubmitForm();
const [action] = capture(storeMock.dispatch).last();
expect(action).toEqual(new QuotesCreate(text, author));
});
it('Fields of form cleans', () => {
fillAndSubmitForm();
expect(pageObject.getInputText(authorInput)).toBe('');
expect(pageObject.getInputText(textInput)).toBe('');
});
});
describe('When form is not valid', () => {
it('The entered data is not sent', () => {
fillAndSubmitForm('1', '2');
verify(storeMock.dispatch(anything())).never();
});
});
});
});
});