-
Notifications
You must be signed in to change notification settings - Fork 68
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
Add profiles component tests #971
Merged
kkangs0226
merged 6 commits into
CATcher-org:master
from
snss231:add-profiles-component-tests
Nov 13, 2022
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f36edf8
Add profiles component DOM tests
snss231 4598e14
Merge branch 'master' into add-profiles-component-tests
snss231 82f421f
Merge branch 'master' into add-profiles-component-tests
chunweii cbc89ac
Merge branch 'master' into add-profiles-component-tests
snss231 ed075b5
Merge branch 'master' into add-profiles-component-tests
snss231 0908450
Edit spec description to specify selectProfile
snss231 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { Component, DebugElement } from '@angular/core'; | ||
import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; | ||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; | ||
|
||
import { ProfilesComponent } from '../../../../src/app/auth/profiles/profiles.component'; | ||
import { Profile } from '../../../../src/app/core/models/profile.model'; | ||
import { ErrorHandlingService } from '../../../../src/app/core/services/error-handling.service'; | ||
import { ProfileService } from '../../../../src/app/core/services/profile.service'; | ||
import { SharedModule } from '../../../../src/app/shared/shared.module'; | ||
|
||
@Component({ | ||
template: `<app-profiles (selectedProfileEmitter)="onSelected($event)"> </app-profiles>` | ||
}) | ||
class TestHostComponent { | ||
selectedProfile: Profile | undefined; | ||
onSelected(profile: Profile) { | ||
this.selectedProfile = profile; | ||
} | ||
} | ||
|
||
describe('ProfilesComponent', () => { | ||
let fixture: ComponentFixture<TestHostComponent>; | ||
let debugElement: DebugElement; | ||
let nativeElement: HTMLElement; | ||
let testHost: TestHostComponent; | ||
let profilesEl: HTMLElement; | ||
|
||
const profileService = jasmine.createSpyObj('ProfileService', ['fetchExternalProfiles']); | ||
const errorHandlingService = jasmine.createSpyObj('ErrorHandlingService', ['handleError']); | ||
|
||
const testProfiles: Profile[] = [ | ||
{ | ||
profileName: 'testProfile1', | ||
repoName: 'test-org1/pe' | ||
}, | ||
{ | ||
profileName: 'testProfile2', | ||
repoName: 'test-org2/pe' | ||
}, | ||
{ | ||
profileName: 'testProfile3', | ||
repoName: 'test-org3/pe' | ||
} | ||
]; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [BrowserAnimationsModule, SharedModule], | ||
declarations: [ProfilesComponent, TestHostComponent], | ||
providers: [ | ||
{ provide: ProfileService, useValue: profileService }, | ||
{ provide: ErrorHandlingService, useValue: errorHandlingService } | ||
] | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(fakeAsync(() => { | ||
profileService.fetchExternalProfiles.and.returnValue(Promise.resolve(testProfiles)); | ||
|
||
fixture = TestBed.createComponent(TestHostComponent); | ||
testHost = fixture.componentInstance; | ||
|
||
debugElement = fixture.debugElement; | ||
nativeElement = debugElement.nativeElement; | ||
profilesEl = <HTMLElement>nativeElement.querySelector('app-profiles'); | ||
|
||
fixture.detectChanges(); // onInit() | ||
tick(); // wait for profiles to be loaded | ||
})); | ||
|
||
it('should display the correct profiles from AppConfig', () => { | ||
openMatSelect(); | ||
const displayedOptions = getOptions(); | ||
|
||
displayedOptions.slice(1).forEach((el, i) => { | ||
const optionTextEl = <HTMLElement>el.querySelector('.mat-option-text'); | ||
if (!optionTextEl) { | ||
fail('optionTextEl should not be null'); | ||
} | ||
const profileName = optionTextEl.innerText; | ||
expect(profileName).toBe(testProfiles[i].profileName); | ||
}); | ||
}); | ||
|
||
it('should emit the correct profile when mat-option is clicked', () => { | ||
openMatSelect(); | ||
const displayedOptions = getOptions(); | ||
|
||
displayedOptions[3].click(); | ||
expect(testHost.selectedProfile).toEqual(testProfiles[2]); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
function openMatSelect(): void { | ||
const select = <HTMLElement>profilesEl.querySelector('.mat-select'); | ||
if (!select) { | ||
fail('Select should not be null'); | ||
} | ||
select.click(); | ||
fixture.detectChanges(); | ||
} | ||
|
||
function getOptions(): HTMLElement[] { | ||
return Array.from(document.querySelectorAll('.mat-option')); | ||
} | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be clearer if we specify the name of the function
selectProfile
that is triggered when mat-option is clickedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the spec description to
should emit the correct profile through selectProfile when mat-option is clicked
. Hope that is satisfactory!