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

Issues/12844 improve test env #12853

Merged
merged 4 commits into from
Oct 12, 2022
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
62 changes: 62 additions & 0 deletions templates/app-nolayers/angular/src/app/home/home.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { CoreTestingModule } from '@abp/ng.core/testing';
import { ThemeBasicTestingModule } from '@abp/ng.theme.basic/testing';
import { ThemeSharedTestingModule } from '@abp/ng.theme.shared/testing';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { NgxValidateCoreModule } from '@ngx-validate/core';
import { HomeComponent } from './home.component';
import { OAuthService } from 'angular-oauth2-oidc';
import { AuthService } from '@abp/ng.core';

describe('HomeComponent', () => {
let fixture: ComponentFixture<HomeComponent>;
const mockOAuthService = jasmine.createSpyObj('OAuthService', ['hasValidAccessToken']);
const mockAuthService = jasmine.createSpyObj('AuthService', ['navigateToLogin']);
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [HomeComponent],
imports: [
CoreTestingModule.withConfig(),
ThemeSharedTestingModule.withConfig(),
ThemeBasicTestingModule.withConfig(),
NgxValidateCoreModule,
],
providers: [
/* mock providers here */
{
provide: OAuthService,
useValue: mockOAuthService,
},
{
provide: AuthService,
useValue: mockAuthService,
},
],
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(HomeComponent);
fixture.detectChanges();
});

it('should be initiated', () => {
expect(fixture.componentInstance).toBeTruthy();
});

describe('when login state is true', () => {
beforeAll(() => {
mockOAuthService.hasValidAccessToken.and.returnValue(true);
});

it('hasLoggedIn should be true', () => {
expect(fixture.componentInstance.hasLoggedIn).toBeTrue();
expect(mockOAuthService.hasValidAccessToken).toHaveBeenCalled();
});

it('button should not be exists', () => {
const element = fixture.nativeElement;
const cardTitle = element.querySelector('.card-title');
expect(cardTitle).toBeTruthy();
});
});
});
3 changes: 1 addition & 2 deletions templates/app-nolayers/angular/src/test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import 'zone.js/testing';
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';
import 'zone.js/testing';

declare const require: {
context(
Expand Down
100 changes: 100 additions & 0 deletions templates/app/angular/src/app/home/home.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { CoreTestingModule } from "@abp/ng.core/testing";
import { ThemeBasicTestingModule } from "@abp/ng.theme.basic/testing";
import { ThemeSharedTestingModule } from "@abp/ng.theme.shared/testing";
import { ComponentFixture, TestBed, waitForAsync } from "@angular/core/testing";
import { NgxValidateCoreModule } from "@ngx-validate/core";
import { HomeComponent } from "./home.component";
import { OAuthService } from 'angular-oauth2-oidc';
import { AuthService } from '@abp/ng.core';


describe("HomeComponent", () => {
let fixture: ComponentFixture<HomeComponent>;
const mockOAuthService = jasmine.createSpyObj('OAuthService', ['hasValidAccessToken'])
const mockAuthService = jasmine.createSpyObj('AuthService', ['navigateToLogin'])
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [HomeComponent],
imports: [
CoreTestingModule.withConfig(),
ThemeSharedTestingModule.withConfig(),
ThemeBasicTestingModule.withConfig(),
NgxValidateCoreModule,
],
providers: [
/* mock providers here */
{
provide: OAuthService,
useValue: mockOAuthService
},
{
provide: AuthService,
useValue: mockAuthService
}
],
}).compileComponents();
})
);

beforeEach(() => {
fixture = TestBed.createComponent(HomeComponent);
fixture.detectChanges();
});

it("should be initiated", () => {
expect(fixture.componentInstance).toBeTruthy();
});



describe('when login state is true', () => {
beforeAll(() => {
mockOAuthService.hasValidAccessToken.and.returnValue(true)
});

it("hasLoggedIn should be true", () => {

expect(fixture.componentInstance.hasLoggedIn).toBeTrue();
expect(mockOAuthService.hasValidAccessToken).toHaveBeenCalled()
})

it("button should not be exists", () => {
const element = fixture.nativeElement
const button = element.querySelector('[role="button"]')
expect(button).toBeNull()
})

})

describe('when login state is false', () => {
beforeAll(() => {
mockOAuthService.hasValidAccessToken.and.returnValue(false)
});

it("hasLoggedIn should be false", () => {

expect(fixture.componentInstance.hasLoggedIn).toBeFalse();
expect(mockOAuthService.hasValidAccessToken).toHaveBeenCalled()
})

it("button should be exists", () => {
const element = fixture.nativeElement
const button = element.querySelector('[role="button"]')
expect(button).toBeDefined()
})
describe('when button clicked', () => {

beforeEach(() => {
const element = fixture.nativeElement
const button = element.querySelector('[role="button"]')
button.click()
});

it("navigateToLogin have been called", () => {
expect(mockAuthService.navigateToLogin).toHaveBeenCalled()
})
})
})

});
4 changes: 2 additions & 2 deletions templates/app/angular/src/test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import 'zone.js/testing';
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';
import 'zone.js/testing';


declare const require: {
context(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';

import { MyProjectNameComponent } from './my-project-name.component';
import { MyProjectNameComponent } from './components/my-project-name.component';
import { MyProjectNameService } from '@my-company-name/my-project-name';
import { of } from 'rxjs';

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

const mockMyProjectNameService = jasmine.createSpyObj('MyProjectNameService', {
sample: of([]),
});
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [ MyProjectNameComponent ]
})
.compileComponents();
declarations: [MyProjectNameComponent],
providers: [
{
provide: MyProjectNameService,
useValue: mockMyProjectNameService,
},
],
}).compileComponents();
}));

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { TestBed } from '@angular/core/testing';

import { MyProjectNameService } from './my-project-name.service';
import { MyProjectNameService } from './services/my-project-name.service';
import { RestService } from '@abp/ng.core';

describe('MyProjectNameService', () => {
let service: MyProjectNameService;

const mockRestService = jasmine.createSpyObj('RestService', ['request']);
beforeEach(() => {
TestBed.configureTestingModule({});
TestBed.configureTestingModule({
providers: [
{
provide: RestService,
useValue: mockRestService,
},
],
});
service = TestBed.inject(MyProjectNameService);
});

Expand Down