|
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | + |
| 3 | +import { BudgetApplicationFormComponent } from './budget-application-form.component'; |
| 4 | + |
| 5 | +describe('BudgetApplicationFormComponent', () => { |
| 6 | + let component: BudgetApplicationFormComponent; |
| 7 | + let fixture: ComponentFixture<BudgetApplicationFormComponent>; |
| 8 | + |
| 9 | + beforeEach(async () => { |
| 10 | + await TestBed.configureTestingModule({ |
| 11 | + imports: [BudgetApplicationFormComponent], |
| 12 | + }).compileComponents(); |
| 13 | + |
| 14 | + fixture = TestBed.createComponent(BudgetApplicationFormComponent); |
| 15 | + component = fixture.componentInstance; |
| 16 | + fixture.detectChanges(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should create', () => { |
| 20 | + expect(component).toBeTruthy(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should display an error message when name is empty', () => { |
| 24 | + component.formData.name = ''; |
| 25 | + component.onSubmit(); |
| 26 | + |
| 27 | + fixture.detectChanges(); |
| 28 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 29 | + expect(errorMessage).toBeTruthy(); |
| 30 | + expect(errorMessage.textContent).toBe('Name is required'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should display an error message when name is not a string', () => { |
| 34 | + // @ts-ignore |
| 35 | + component.formData.name = 123; |
| 36 | + component.onSubmit(); |
| 37 | + |
| 38 | + fixture.detectChanges(); |
| 39 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 40 | + expect(errorMessage).toBeTruthy(); |
| 41 | + expect(errorMessage.textContent).toBe('Name must be a string'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should display an error message when name is not enough characters', () => { |
| 45 | + component.formData.name = 'b'; |
| 46 | + component.onSubmit(); |
| 47 | + |
| 48 | + fixture.detectChanges(); |
| 49 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 50 | + expect(errorMessage).toBeTruthy(); |
| 51 | + expect(errorMessage.textContent).toBe( |
| 52 | + 'Name must be at least 2 characters long' |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should display an error message when name is not letters only', () => { |
| 57 | + component.formData.name = '123'; |
| 58 | + component.onSubmit(); |
| 59 | + |
| 60 | + fixture.detectChanges(); |
| 61 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 62 | + expect(errorMessage).toBeTruthy(); |
| 63 | + expect(errorMessage.textContent).toBe('Name must contain only letters'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should display an error message when email is empty', () => { |
| 67 | + component.formData.name = 'John Doe'; |
| 68 | + component.formData.email = ''; |
| 69 | + component.onSubmit(); |
| 70 | + |
| 71 | + fixture.detectChanges(); |
| 72 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 73 | + expect(errorMessage).toBeTruthy(); |
| 74 | + expect(errorMessage.textContent).toBe('Email is required'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should display an error message when email is not a string', () => { |
| 78 | + // @ts-ignore |
| 79 | + component.formData.email = 123; |
| 80 | + component.formData.name = 'John Doe'; |
| 81 | + component.onSubmit(); |
| 82 | + |
| 83 | + fixture.detectChanges(); |
| 84 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 85 | + expect(errorMessage).toBeTruthy(); |
| 86 | + expect(errorMessage.textContent).toBe('Email must be a string'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should display an error message when email does not contain an @', () => { |
| 90 | + component.formData.name = 'John Doe'; |
| 91 | + component.formData.email = '123'; |
| 92 | + component.onSubmit(); |
| 93 | + |
| 94 | + fixture.detectChanges(); |
| 95 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 96 | + expect(errorMessage).toBeTruthy(); |
| 97 | + expect(errorMessage.textContent).toBe('Email must contain @'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should display an error message when email does not contain a dot after @', () => { |
| 101 | + component.formData.name = 'John Doe'; |
| 102 | + component.formData.email = '123@'; |
| 103 | + component.onSubmit(); |
| 104 | + |
| 105 | + fixture.detectChanges(); |
| 106 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 107 | + expect(errorMessage).toBeTruthy(); |
| 108 | + expect(errorMessage.textContent).toBe('Email must contain .'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should display an error message when email does contain a dot before @ and not after', () => { |
| 112 | + component.formData.name = 'John Doe'; |
| 113 | + component.formData.email = 'john.doe@'; |
| 114 | + component.onSubmit(); |
| 115 | + |
| 116 | + fixture.detectChanges(); |
| 117 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 118 | + expect(errorMessage).toBeTruthy(); |
| 119 | + expect(errorMessage.textContent).toBe('Email must contain .'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should display an error message when phone is empty', () => { |
| 123 | + component.formData.name = 'John Doe'; |
| 124 | + component.formData.email = 'john.doe@gmail.com'; |
| 125 | + component.formData.phone = ''; |
| 126 | + component.onSubmit(); |
| 127 | + |
| 128 | + fixture.detectChanges(); |
| 129 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 130 | + expect(errorMessage).toBeTruthy(); |
| 131 | + expect(errorMessage.textContent).toBe('Phone is required'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should display an error message when phone is not a string', () => { |
| 135 | + // @ts-ignore |
| 136 | + component.formData.phone = 123; |
| 137 | + component.formData.name = 'John Doe'; |
| 138 | + component.formData.email = 'john.doe@gmail.com'; |
| 139 | + component.onSubmit(); |
| 140 | + |
| 141 | + fixture.detectChanges(); |
| 142 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 143 | + expect(errorMessage).toBeTruthy(); |
| 144 | + expect(errorMessage.textContent).toBe('Phone must be a string'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should display an error message when phone does not contain only numbers', () => { |
| 148 | + component.formData.name = 'John Doe'; |
| 149 | + component.formData.email = 'john.doe@gmail.com'; |
| 150 | + component.formData.phone = '123a'; |
| 151 | + component.onSubmit(); |
| 152 | + |
| 153 | + fixture.detectChanges(); |
| 154 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 155 | + expect(errorMessage).toBeTruthy(); |
| 156 | + expect(errorMessage.textContent).toBe('Phone must contain only numbers'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should display an error message when phone does not contain enough characters', () => { |
| 160 | + component.formData.name = 'John Doe'; |
| 161 | + component.formData.email = 'john.doe@gmail.com'; |
| 162 | + component.formData.phone = '123'; |
| 163 | + component.onSubmit(); |
| 164 | + |
| 165 | + fixture.detectChanges(); |
| 166 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 167 | + expect(errorMessage).toBeTruthy(); |
| 168 | + expect(errorMessage.textContent).toBe( |
| 169 | + 'Phone must be at least 6 characters long' |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should display an error message when phone does contain too many characters', () => { |
| 174 | + component.formData.name = 'John Doe'; |
| 175 | + component.formData.email = 'john.doe@gmail.com'; |
| 176 | + component.formData.phone = '1234567890154545'; |
| 177 | + component.onSubmit(); |
| 178 | + |
| 179 | + fixture.detectChanges(); |
| 180 | + const errorMessage = fixture.nativeElement.querySelector('.error-message'); |
| 181 | + expect(errorMessage).toBeTruthy(); |
| 182 | + expect(errorMessage.textContent).toBe( |
| 183 | + 'Phone must be at most 9 characters long' |
| 184 | + ); |
| 185 | + }); |
| 186 | +}); |
0 commit comments