Skip to content
Snippets Groups Projects
Commit 298eb153 authored by Tormod Nygård's avatar Tormod Nygård
Browse files

Issue: Fixed and added some tests (#12)

parent 66f9121f
No related branches found
No related tags found
No related merge requests found
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing'; import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
...@@ -6,7 +7,8 @@ describe('AppComponent', () => { ...@@ -6,7 +7,8 @@ describe('AppComponent', () => {
beforeEach(async () => { beforeEach(async () => {
await TestBed.configureTestingModule({ await TestBed.configureTestingModule({
imports: [ imports: [
RouterTestingModule RouterTestingModule,
HttpClientTestingModule
], ],
declarations: [ declarations: [
AppComponent AppComponent
......
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { SharedModule } from '../shared/shared.module';
......
...@@ -2,7 +2,9 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; ...@@ -2,7 +2,9 @@ import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { AuthService } from 'src/app/authentication/auth.service';
import { Post } from 'src/app/models/post.model'; 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 { SharedModule } from 'src/app/shared/shared.module';
import { PostListComponent } from '../post-list/post-list.component'; import { PostListComponent } from '../post-list/post-list.component';
import { PostService } from '../post.service'; import { PostService } from '../post.service';
...@@ -12,9 +14,19 @@ import { PostDetailsComponent } from './post-details.component'; ...@@ -12,9 +14,19 @@ import { PostDetailsComponent } from './post-details.component';
describe('PostDetailsComponent', () => { describe('PostDetailsComponent', () => {
let component: PostDetailsComponent; let component: PostDetailsComponent;
let fixture: ComponentFixture<PostDetailsComponent>; let fixture: ComponentFixture<PostDetailsComponent>;
let mockPostService; let mockPostService, mockAuthService;
beforeEach(async () => { 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 // PostService mock setup
mockPostService = jasmine.createSpyObj(['getPost', 'deletePost']); mockPostService = jasmine.createSpyObj(['getPost', 'deletePost']);
mockPostService.getPost.and.returnValue( mockPostService.getPost.and.returnValue(
...@@ -25,7 +37,7 @@ describe('PostDetailsComponent', () => { ...@@ -25,7 +37,7 @@ describe('PostDetailsComponent', () => {
title: "Test", title: "Test",
description: "TestDescription", description: "TestDescription",
timestamp: 23947298, timestamp: 23947298,
owner: "user", owner: 4,
imageUrl: null, imageUrl: null,
price: 49, price: 49,
categoryid: 2 categoryid: 2
...@@ -50,7 +62,8 @@ describe('PostDetailsComponent', () => { ...@@ -50,7 +62,8 @@ describe('PostDetailsComponent', () => {
], ],
providers: [ providers: [
{ provide: ActivatedRoute, useValue: { snapshot: {params: {id: 5}}}}, { provide: ActivatedRoute, useValue: { snapshot: {params: {id: 5}}}},
{ provide: PostService, useValue: mockPostService } { provide: PostService, useValue: mockPostService },
{ provide: AuthService, useValue: mockAuthService }
] ]
}) })
.compileComponents(); .compileComponents();
...@@ -68,30 +81,32 @@ describe('PostDetailsComponent', () => { ...@@ -68,30 +81,32 @@ describe('PostDetailsComponent', () => {
it('should get post with id from url parameter', async () => { it('should get post with id from url parameter', async () => {
// Waits for ngOnInit and checks that we get post // 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(() => { it('should get current user', async () => {
expect(mockPostService.getPost).toHaveBeenCalledWith(5); // Waits for ngOnInit and checks that we get post
expect(component.post).toEqual(new Post({ await fixture.whenStable();
id: 5,
title: "Test", expect(mockAuthService.getCurrentUser).toHaveBeenCalledWith(false);
description: "TestDescription", expect(component.userId).toBe(4);
timestamp: 23947298,
owner: "user",
imageUrl: null,
price: 49,
categoryid: 2
}));
});
}); });
it('should delete post with id', async () => { it('should delete post with id', async () => {
// Waits for ngOnInit and checks that we can delete post // Waits for ngOnInit and checks that we can delete post
expect(component.post).not.toBeNull(); await fixture.whenStable();
component.deletePost();
fixture.whenStable().then(() => { expect(mockPostService.deletePost).toHaveBeenCalledWith(5);
component.deletePost();
expect(mockPostService.deletePost).toHaveBeenCalledWith(5);
});
}); });
}); });
...@@ -17,12 +17,12 @@ export class PostDetailsComponent implements OnInit { ...@@ -17,12 +17,12 @@ export class PostDetailsComponent implements OnInit {
constructor(private postService: PostService, private activatedRoute: ActivatedRoute, private router: Router, private authService: AuthService) { } constructor(private postService: PostService, private activatedRoute: ActivatedRoute, private router: Router, private authService: AuthService) { }
ngOnInit(): void { ngOnInit(): void {
// Gets id parameter from URL
const id = this.activatedRoute.snapshot.params["id"];
// Gets ID from current user // Gets ID from current user
this.userId = this.authService.getCurrentUser(false).getUserId; 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 // Gets Post with id from database
this.postService.getPost(id).then(post => { this.postService.getPost(id).then(post => {
this.post = post; this.post = post;
......
...@@ -3,8 +3,11 @@ import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing'; ...@@ -3,8 +3,11 @@ import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { AuthService } from 'src/app/authentication/auth.service';
import { Category } from 'src/app/models/category.model'; 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 { 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 { PostListComponent } from '../post-list/post-list.component';
import { PostService } from '../post.service'; import { PostService } from '../post.service';
...@@ -14,9 +17,19 @@ describe('PostFormComponent', () => { ...@@ -14,9 +17,19 @@ describe('PostFormComponent', () => {
let component: PostFormComponent; let component: PostFormComponent;
let fixture: ComponentFixture<PostFormComponent>; let fixture: ComponentFixture<PostFormComponent>;
let router: Router; let router: Router;
let mockPostService; let mockPostService, mockAuthService;
beforeEach(async () => { 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 // PostService mock setup
mockPostService = jasmine.createSpyObj(['getAllCategories', 'addPost', 'deletePost']); mockPostService = jasmine.createSpyObj(['getAllCategories', 'addPost', 'deletePost']);
mockPostService.getAllCategories.and.returnValue( mockPostService.getAllCategories.and.returnValue(
...@@ -45,10 +58,14 @@ describe('PostFormComponent', () => { ...@@ -45,10 +58,14 @@ describe('PostFormComponent', () => {
FormsModule, FormsModule,
SharedModule, SharedModule,
RouterTestingModule.withRoutes([ 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(); .compileComponents();
}); });
...@@ -61,16 +78,20 @@ describe('PostFormComponent', () => { ...@@ -61,16 +78,20 @@ describe('PostFormComponent', () => {
router = TestBed.inject(Router); router = TestBed.inject(Router);
}); });
it('should create and get all categories', async () => { it('should get current user', async () => {
expect(component).toBeTruthy(); // 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 // Waits for ngOnInit and checks that we get categories
fixture.whenStable().then(() => { await fixture.whenStable();
expect(mockPostService.getAllCategories).toHaveBeenCalled(); expect(mockPostService.getAllCategories).toHaveBeenCalled();
expect(component.categories.length).toBe(2); expect(component.categories.length).toBe(2);
expect(component.categories[0].getCategoryId).toBe(1); expect(component.categories[0].getCategoryId).toBe(1);
expect(component.categories[1].getName).toBe("Bil"); expect(component.categories[1].getName).toBe("Bil");
});
}); });
it('should validate form', () => { it('should validate form', () => {
...@@ -126,21 +147,17 @@ describe('PostFormComponent', () => { ...@@ -126,21 +147,17 @@ describe('PostFormComponent', () => {
it('should delete post with id', async () => { it('should delete post with id', async () => {
component.id = 5; component.id = 5;
expect(component.id).toBe(5);
// Waits for ngOnInit and checks that we can delete post // Waits for ngOnInit and checks that we can delete post
fixture.whenStable().then(() => { await fixture.whenStable();
component.deletePost(); component.deletePost();
expect(mockPostService.deletePost).toHaveBeenCalledWith(5); expect(mockPostService.deletePost).toHaveBeenCalledWith(5);
});
}); });
it('should not delete new post', async () => { it('should not delete new post', async () => {
// Waits for ngOnInit and checks that we can delete post // Waits for ngOnInit and checks that we can delete post
expect(component.id).toBe(0); await fixture.whenStable();
fixture.whenStable().then(() => { component.deletePost();
component.deletePost(); expect(mockPostService.deletePost).not.toHaveBeenCalledWith(5);
expect(mockPostService.deletePost).not.toHaveBeenCalledWith(5);
});
}); });
}); });
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { SharedModule } from '../shared.module';
import { PasswordInputComponent } from './password-input.component'; import { PasswordInputComponent } from './password-input.component';
...@@ -10,7 +11,7 @@ describe('PasswordInputComponent', () => { ...@@ -10,7 +11,7 @@ describe('PasswordInputComponent', () => {
beforeEach(async () => { beforeEach(async () => {
await TestBed.configureTestingModule({ await TestBed.configureTestingModule({
declarations: [ PasswordInputComponent ], declarations: [ PasswordInputComponent ],
imports: [ FormsModule ] imports: [ SharedModule, FormsModule ]
}) })
.compileComponents(); .compileComponents();
}); });
......
import { HttpClientTestingModule } from '@angular/common/http/testing'; import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/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'; import { UserProfileComponent } from './user-profile.component';
describe('UserProfileComponent', () => { describe('UserProfileComponent', () => {
let component: UserProfileComponent; let component: UserProfileComponent;
let fixture: ComponentFixture<UserProfileComponent>; let fixture: ComponentFixture<UserProfileComponent>;
let mockAuthService;
beforeEach(async () => { 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({ await TestBed.configureTestingModule({
declarations: [ UserProfileComponent ], declarations: [ UserProfileComponent ],
imports: [ HttpClientTestingModule, RouterTestingModule ] imports: [
HttpClientTestingModule,
RouterTestingModule.withRoutes([
{ path: 'login', component: UserLoginFormComponent}
])
],
providers: [
{ provide: AuthService, useValue: mockAuthService }
]
}) })
.compileComponents(); .compileComponents();
}); });
...@@ -25,4 +48,16 @@ describe('UserProfileComponent', () => { ...@@ -25,4 +48,16 @@ describe('UserProfileComponent', () => {
it('should create', () => { it('should create', () => {
expect(component).toBeTruthy(); 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
}));
});
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment