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

add admin module tests for Angular #8048

Merged
merged 1 commit into from Jul 31, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions generators/client/files-angular.js
Expand Up @@ -460,7 +460,14 @@ const files = {
'jest.ts',
'jest-global-mocks.ts',
'spec/test.module.ts',
'spec/app/admin/configuration/configuration.component.spec.ts',
'spec/app/admin/configuration/configuration.service.spec.ts',
'spec/app/admin/health/health.component.spec.ts',
'spec/app/admin/logs/logs.component.spec.ts',
'spec/app/admin/logs/logs.service.spec.ts',
'spec/app/admin/metrics/metrics.component.spec.ts',
'spec/app/admin/metrics/metrics.service.spec.ts',
'spec/app/admin/metrics/metrics-modal.component.spec.ts',
'spec/helpers/spyobject.ts',
'spec/helpers/mock-account.service.ts',
'spec/helpers/mock-principal.service.ts',
Expand Down
@@ -0,0 +1,89 @@
<%#
Copyright 2013-2018 the original author or authors from the JHipster project.

This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { of } from 'rxjs';
import { HttpHeaders, HttpResponse } from '@angular/common/http';

import { <%=angularXAppName%>TestModule } from '../../../test.module';
import { <%=jhiPrefixCapitalized%>ConfigurationComponent } from 'app/admin/configuration/configuration.component';
import { <%=jhiPrefixCapitalized%>ConfigurationService } from 'app/admin/configuration/configuration.service';
import { ITEMS_PER_PAGE } from 'app/shared';
import { Log } from 'app/admin';

describe('Component Tests', () => {
describe('<%=jhiPrefixCapitalized%>ConfigurationComponent', () => {
let comp: <%=jhiPrefixCapitalized%>ConfigurationComponent;
let fixture: ComponentFixture<<%=jhiPrefixCapitalized%>ConfigurationComponent>;
let service: <%=jhiPrefixCapitalized%>ConfigurationService;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [<%=angularXAppName%>TestModule],
declarations: [<%=jhiPrefixCapitalized%>ConfigurationComponent],
providers: [<%=jhiPrefixCapitalized%>ConfigurationService]
})
.overrideTemplate(<%=jhiPrefixCapitalized%>ConfigurationComponent, '')
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(<%=jhiPrefixCapitalized%>ConfigurationComponent);
comp = fixture.componentInstance;
service = fixture.debugElement.injector.get(<%=jhiPrefixCapitalized%>ConfigurationService);
});

describe('OnInit', () => {
it('should set all default values correctly', () => {
expect(comp.configKeys).toEqual([]);
expect(comp.filter).toBe('');
expect(comp.orderProp).toBe('prefix');
expect(comp.reverse).toBe(false);
});
it('Should call load all on init', () => {
// GIVEN
const body = [{config: 'test', properties: 'test'}, {config: 'test2'}]
const envConfig = {envConfig: 'test'}
spyOn(service, 'get').and.returnValue(of(body));
spyOn(service, 'getEnv').and.returnValue(of(envConfig));

// WHEN
comp.ngOnInit();

// THEN
expect(service.get).toHaveBeenCalled();
expect(service.getEnv).toHaveBeenCalled();
expect(comp.configKeys).toEqual([["0", "1", "2", "3"]]);
expect(comp.allConfiguration).toEqual(envConfig);
});
});
describe('keys method', () => {
it('should return the keys of an Object', () => {
// GIVEN
const data = {
key1: 'test',
key2: 'test2'
}

// THEN
expect(comp.keys(data)).toEqual(['key1', 'key2']);
expect(comp.keys(undefined)).toEqual([]);
});
});
});
});
@@ -0,0 +1,85 @@
<%#
Copyright 2013-2018 the original author or authors from the JHipster project.

This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
import { TestBed } from '@angular/core/testing';

import { <%=jhiPrefixCapitalized%>ConfigurationService } from 'app/admin/configuration/configuration.service';
import { SERVER_API_URL } from 'app/app.constants';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { HttpResponse } from '@angular/common/http';

describe('Service Tests', () => {
describe('Logs Service', () => {
let service: <%=jhiPrefixCapitalized%>ConfigurationService;
let httpMock;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule]
});

service = TestBed.get(<%=jhiPrefixCapitalized%>ConfigurationService);
httpMock = TestBed.get(HttpTestingController);
});

afterEach(() => {
httpMock.verify();
});

describe('Service methods', () => {
it('should call correct URL', () => {
service.get().subscribe(() => {});

const req = httpMock.expectOne({ method: 'GET' });
const resourceUrl = SERVER_API_URL + 'management/configprops';
expect(req.request.url).toEqual(resourceUrl);
});

it('should get the config', () => {
const angularConfig = {
contexts: {
angular: {
beans: [
'test2'
]
}
}
}
service.get().subscribe(received => {
expect(received.body[0]).toEqual(angularConfig);
});

const req = httpMock.expectOne({ method: 'GET' });
req.flush(angularConfig);
});

it('should get the env', () => {
const propertySources = new HttpResponse({body: [
{name: 'test1', properties: 'test1'},
{name: 'test2', properties: 'test2'},
]})
service.get().subscribe(received => {
expect(received.body[0]).toEqual(propertySources);
});

const req = httpMock.expectOne({ method: 'GET' });
req.flush(propertySources);
});
});
});
});