Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
alisaduncan committed Dec 6, 2021
1 parent 95683b3 commit b45d4a0
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 28 deletions.
21 changes: 7 additions & 14 deletions src/app/app.component.spec.ts
@@ -1,16 +1,22 @@
import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { MatToolbarModule } from '@angular/material/toolbar';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
RouterTestingModule
RouterTestingModule,
MatToolbarModule,
NoopAnimationsModule
],
declarations: [
AppComponent
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
});

Expand All @@ -19,17 +25,4 @@ describe('AppComponent', () => {
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});

it(`should have as title 'okta-angular-dynamic-components'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('okta-angular-dynamic-components');
});

it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.querySelector('.content span')?.textContent).toContain('okta-angular-dynamic-components app is running!');
});
});
35 changes: 34 additions & 1 deletion src/app/menu/menu.component.spec.ts
@@ -1,14 +1,41 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatMenuModule } from '@angular/material/menu';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';
import { OktaAuthStateService } from '@okta/okta-angular';
import { AuthState, OktaAuth } from '@okta/okta-auth-js';
import { of } from 'rxjs';

import { MenuComponent } from './menu.component';

describe('MenuComponent', () => {
let component: MenuComponent;
let fixture: ComponentFixture<MenuComponent>;

const authState: AuthState = {
isAuthenticated: false
};

let authStateSpy = jasmine.createSpyObj<OktaAuthStateService>([],
{authState$: of(authState)});
let authSpy = jasmine.createSpyObj<OktaAuth>(['signInWithRedirect', 'signOut']);

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ MenuComponent ]
declarations: [ MenuComponent ],
imports: [
RouterTestingModule,
MatMenuModule,
MatButtonModule,
MatIconModule,
NoopAnimationsModule
],
providers: [
{ provide: OktaAuthStateService, useValue: authStateSpy },
{ provide: OktaAuth, useValue: authSpy }
]
})
.compileComponents();
});
Expand All @@ -22,4 +49,10 @@ describe('MenuComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

it('should call signInWithRedirect with logging in', () => {
authSpy.signInWithRedirect.and.resolveTo();
component.login();
expect(authSpy.signInWithRedirect).toHaveBeenCalled();
});
});
58 changes: 50 additions & 8 deletions src/app/profile/profile.component.spec.ts
@@ -1,25 +1,67 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { OktaAuthStateService } from '@okta/okta-angular';
import { UserClaims, IDToken, AuthState } from '@okta/okta-auth-js';
import { of } from 'rxjs';

import { ProfileComponent } from './profile.component';

describe('ProfileComponent', () => {
let component: ProfileComponent;
let fixture: ComponentFixture<ProfileComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ ProfileComponent ]
})
.compileComponents();
});
const claims: UserClaims = {
sub: 'sub',
name: 'Test Name'
};

const idToken: IDToken = {
idToken: 'token',
clientId: 'client',
issuer: 'issuer',
authorizeUrl: 'authorize',
expiresAt: 123,
scopes: [],
claims
};

const authState: AuthState = {
isAuthenticated: true,
idToken
};

let authStateSpy = jasmine.createSpyObj<OktaAuthStateService>([],['authState$']);


beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ ProfileComponent ],
providers: [
{ provide: OktaAuthStateService, useValue: authStateSpy }
]
});
});

it('should show text that welcomes the user by name', () => {
(Object.getOwnPropertyDescriptor(authStateSpy, 'authState$')?.get as jasmine.Spy).and.returnValue(of(authState));
fixture = TestBed.createComponent(ProfileComponent);
component = fixture.componentInstance;
fixture.detectChanges();
const spanEl = fixture.debugElement.query(By.css('span'));
expect(spanEl).toBeTruthy();
expect(spanEl.nativeElement.innerHTML).toEqual('Test Name');
});

it('should create', () => {
expect(component).toBeTruthy();
it('should show text that welcomes a user with no name', () => {
(Object.getOwnPropertyDescriptor(authStateSpy, 'authState$')?.get as jasmine.Spy).and.returnValue(of({
isAuthenticated: true,
idToken: {...idToken, claims: {sub: 'sub'}}
}));

fixture = TestBed.createComponent(ProfileComponent);
component = fixture.componentInstance;
fixture.detectChanges();
const spanEl = fixture.debugElement.query(By.css('span'));
expect(spanEl).toBeFalsy();
});
});
9 changes: 8 additions & 1 deletion src/app/protected/department/clawesome.component.spec.ts
@@ -1,4 +1,6 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatCardModule } from '@angular/material/card';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

import { ClawesomeComponent } from './clawesome.component';

Expand All @@ -8,14 +10,19 @@ describe('ClawesomeComponent', () => {

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ ClawesomeComponent ]
declarations: [ ClawesomeComponent ],
imports: [
MatCardModule,
NoopAnimationsModule
]
})
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(ClawesomeComponent);
component = fixture.componentInstance;
component.data = {url: '', content: 'testing'};
fixture.detectChanges();
});

Expand Down
3 changes: 2 additions & 1 deletion src/app/protected/department/department.component.spec.ts
@@ -1,14 +1,15 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { DepartmentComponent } from './department.component';
import { DynamicDirective } from './dynamic.directive';

describe('DepartmentComponent', () => {
let component: DepartmentComponent;
let fixture: ComponentFixture<DepartmentComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ DepartmentComponent ]
declarations: [ DepartmentComponent, DynamicDirective ]
})
.compileComponents();
});
Expand Down
9 changes: 8 additions & 1 deletion src/app/protected/department/pawesome.component.spec.ts
@@ -1,4 +1,6 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatCardModule } from '@angular/material/card';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

import { PawesomeComponent } from './pawesome.component';

Expand All @@ -8,14 +10,19 @@ describe('PawesomeComponent', () => {

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ PawesomeComponent ]
declarations: [ PawesomeComponent ],
imports: [
MatCardModule,
NoopAnimationsModule
]
})
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(PawesomeComponent);
component = fixture.componentInstance;
component.data = {url: '', content: { about: 'testing', name: 'test'}};
fixture.detectChanges();
});

Expand Down
9 changes: 8 additions & 1 deletion src/app/protected/department/smiley.component.spec.ts
@@ -1,4 +1,6 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatCardModule } from '@angular/material/card';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

import { SmileyComponent } from './smiley.component';

Expand All @@ -8,14 +10,19 @@ describe('SmileyComponent', () => {

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ SmileyComponent ]
declarations: [ SmileyComponent ],
imports: [
MatCardModule,
NoopAnimationsModule
]
})
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(SmileyComponent);
component = fixture.componentInstance;
component.data = {url: ''};
fixture.detectChanges();
});

Expand Down
38 changes: 37 additions & 1 deletion src/app/protected/protected.component.spec.ts
@@ -1,14 +1,50 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { ProtectedComponent } from './protected.component';
import { AuthState } from '@okta/okta-auth-js';
import { OktaAuthStateService } from '@okta/okta-angular';
import { of } from 'rxjs';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { MatListModule } from '@angular/material/list';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { MessageService } from '../message.service';

describe('ProtectedComponent', () => {
let component: ProtectedComponent;
let fixture: ComponentFixture<ProtectedComponent>;

const authState: AuthState = {
isAuthenticated: true,
idToken: {
idToken: 'token',
clientId: 'client',
issuer: 'issuer',
authorizeUrl: 'authorize',
expiresAt: 123,
scopes: [],
claims: {
sub: 'sub',
name: 'Test Name',
department: 3
}
}
};

let authStateSpy = jasmine.createSpyObj<OktaAuthStateService>([],
{authState$: of(authState)});

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ ProtectedComponent ]
declarations: [ ProtectedComponent ],
imports: [
MatListModule,
NoopAnimationsModule
],
providers: [
{ provide: OktaAuthStateService, useValue: authStateSpy },
{ provide: MessageService, useValue: {getMessages: (d: number) => []}}
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
.compileComponents();
});
Expand Down

0 comments on commit b45d4a0

Please sign in to comment.