Browse Source

test: fix testing errors

pull/5333/head
mehmet-erim 6 years ago
parent
commit
89df9fdde0
  1. 18
      npm/ng-packs/packages/core/src/lib/tests/replaceable-route-container.component.spec.ts
  2. 22
      npm/ng-packs/packages/core/src/lib/tests/replaceable-template.directive.spec.ts

18
npm/ng-packs/packages/core/src/lib/tests/replaceable-route-container.component.spec.ts

@ -5,6 +5,7 @@ import { Store } from '@ngxs/store';
import { of, Subject, BehaviorSubject } from 'rxjs'; import { of, Subject, BehaviorSubject } from 'rxjs';
import { ReplaceableRouteContainerComponent } from '../components/replaceable-route-container.component'; import { ReplaceableRouteContainerComponent } from '../components/replaceable-route-container.component';
import { ReplaceableComponentsState } from '../states'; import { ReplaceableComponentsState } from '../states';
import { ReplaceableComponentsService } from '../services/replaceable-components.service';
@Component({ @Component({
selector: 'abp-external-component', selector: 'abp-external-component',
@ -30,16 +31,15 @@ const activatedRouteMock = {
}; };
describe('ReplaceableRouteContainerComponent', () => { describe('ReplaceableRouteContainerComponent', () => {
const selectResponse = new BehaviorSubject(undefined);
const mockSelect = jest.fn(() => selectResponse);
let spectator: SpectatorHost<ReplaceableRouteContainerComponent>; let spectator: SpectatorHost<ReplaceableRouteContainerComponent>;
const get$Res = new BehaviorSubject(undefined);
const replaceableComponents = spectator.inject(ReplaceableComponentsService);
const spy = jest.spyOn(replaceableComponents, 'get$');
spy.mockReturnValue(get$Res as any);
const createHost = createHostFactory({ const createHost = createHostFactory({
component: ReplaceableRouteContainerComponent, component: ReplaceableRouteContainerComponent,
providers: [ providers: [{ provide: ActivatedRoute, useValue: activatedRouteMock }],
{ provide: ActivatedRoute, useValue: activatedRouteMock },
{ provide: Store, useValue: { select: mockSelect } },
],
declarations: [ExternalComponent, DefaultComponent], declarations: [ExternalComponent, DefaultComponent],
entryComponents: [DefaultComponent, ExternalComponent], entryComponents: [DefaultComponent, ExternalComponent],
}); });
@ -55,11 +55,11 @@ describe('ReplaceableRouteContainerComponent', () => {
}); });
it("should display the external component if it's available in store.", () => { it("should display the external component if it's available in store.", () => {
selectResponse.next({ component: ExternalComponent }); get$Res.next({ component: ExternalComponent });
spectator.detectChanges(); spectator.detectChanges();
expect(spectator.query('p')).toHaveText('external'); expect(spectator.query('p')).toHaveText('external');
selectResponse.next({ component: null }); get$Res.next({ component: null });
spectator.detectChanges(); spectator.detectChanges();
expect(spectator.query('p')).toHaveText('default'); expect(spectator.query('p')).toHaveText('default');
}); });

22
npm/ng-packs/packages/core/src/lib/tests/replaceable-template.directive.spec.ts

@ -4,6 +4,8 @@ import { Store } from '@ngxs/store';
import { Subject } from 'rxjs'; import { Subject } from 'rxjs';
import { ReplaceableTemplateDirective } from '../directives'; import { ReplaceableTemplateDirective } from '../directives';
import { ReplaceableComponents } from '../models'; import { ReplaceableComponents } from '../models';
import { Router } from '@angular/router';
import { ReplaceableComponentsService } from '../services/replaceable-components.service';
@Component({ @Component({
selector: 'abp-default-component', selector: 'abp-default-component',
@ -48,15 +50,17 @@ class ExternalComponent {
} }
describe('ReplaceableTemplateDirective', () => { describe('ReplaceableTemplateDirective', () => {
const selectResponse = new Subject();
const mockSelect = jest.fn(() => selectResponse);
let spectator: SpectatorDirective<ReplaceableTemplateDirective>; let spectator: SpectatorDirective<ReplaceableTemplateDirective>;
const get$Res = new Subject();
const replaceableComponents = spectator.inject(ReplaceableComponentsService);
const spy = jest.spyOn(replaceableComponents, 'get$');
spy.mockReturnValue(get$Res as any);
const createDirective = createDirectiveFactory({ const createDirective = createDirectiveFactory({
directive: ReplaceableTemplateDirective, directive: ReplaceableTemplateDirective,
providers: [{ provide: Store, useValue: { select: mockSelect } }],
declarations: [DefaultComponent, ExternalComponent], declarations: [DefaultComponent, ExternalComponent],
entryComponents: [ExternalComponent], entryComponents: [ExternalComponent],
mocks: [Router],
}); });
describe('without external component', () => { describe('without external component', () => {
@ -72,7 +76,7 @@ describe('ReplaceableTemplateDirective', () => {
`, `,
{ hostProps: { oneWay: { label: 'Test' }, twoWay: false, twoWayChange, someOutput } }, { hostProps: { oneWay: { label: 'Test' }, twoWay: false, twoWayChange, someOutput } },
); );
selectResponse.next(undefined); get$Res.next(undefined);
const component = spectator.query(DefaultComponent); const component = spectator.query(DefaultComponent);
spectator.directive.context.initTemplate(component); spectator.directive.context.initTemplate(component);
spectator.detectChanges(); spectator.detectChanges();
@ -114,7 +118,7 @@ describe('ReplaceableTemplateDirective', () => {
`, `,
{ hostProps: { oneWay: { label: 'Test' }, twoWay: false, twoWayChange, someOutput } }, { hostProps: { oneWay: { label: 'Test' }, twoWay: false, twoWayChange, someOutput } },
); );
selectResponse.next({ component: ExternalComponent, key: 'TestModule.TestComponent' }); get$Res.next({ component: ExternalComponent, key: 'TestModule.TestComponent' });
}); });
afterEach(() => twoWayChange.mockClear()); afterEach(() => twoWayChange.mockClear());
@ -150,7 +154,7 @@ describe('ReplaceableTemplateDirective', () => {
const externalComponent = spectator.query(ExternalComponent); const externalComponent = spectator.query(ExternalComponent);
spectator.setHostInput({ oneWay: 'test' }); spectator.setHostInput({ oneWay: 'test' });
externalComponent.data.inputs.twoWay = true; externalComponent.data.inputs.twoWay = true;
selectResponse.next({ component: null, key: 'TestModule.TestComponent' }); get$Res.next({ component: null, key: 'TestModule.TestComponent' });
spectator.detectChanges(); spectator.detectChanges();
const component = spectator.query(DefaultComponent); const component = spectator.query(DefaultComponent);
spectator.directive.context.initTemplate(component); spectator.directive.context.initTemplate(component);
@ -161,14 +165,14 @@ describe('ReplaceableTemplateDirective', () => {
}); });
it('should reset default component subscriptions', () => { it('should reset default component subscriptions', () => {
selectResponse.next({ component: null, key: 'TestModule.TestComponent' }); get$Res.next({ component: null, key: 'TestModule.TestComponent' });
const component = spectator.query(DefaultComponent); const component = spectator.query(DefaultComponent);
spectator.directive.context.initTemplate(component); spectator.directive.context.initTemplate(component);
spectator.detectChanges(); spectator.detectChanges();
const unsubscribe = jest.fn(() => {}); const unsubscribe = jest.fn(() => {});
spectator.directive.defaultComponentSubscriptions.twoWayChange.unsubscribe = unsubscribe; spectator.directive.defaultComponentSubscriptions.twoWayChange.unsubscribe = unsubscribe;
selectResponse.next({ component: ExternalComponent, key: 'TestModule.TestComponent' }); get$Res.next({ component: ExternalComponent, key: 'TestModule.TestComponent' });
expect(unsubscribe).toHaveBeenCalled(); expect(unsubscribe).toHaveBeenCalled();
}); });
}); });

Loading…
Cancel
Save