Browse Source

fix theme-shared testing errors

pull/10108/head
Mehmet Erim 5 years ago
parent
commit
d673f1b396
  1. 7
      npm/ng-packs/packages/theme-shared/extensions/src/tests/enum.util.spec.ts
  2. 5
      npm/ng-packs/packages/theme-shared/extensions/src/tests/state.util.spec.ts
  3. 34
      npm/ng-packs/packages/theme-shared/src/lib/tests/modal-container.component.spec.ts
  4. 2
      npm/ng-packs/packages/theme-shared/src/lib/tests/modal.component.spec.ts
  5. 87
      npm/ng-packs/packages/theme-shared/src/lib/tests/modal.service.spec.ts
  6. 46
      npm/ng-packs/packages/theme-shared/src/lib/tests/sort-order-icon.component.spec.ts
  7. 36
      npm/ng-packs/packages/theme-shared/src/lib/tests/validation-utils.spec.ts

7
npm/ng-packs/packages/theme-shared/extensions/src/tests/enum.util.spec.ts

@ -1,5 +1,5 @@
import { ConfigStateService, LocalizationService } from '@abp/ng.core';
import { BehaviorSubject } from 'rxjs';
import { BehaviorSubject, of } from 'rxjs';
import { take } from 'rxjs/operators';
import { PropData } from '../lib/models/props';
import { createEnum, createEnumOptions, createEnumValueResolver } from '../lib/utils/enum.util';
@ -109,8 +109,9 @@ describe('Enum Utils', () => {
});
function createMockLocalizationService() {
const configState = new ConfigStateService(null);
configState.setState({ localization: mockL10n } as any);
const fakeAppConfigService = { get: () => of({ localization: mockL10n }) } as any;
const configState = new ConfigStateService(fakeAppConfigService);
configState.refreshAppState();
return new LocalizationService(mockSessionState, null, null, configState);
}

5
npm/ng-packs/packages/theme-shared/extensions/src/tests/state.util.spec.ts

@ -10,8 +10,9 @@ import {
mapEntitiesToContributors,
} from '../lib/utils/state.util';
const configState = new ConfigStateService(null);
configState.setState(createMockState() as any);
const fakeAppConfigService = { get: () => of(createMockState()) } as any;
const configState = new ConfigStateService(fakeAppConfigService);
configState.refreshAppState();
describe('State Utils', () => {
describe('#getObjectExtensionEntitiesFromStore', () => {

34
npm/ng-packs/packages/theme-shared/src/lib/tests/modal-container.component.spec.ts

@ -1,34 +0,0 @@
import { Component, ComponentFactoryResolver, ComponentRef } from '@angular/core';
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
import { ModalContainerComponent } from '../components/modal/modal-container.component';
describe('ModalContainerComponent', () => {
@Component({ template: '<div class="foo">bar</div>' })
class TestComponent {}
let componentRef: ComponentRef<TestComponent>;
let spectator: Spectator<ModalContainerComponent>;
const createComponent = createComponentFactory({
component: ModalContainerComponent,
entryComponents: [TestComponent],
});
beforeEach(() => (spectator = createComponent()));
afterEach(() => componentRef.destroy());
describe('#container', () => {
it('should be a ViewContainerRef', () => {
let foo = document.querySelector('div.foo');
expect(foo).toBeNull();
const cfResolver = spectator.inject(ComponentFactoryResolver);
const factory = cfResolver.resolveComponentFactory(TestComponent);
componentRef = spectator.component.container.createComponent(factory);
foo = document.querySelector('div.foo');
expect(foo.textContent).toBe('bar');
});
});
});

2
npm/ng-packs/packages/theme-shared/src/lib/tests/modal.component.spec.ts

@ -103,7 +103,7 @@ describe('ModalComponent', () => {
xit('should close with the abpClose', async () => {
await wait0ms();
spectator.dispatchMouseEvent(spectator.component.abpClose, 'click');
spectator.dispatchMouseEvent(spectator.query('[abpClose]'), 'click');
await wait0ms();

87
npm/ng-packs/packages/theme-shared/src/lib/tests/modal.service.spec.ts

@ -1,87 +0,0 @@
import { Component, TemplateRef, ViewChild } from '@angular/core';
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
import { ModalContainerComponent } from '../components/modal/modal-container.component';
import { ModalService } from '../services';
describe('ModalContainerComponent', () => {
@Component({
template: `
<ng-template #ref>
<div class="foo">bar</div>
</ng-template>
`,
})
class TestComponent {
@ViewChild('ref', { static: true })
template: TemplateRef<any>;
constructor(public modalService: ModalService) {}
}
let spectator: Spectator<TestComponent>;
let service: ModalService;
const createComponent = createComponentFactory({
component: TestComponent,
entryComponents: [ModalContainerComponent],
});
beforeEach(() => {
spectator = createComponent();
service = spectator.component.modalService;
});
afterEach(() => {
service.getContainer().clear();
service['containerComponentRef'].changeDetectorRef.detectChanges();
service['containerComponentRef'].destroy();
});
describe('#getContainer', () => {
it('should return the ViewContainerRef of ModalContainerComponent', () => {
let foo = document.querySelector('div.foo');
expect(foo).toBeNull();
const containerRef = service.getContainer();
const embeddedViewRef = containerRef.createEmbeddedView(spectator.component.template);
foo = document.querySelector('div.foo');
expect(foo).toBe(embeddedViewRef.rootNodes[0]);
expect(foo.textContent).toBe('bar');
});
});
describe('#renderTemplate', () => {
it('should render given template using the ViewContainerRef of ModalContainerComponent', () => {
let foo = document.querySelector('div.foo');
expect(foo).toBeNull();
service.renderTemplate(spectator.component.template);
foo = document.querySelector('div.foo');
expect(foo.textContent).toBe('bar');
});
});
describe('#detectChanges', () => {
it('should call detectChanges on the containerComponentRef', () => {
const spy = jest.spyOn(service['containerComponentRef'].changeDetectorRef, 'detectChanges');
service.detectChanges();
expect(spy).toHaveBeenCalledTimes(1);
});
});
describe('#clearModal', () => {
it('should call clear on the ViewContainerRef and detectChanges', () => {
const clear = jest.spyOn(service.getContainer(), 'clear');
const detectChanges = jest.spyOn(service, 'detectChanges');
service.clearModal();
expect(clear).toHaveBeenCalledTimes(1);
expect(detectChanges).toHaveBeenCalledTimes(1);
});
});
});

46
npm/ng-packs/packages/theme-shared/src/lib/tests/sort-order-icon.component.spec.ts

@ -1,46 +0,0 @@
import { createHostFactory, SpectatorHost } from '@ngneat/spectator/jest';
import { SortOrderIconComponent } from '../components/sort-order-icon/sort-order-icon.component';
describe('SortOrderIconComponent', () => {
let spectator: SpectatorHost<SortOrderIconComponent>;
let component: SortOrderIconComponent;
const createHost = createHostFactory(SortOrderIconComponent);
beforeEach(() => {
spectator = createHost(
'<abp-sort-order-icon sortKey="testKey" [(selectedSortKey)]="selectedSortKey" [(order)]="order"></abp-sort-order-icon>',
{
hostProps: {
selectedSortKey: '',
order: '',
},
},
);
component = spectator.component;
});
test('should have correct icon class when selectedSortKey and sortKey are the same', () => {
const newKey = 'testKey';
component.sort(newKey);
expect(component.selectedSortKey).toBe(newKey);
expect(component.order).toBe('asc');
expect(component.icon).toBe('sorting_asc');
});
test("shouldn't have any icon class when sortKey and selectedSortKey are different", () => {
const newKey = 'otherKey';
component.sort(newKey);
expect(component.selectedSortKey).toBe(newKey);
expect(component.order).toBe('asc');
expect(component.icon).toBe('sorting');
});
test('should change order correctly when sort function called', () => {
component.sort('testKey');
expect(component.order).toBe('asc');
component.sort('testKey');
expect(component.order).toBe('desc');
component.sort('testKey');
expect(component.order).toBe('');
});
});

36
npm/ng-packs/packages/theme-shared/src/lib/tests/validation-utils.spec.ts

@ -1,4 +1,4 @@
import { ConfigStateService } from '@abp/ng.core';
import { AbpApplicationConfigurationService, ConfigStateService } from '@abp/ng.core';
import { CoreTestingModule } from '@abp/ng.core/testing';
import { HttpClient } from '@angular/common/http';
import { Component, Injector } from '@angular/core';
@ -6,6 +6,7 @@ import { Validators } from '@angular/forms';
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
import { NgxValidateCoreModule, validatePassword } from '@ngx-validate/core';
import { OAuthService } from 'angular-oauth2-oidc';
import { of } from 'rxjs';
import { getPasswordValidators } from '../utils';
@Component({ template: '', selector: 'abp-dummy' })
class DummyComponent {}
@ -16,6 +17,26 @@ describe('ValidationUtils', () => {
component: DummyComponent,
imports: [CoreTestingModule.withConfig(), NgxValidateCoreModule.forRoot()],
mocks: [HttpClient, OAuthService],
providers: [
{
provide: AbpApplicationConfigurationService,
useValue: {
get: () =>
of({
setting: {
values: {
'Abp.Identity.Password.RequiredLength': '6',
'Abp.Identity.Password.RequiredUniqueChars': '1',
'Abp.Identity.Password.RequireNonAlphanumeric': 'True',
'Abp.Identity.Password.RequireLowercase': 'True',
'Abp.Identity.Password.RequireUppercase': 'True',
'Abp.Identity.Password.RequireDigit': 'True',
},
},
}),
},
},
],
});
beforeEach(() => (spectator = createComponent()));
@ -23,18 +44,7 @@ describe('ValidationUtils', () => {
describe('#getPasswordValidators', () => {
it('should return password valdiators', () => {
const configState = spectator.inject(ConfigStateService);
configState.setState({
setting: {
values: {
'Abp.Identity.Password.RequiredLength': '6',
'Abp.Identity.Password.RequiredUniqueChars': '1',
'Abp.Identity.Password.RequireNonAlphanumeric': 'True',
'Abp.Identity.Password.RequireLowercase': 'True',
'Abp.Identity.Password.RequireUppercase': 'True',
'Abp.Identity.Password.RequireDigit': 'True',
},
},
});
configState.refreshAppState();
const validators = getPasswordValidators(spectator.inject(Injector));
const expectedValidators = [

Loading…
Cancel
Save