11import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
22
33import { LoginComponent } from './login.component' ;
4+ import { ReactiveFormsModule } from '@angular/forms' ;
45
56describe ( 'LoginComponent' , ( ) => {
67let component : LoginComponent ;
78let fixture : ComponentFixture < LoginComponent > ;
89
910beforeEach ( async ( ( ) => {
1011TestBed . configureTestingModule ( {
11- declarations : [ LoginComponent ]
12- } )
13- . compileComponents ( ) ;
12+ declarations : [ LoginComponent ] ,
13+ imports : [ ReactiveFormsModule ]
14+ } ) . compileComponents ( ) ;
1415} ) ) ;
1516
1617beforeEach ( ( ) => {
@@ -22,4 +23,77 @@ describe('LoginComponent', () =>{
2223it ( 'should create' , ( ) => {
2324expect ( component ) . toBeTruthy ( ) ;
2425} ) ;
26+
27+ it ( 'should render form with email and password inputs' , ( ) => {
28+ const element = fixture . nativeElement ;
29+
30+ expect ( element . querySelector ( 'form' ) ) . toBeTruthy ( ) ;
31+ expect ( element . querySelector ( '#email' ) ) . toBeTruthy ( ) ;
32+ expect ( element . querySelector ( '#password' ) ) . toBeTruthy ( ) ;
33+ expect ( element . querySelector ( 'button' ) ) . toBeTruthy ( ) ;
34+ } ) ;
35+
36+ it ( 'should return model invalid when form is empty' , ( ) => {
37+ expect ( component . form . valid ) . toBeFalsy ( ) ;
38+ } ) ;
39+
40+ it ( 'should validate email input as required' , ( ) => {
41+ const email = component . form . controls . email ;
42+
43+ expect ( email . valid ) . toBeFalsy ( ) ;
44+ expect ( email . errors . required ) . toBeTruthy ( ) ;
45+ } ) ;
46+
47+ it ( 'should validate password input as required' , ( ) => {
48+ const password = component . form . controls . password ;
49+
50+ expect ( password . valid ) . toBeFalsy ( ) ;
51+ expect ( password . errors . required ) . toBeTruthy ( ) ;
52+ } ) ;
53+
54+ it ( 'should validate email format' , ( ) => {
55+ const email = component . form . controls . email ;
56+ email . setValue ( 'test' ) ;
57+ const errors = email . errors ;
58+
59+ expect ( errors . required ) . toBeFalsy ( ) ;
60+ expect ( errors . pattern ) . toBeTruthy ( ) ;
61+ expect ( email . valid ) . toBeFalsy ( ) ;
62+ } ) ;
63+
64+ it ( 'should validate email format correctly' , ( ) => {
65+ const email = component . form . controls . email ;
66+ email . setValue ( '[email protected] ' ) ; 67+ const errors = email . errors || { } ;
68+
69+ expect ( email . valid ) . toBeTruthy ( ) ;
70+ expect ( errors . required ) . toBeFalsy ( ) ;
71+ expect ( errors . pattern ) . toBeFalsy ( ) ;
72+ } ) ;
73+
74+ it ( 'should render email validation message when formControl is submitted and invalid' , ( ) => {
75+ const elements : HTMLElement = fixture . nativeElement ;
76+ expect ( elements . querySelector ( '#email-error' ) ) . toBeFalsy ( ) ;
77+
78+ elements . querySelector ( 'button' ) . click ( ) ;
79+
80+ fixture . detectChanges ( ) ;
81+ expect ( elements . querySelector ( '#email-error' ) ) . toBeTruthy ( ) ;
82+ expect ( elements . querySelector ( '#email-error' ) . textContent ) . toContain (
83+ 'Please enter a valid email.'
84+ ) ;
85+ } ) ;
86+
87+ it ( 'should render password validation message when formControl is submitted and invalid' , ( ) => {
88+ const elements : HTMLElement = fixture . nativeElement ;
89+ expect ( elements . querySelector ( '#password-error' ) ) . toBeFalsy ( ) ;
90+
91+ elements . querySelector ( 'button' ) . click ( ) ;
92+
93+ fixture . detectChanges ( ) ;
94+ expect ( elements . querySelector ( '#password-error' ) ) . toBeTruthy ( ) ;
95+ expect ( elements . querySelector ( '#password-error' ) . textContent ) . toContain (
96+ 'Please enter a valid password.'
97+ ) ;
98+ } ) ;
2599} ) ;
0 commit comments