Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor test cases for Login Component, Session Model and Conflict Model #241

Merged
merged 7 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 49 additions & 14 deletions tests/app/auth/login/login.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { LoginComponent } from '../../../../src/app/auth/login/login.component';
import { AuthService } from '../../../../src/app/core/services/auth.service';
import { AuthService, AuthState } from '../../../../src/app/core/services/auth.service';
import { ErrorHandlingService } from '../../../../src/app/core/services/error-handling.service';
import { LoggingService } from '../../../../src/app/core/services/logging.service';

describe('LoginComponent', () => {
let authService: jasmine.SpyObj<AuthService>;
let errorHandlingService: jasmine.SpyObj<ErrorHandlingService>;
let logger: jasmine.SpyObj<LoggingService>;
let authServiceSpy: jasmine.SpyObj<AuthService>;
let errorHandlingServiceSpy: jasmine.SpyObj<ErrorHandlingService>;
let loggingServiceSpy: jasmine.SpyObj<LoggingService>;
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;

beforeEach(
waitForAsync(() => {
authService = jasmine.createSpyObj<AuthService>('AuthService', ['startOAuthProcess']);
logger = jasmine.createSpyObj<LoggingService>('LoggingService', ['info']);
errorHandlingService = jasmine.createSpyObj<ErrorHandlingService>('ErrorHandlingService', ['handleError']);
authServiceSpy = jasmine.createSpyObj<AuthService>('AuthService', ['startOAuthProcess', 'changeAuthState']);
loggingServiceSpy = jasmine.createSpyObj<LoggingService>('LoggingService', ['info']);
errorHandlingServiceSpy = jasmine.createSpyObj<ErrorHandlingService>('ErrorHandlingService', ['handleError']);

TestBed.configureTestingModule({
providers: [
{ provide: AuthService, useValue: authService },
{ provide: LoggingService, useValue: logger },
{ provide: ErrorHandlingService, useValue: errorHandlingService }
{ provide: AuthService, useValue: authServiceSpy },
{ provide: LoggingService, useValue: loggingServiceSpy },
{ provide: ErrorHandlingService, useValue: errorHandlingServiceSpy }
],
declarations: [LoginComponent]
}).compileComponents();
Expand All @@ -36,10 +36,45 @@ describe('LoginComponent', () => {
expect(component).toBeTruthy();
});

it('should start login process', () => {
component.startLoginProcess();
it('startPublicOnlyLoginProcess should call startLoginProcess with false', () => {
spyOn(component, 'startLoginProcess');

expect(logger.info).toHaveBeenCalled();
expect(authService.startOAuthProcess).toHaveBeenCalled();
component.startPublicOnlyLoginProcess();

expect(component.startLoginProcess).toHaveBeenCalledWith(false);
});

it('startIncludePrivateLoginProcess should call startLoginProcess with true', () => {
spyOn(component, 'startLoginProcess');

component.startIncludePrivateLoginProcess();

expect(component.startLoginProcess).toHaveBeenCalledWith(true);
});

it('should call authService.startOAuthProcess on startLoginProcess', () => {
const hasPrivateConsent = false;

component.startLoginProcess(hasPrivateConsent);

expect(authServiceSpy.startOAuthProcess).toHaveBeenCalledWith(hasPrivateConsent);
expect(loggingServiceSpy.info).toHaveBeenCalledWith('LoginComponent: Beginning login process');
});

it('should call error handling methods when error is thrown', () => {
const hasPrivateConsent = false;
const errorMessage = 'Error!';

const error: Error = new Error(errorMessage);
authServiceSpy.startOAuthProcess.and.throwError(error);

component.startLoginProcess(hasPrivateConsent);

expect(authServiceSpy.changeAuthState).toHaveBeenCalledWith(AuthState.NotAuthenticated);
expect(errorHandlingServiceSpy.handleError).toHaveBeenCalledWith(error);
expect(loggingServiceSpy.info.calls.allArgs()).toEqual([
['LoginComponent: Beginning login process'],
[`LoginComponent: Login process failed with an error: ${error}`]
]);
});
});
49 changes: 0 additions & 49 deletions tests/app/core/models/conflict/conflict.model.spec.ts

This file was deleted.

52 changes: 26 additions & 26 deletions tests/app/core/models/session-model.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { of } from 'rxjs';
import { Phase } from '../../../../src/app/core/models/phase.model';
import {
assertSessionDataIntegrity,
NO_ACCESSIBLE_PHASES,
NO_VALID_OPEN_PHASES,
OPENED_PHASE_REPO_UNDEFINED,
SESSION_DATA_MISSING_OPENPHASES_KEY,
SESSION_DATA_MISSING_FIELDS,
SESSION_DATA_UNAVAILABLE
} from '../../../../src/app/core/models/session.model';
import { BUG_REPORTING_PHASE_SESSION_DATA } from '../../../constants/session.constants';
import { VALID_SESSION_DATA, WATCHER_REPO } from '../../../constants/session.constants';

describe('Session Model', () => {
describe('assertSessionDataIntegrity()', () => {
Expand All @@ -20,67 +20,67 @@ describe('Session Model', () => {
});
});

it('should throw error on session data with missing crucial values', () => {
of({ dummyKey: undefined })
it('should throw error on session data with invalid session', () => {
of({ sessionRepo: null })
.pipe(assertSessionDataIntegrity())
.subscribe({
next: () => fail(),
error: (err) => expect(err).toEqual(new Error(SESSION_DATA_MISSING_OPENPHASES_KEY))
error: (err) => expect(err).toEqual(new Error(SESSION_DATA_MISSING_FIELDS))
});
});

it('should throw error on session with no open phases', () => {
of(NO_OPEN_PHASES_SESSION_DATA)
of({ sessionRepo: [] })
.pipe(assertSessionDataIntegrity())
.subscribe({
next: () => fail(),
error: (err) => expect(err).toEqual(new Error(SESSION_DATA_MISSING_FIELDS))
});
of({ sessionRepo: 'repo' })
.pipe(assertSessionDataIntegrity())
.subscribe({
next: () => fail(),
error: (err) => expect(err).toEqual(new Error(NO_ACCESSIBLE_PHASES))
error: (err) => expect(err).toEqual(new Error(SESSION_DATA_MISSING_FIELDS))
});
});

it('should throw error on session data with invalid open phases', () => {
of({ ...BUG_REPORTING_PHASE_SESSION_DATA, openPhases: ['unknownPhase'] })
it('should throw error on session with invalid phases', () => {
of({ sessionRepo: [{ phase: 'invalidPhase' as Phase, repos: [WATCHER_REPO] }] })
.pipe(assertSessionDataIntegrity())
.subscribe({
next: () => fail(),
error: (err) => expect(err).toEqual(new Error(NO_VALID_OPEN_PHASES))
});
});

it('should throw error on session data with undefined repo for open phase', () => {
of({ ...BUG_REPORTING_PHASE_SESSION_DATA, phaseBugReporting: undefined })
it('should throw error on session data with invalid repo', () => {
of({ sessionRepo: [{ phase: Phase.issuesViewer, repo: undefined }] })
.pipe(assertSessionDataIntegrity())
.subscribe({
next: () => fail(),
error: (err) => expect(err).toEqual(new Error(OPENED_PHASE_REPO_UNDEFINED))
});
of({ ...BUG_REPORTING_PHASE_SESSION_DATA, phaseBugReporting: null })
of({ sessionRepo: [{ phase: Phase.issuesViewer, repo: null }] })
.pipe(assertSessionDataIntegrity())
.subscribe({
next: () => fail(),
error: (err) => expect(err).toEqual(new Error(OPENED_PHASE_REPO_UNDEFINED))
});
of({ ...BUG_REPORTING_PHASE_SESSION_DATA, phaseBugReporting: '' })
of({ sessionRepo: [{ phase: Phase.issuesViewer, repo: '' }] })
.pipe(assertSessionDataIntegrity())
.subscribe({
next: () => fail(),
error: (err) => expect(err).toEqual(new Error(OPENED_PHASE_REPO_UNDEFINED))
});
});

it('should not throw error if session data contains repo information of unopened phases', () => {
of(BUG_REPORTING_PHASE_SESSION_DATA)
of({ sessionRepo: [{ phase: Phase.issuesViewer, repo: [] }] })
.pipe(assertSessionDataIntegrity())
.subscribe({
next: (el) => expect(el).toEqual(BUG_REPORTING_PHASE_SESSION_DATA),
error: () => fail()
next: () => fail(),
error: (err) => expect(err).toEqual(new Error(OPENED_PHASE_REPO_UNDEFINED))
});
});

it('should pass valid session data', () => {
of(BUG_REPORTING_PHASE_SESSION_DATA)
it('should pass for valid session data', () => {
of(VALID_SESSION_DATA)
.pipe(assertSessionDataIntegrity())
.subscribe((el) => expect(el).toEqual(BUG_REPORTING_PHASE_SESSION_DATA));
.subscribe((el) => expect(el).toEqual(VALID_SESSION_DATA));
});
});
});
28 changes: 11 additions & 17 deletions tests/constants/session.constants.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import { Phase } from '../../src/app/core/models/phase.model';
import { SessionData } from '../../src/app/core/models/session.model';
import { Repo } from '../../src/app/core/models/repo.model';
import { SessionData, SessionRepo } from '../../src/app/core/models/session.model';

export const BUG_REPORTING_PHASE_SESSION_DATA: SessionData = {
openPhases: [Phase.phaseBugReporting],
[Phase.phaseBugReporting]: 'bugreporting',
[Phase.phaseTeamResponse]: 'pe-results',
[Phase.phaseTesterResponse]: 'testerresponse',
[Phase.phaseModeration]: 'pe-evaluation'
};
export const WATCHER_REPO: Repo = Repo.of('CATcher-org/WATcher');

export const MODERATION_PHASE_SESSION_DATA: SessionData = {
...BUG_REPORTING_PHASE_SESSION_DATA,
openPhases: [Phase.phaseModeration]
const ISSUES_VIEWER_SESSION_REPO: SessionRepo = {
phase: Phase.issuesViewer,
repos: [WATCHER_REPO]
};

export const NO_OPEN_PHASES_SESSION_DATA: SessionData = {
...BUG_REPORTING_PHASE_SESSION_DATA,
openPhases: []
const ACTIVITY_DASHBOARD_SESSION_REPO: SessionRepo = {
phase: Phase.activityDashboard,
repos: [WATCHER_REPO]
};

export const MULTIPLE_OPEN_PHASES_SESSION_DATA: SessionData = {
...BUG_REPORTING_PHASE_SESSION_DATA,
openPhases: [Phase.phaseBugReporting, Phase.phaseTeamResponse, Phase.phaseTesterResponse, Phase.phaseModeration]
export const VALID_SESSION_DATA: SessionData = {
sessionRepo: [ISSUES_VIEWER_SESSION_REPO, ACTIVITY_DASHBOARD_SESSION_REPO]
};
Loading