diff --git a/client/src/app/app.component.spec.ts b/client/src/app/app.component.spec.ts index b4db530463749df9f01471a6a79affc07d6d7532..31481afd669b0a973ef45ad0a85216c686f26ab7 100644 --- a/client/src/app/app.component.spec.ts +++ b/client/src/app/app.component.spec.ts @@ -1,3 +1,4 @@ +import { HttpClientTestingModule } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { AppComponent } from './app.component'; @@ -6,7 +7,8 @@ describe('AppComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ imports: [ - RouterTestingModule + RouterTestingModule, + HttpClientTestingModule ], declarations: [ AppComponent diff --git a/client/src/app/authentication/auth.module.ts b/client/src/app/authentication/auth.module.ts index d2e4fae6f86c813833e6f24066c50b4076a0d599..65c0542d9e9769feb7f03bde0ffe940dc89599ab 100644 --- a/client/src/app/authentication/auth.module.ts +++ b/client/src/app/authentication/auth.module.ts @@ -1,5 +1,6 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; +import { SharedModule } from '../shared/shared.module'; diff --git a/client/src/app/posts/post-details/post-details.component.spec.ts b/client/src/app/posts/post-details/post-details.component.spec.ts index dd08767ab534fe61aad83da494d6cfd33f4e6b32..a2ab8bf1d8b402dab5886a209ca5bd2721619df2 100644 --- a/client/src/app/posts/post-details/post-details.component.spec.ts +++ b/client/src/app/posts/post-details/post-details.component.spec.ts @@ -2,7 +2,9 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ActivatedRoute } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; +import { AuthService } from 'src/app/authentication/auth.service'; import { Post } from 'src/app/models/post.model'; +import { User } from 'src/app/models/user.model'; import { SharedModule } from 'src/app/shared/shared.module'; import { PostListComponent } from '../post-list/post-list.component'; import { PostService } from '../post.service'; @@ -12,9 +14,19 @@ import { PostDetailsComponent } from './post-details.component'; describe('PostDetailsComponent', () => { let component: PostDetailsComponent; let fixture: ComponentFixture<PostDetailsComponent>; - let mockPostService; + let mockPostService, mockAuthService; beforeEach(async () => { + // AuthService mock setup + mockAuthService = jasmine.createSpyObj(['getCurrentUser']); + mockAuthService.getCurrentUser.and.returnValue(new User({ + userId: 4, + username: "tester", + email: "test@test.com", + password: "1234", + create_time: 513498 + })); + // PostService mock setup mockPostService = jasmine.createSpyObj(['getPost', 'deletePost']); mockPostService.getPost.and.returnValue( @@ -25,7 +37,7 @@ describe('PostDetailsComponent', () => { title: "Test", description: "TestDescription", timestamp: 23947298, - owner: "user", + owner: 4, imageUrl: null, price: 49, categoryid: 2 @@ -50,7 +62,8 @@ describe('PostDetailsComponent', () => { ], providers: [ { provide: ActivatedRoute, useValue: { snapshot: {params: {id: 5}}}}, - { provide: PostService, useValue: mockPostService } + { provide: PostService, useValue: mockPostService }, + { provide: AuthService, useValue: mockAuthService } ] }) .compileComponents(); @@ -68,30 +81,32 @@ describe('PostDetailsComponent', () => { it('should get post with id from url parameter', async () => { // Waits for ngOnInit and checks that we get post - expect(component.post).not.toBeNull(); + await fixture.whenStable(); + expect(mockPostService.getPost).toHaveBeenCalledWith(5); + expect(component.post).toEqual(new Post({ + id: 5, + title: "Test", + description: "TestDescription", + timestamp: 23947298, + owner: 4, + imageUrl: null, + price: 49, + categoryid: 2 + })); + }); - fixture.whenStable().then(() => { - expect(mockPostService.getPost).toHaveBeenCalledWith(5); - expect(component.post).toEqual(new Post({ - id: 5, - title: "Test", - description: "TestDescription", - timestamp: 23947298, - owner: "user", - imageUrl: null, - price: 49, - categoryid: 2 - })); - }); + it('should get current user', async () => { + // Waits for ngOnInit and checks that we get post + await fixture.whenStable(); + + expect(mockAuthService.getCurrentUser).toHaveBeenCalledWith(false); + expect(component.userId).toBe(4); }); it('should delete post with id', async () => { // Waits for ngOnInit and checks that we can delete post - expect(component.post).not.toBeNull(); - - fixture.whenStable().then(() => { - component.deletePost(); - expect(mockPostService.deletePost).toHaveBeenCalledWith(5); - }); + await fixture.whenStable(); + component.deletePost(); + expect(mockPostService.deletePost).toHaveBeenCalledWith(5); }); }); diff --git a/client/src/app/posts/post-details/post-details.component.ts b/client/src/app/posts/post-details/post-details.component.ts index d995c9867623aab33367c93e0b477da4de601f0d..efa7fbe43e87f10d721028749c5cd7d78f142194 100644 --- a/client/src/app/posts/post-details/post-details.component.ts +++ b/client/src/app/posts/post-details/post-details.component.ts @@ -17,12 +17,12 @@ export class PostDetailsComponent implements OnInit { constructor(private postService: PostService, private activatedRoute: ActivatedRoute, private router: Router, private authService: AuthService) { } ngOnInit(): void { - // Gets id parameter from URL - const id = this.activatedRoute.snapshot.params["id"]; - // Gets ID from current user this.userId = this.authService.getCurrentUser(false).getUserId; + // Gets id parameter from URL + const id = this.activatedRoute.snapshot.params["id"]; + // Gets Post with id from database this.postService.getPost(id).then(post => { this.post = post; diff --git a/client/src/app/posts/post-form/post-form.component.spec.ts b/client/src/app/posts/post-form/post-form.component.spec.ts index b16d0c1d0f8cf6a09db3e50c254b9fac775f73d3..adfab335d54f814cd3a1609288effaa2c22d1f0f 100644 --- a/client/src/app/posts/post-form/post-form.component.spec.ts +++ b/client/src/app/posts/post-form/post-form.component.spec.ts @@ -3,8 +3,11 @@ import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing'; import { FormsModule } from '@angular/forms'; import { Router } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; +import { AuthService } from 'src/app/authentication/auth.service'; import { Category } from 'src/app/models/category.model'; +import { User } from 'src/app/models/user.model'; import { SharedModule } from 'src/app/shared/shared.module'; +import { UserLoginFormComponent } from 'src/app/users/user-login-form/user-login-form.component'; import { PostListComponent } from '../post-list/post-list.component'; import { PostService } from '../post.service'; @@ -14,9 +17,19 @@ describe('PostFormComponent', () => { let component: PostFormComponent; let fixture: ComponentFixture<PostFormComponent>; let router: Router; - let mockPostService; + let mockPostService, mockAuthService; beforeEach(async () => { + // AuthService mock setup + mockAuthService = jasmine.createSpyObj(['getCurrentUser']); + mockAuthService.getCurrentUser.and.returnValue(new User({ + userId: 4, + username: "tester", + email: "test@test.com", + password: "1234", + create_time: 513498 + })); + // PostService mock setup mockPostService = jasmine.createSpyObj(['getAllCategories', 'addPost', 'deletePost']); mockPostService.getAllCategories.and.returnValue( @@ -45,10 +58,14 @@ describe('PostFormComponent', () => { FormsModule, SharedModule, RouterTestingModule.withRoutes([ - { path: 'annonse', component: PostListComponent} + { path: 'annonse', component: PostListComponent}, + { path: 'login', component: UserLoginFormComponent} ]) ], - providers: [ { provide: PostService, useValue: mockPostService } ] + providers: [ + { provide: PostService, useValue: mockPostService }, + { provide: AuthService, useValue: mockAuthService } + ] }) .compileComponents(); }); @@ -61,16 +78,20 @@ describe('PostFormComponent', () => { router = TestBed.inject(Router); }); - it('should create and get all categories', async () => { - expect(component).toBeTruthy(); + it('should get current user', async () => { + // Waits for ngOnInit and checks that we get categories + await fixture.whenStable(); + expect(mockAuthService.getCurrentUser).toHaveBeenCalled(); + expect(component.currentUser.getUserId).toBe(4); + }); + it('should get all categories', async () => { // Waits for ngOnInit and checks that we get categories - fixture.whenStable().then(() => { - expect(mockPostService.getAllCategories).toHaveBeenCalled(); - expect(component.categories.length).toBe(2); - expect(component.categories[0].getCategoryId).toBe(1); - expect(component.categories[1].getName).toBe("Bil"); - }); + await fixture.whenStable(); + expect(mockPostService.getAllCategories).toHaveBeenCalled(); + expect(component.categories.length).toBe(2); + expect(component.categories[0].getCategoryId).toBe(1); + expect(component.categories[1].getName).toBe("Bil"); }); it('should validate form', () => { @@ -126,21 +147,17 @@ describe('PostFormComponent', () => { it('should delete post with id', async () => { component.id = 5; - expect(component.id).toBe(5); // Waits for ngOnInit and checks that we can delete post - fixture.whenStable().then(() => { - component.deletePost(); - expect(mockPostService.deletePost).toHaveBeenCalledWith(5); - }); + await fixture.whenStable(); + component.deletePost(); + expect(mockPostService.deletePost).toHaveBeenCalledWith(5); }); it('should not delete new post', async () => { // Waits for ngOnInit and checks that we can delete post - expect(component.id).toBe(0); - fixture.whenStable().then(() => { - component.deletePost(); - expect(mockPostService.deletePost).not.toHaveBeenCalledWith(5); - }); + await fixture.whenStable(); + component.deletePost(); + expect(mockPostService.deletePost).not.toHaveBeenCalledWith(5); }); }); diff --git a/client/src/app/shared/password-input/password-input.component.spec.ts b/client/src/app/shared/password-input/password-input.component.spec.ts index e1a583ebf97f377951e4c45c6a18ffa72574a073..14edd4419af19cd4ffc03d0edc00d4a547cb04d7 100644 --- a/client/src/app/shared/password-input/password-input.component.spec.ts +++ b/client/src/app/shared/password-input/password-input.component.spec.ts @@ -1,5 +1,6 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { FormsModule } from '@angular/forms'; +import { SharedModule } from '../shared.module'; import { PasswordInputComponent } from './password-input.component'; @@ -10,7 +11,7 @@ describe('PasswordInputComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [ PasswordInputComponent ], - imports: [ FormsModule ] + imports: [ SharedModule, FormsModule ] }) .compileComponents(); }); diff --git a/client/src/app/users/user-profile/user-profile.component.spec.ts b/client/src/app/users/user-profile/user-profile.component.spec.ts index 4855ff37655c5f682abfbb90e35fed5219aa1484..3cc34bfe7937ceaea07988e2bed0029d17671949 100644 --- a/client/src/app/users/user-profile/user-profile.component.spec.ts +++ b/client/src/app/users/user-profile/user-profile.component.spec.ts @@ -1,17 +1,40 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; +import { AuthService } from 'src/app/authentication/auth.service'; +import { User } from 'src/app/models/user.model'; +import { UserLoginFormComponent } from '../user-login-form/user-login-form.component'; import { UserProfileComponent } from './user-profile.component'; describe('UserProfileComponent', () => { let component: UserProfileComponent; let fixture: ComponentFixture<UserProfileComponent>; + let mockAuthService; beforeEach(async () => { + // AuthService mock setup + mockAuthService = jasmine.createSpyObj(['getCurrentUser']); + mockAuthService.getCurrentUser.and.returnValue(new User({ + userId: 4, + username: "tester", + email: "test@test.com", + password: "1234", + create_time: 513498 + })); + + await TestBed.configureTestingModule({ declarations: [ UserProfileComponent ], - imports: [ HttpClientTestingModule, RouterTestingModule ] + imports: [ + HttpClientTestingModule, + RouterTestingModule.withRoutes([ + { path: 'login', component: UserLoginFormComponent} + ]) + ], + providers: [ + { provide: AuthService, useValue: mockAuthService } + ] }) .compileComponents(); }); @@ -25,4 +48,16 @@ describe('UserProfileComponent', () => { it('should create', () => { expect(component).toBeTruthy(); }); + + it('should get current user', async () => { + await fixture.whenStable(); + expect(mockAuthService.getCurrentUser).toHaveBeenCalled(); + expect(component.user).toEqual(new User({ + userId: 4, + username: "tester", + email: "test@test.com", + password: "1234", + create_time: 513498 + })); + }); });