From 298eb153d0e9cd351b340a7cf74a8d971612846f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tormod=20Nyg=C3=A5rd?= <tormodny@stud.ntnu.no> Date: Mon, 15 Mar 2021 14:28:33 +0100 Subject: [PATCH] Issue: Fixed and added some tests (#12) --- client/src/app/app.component.spec.ts | 4 +- client/src/app/authentication/auth.module.ts | 1 + .../post-details.component.spec.ts | 61 ++++++++++++------- .../post-details/post-details.component.ts | 6 +- .../post-form/post-form.component.spec.ts | 59 +++++++++++------- .../password-input.component.spec.ts | 3 +- .../user-profile.component.spec.ts | 37 ++++++++++- 7 files changed, 121 insertions(+), 50 deletions(-) diff --git a/client/src/app/app.component.spec.ts b/client/src/app/app.component.spec.ts index b4db530..31481af 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 d2e4fae..65c0542 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 dd08767..a2ab8bf 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 d995c98..efa7fbe 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 b16d0c1..adfab33 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 e1a583e..14edd44 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 4855ff3..3cc34bf 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 + })); + }); }); -- GitLab