1
0

97732 Tests for ContextHelpService

This commit is contained in:
Koen Pauwels
2023-01-13 12:28:41 +01:00
parent cad4f74173
commit f7787d74bc
2 changed files with 63 additions and 3 deletions

View File

@@ -59,7 +59,6 @@ export class ContextHelpDirective implements OnChanges, OnDestroy {
= this.componentFactoryResolver.resolveComponentFactory(ContextHelpWrapperComponent); = this.componentFactoryResolver.resolveComponentFactory(ContextHelpWrapperComponent);
this.wrapper = this.viewContainerRef.createComponent(factory); this.wrapper = this.viewContainerRef.createComponent(factory);
} }
console.log(this.templateRef);
this.wrapper.instance.templateRef = this.templateRef; this.wrapper.instance.templateRef = this.templateRef;
this.wrapper.instance.content = this.dsContextHelp.content; this.wrapper.instance.content = this.dsContextHelp.content;
this.wrapper.instance.id = this.dsContextHelp.id; this.wrapper.instance.id = this.dsContextHelp.id;

View File

@@ -1,16 +1,77 @@
import { TestBed } from '@angular/core/testing'; import { TestBed } from '@angular/core/testing';
import { ContextHelpService } from './context-help.service'; import { ContextHelpService } from './context-help.service';
import { StoreModule, Store } from '@ngrx/store';
import { appReducers, storeModuleConfig } from '../app.reducer';
import { TestScheduler } from 'rxjs/testing';
describe('ContextHelpService', () => { describe('ContextHelpService', () => {
let service: ContextHelpService; let service: ContextHelpService;
let store;
let initialState;
let testScheduler;
const booleans = { f: false, t: true };
const mkContextHelp = (id: string) => ({ 0: {id, isTooltipVisible: false}, 1: {id, isTooltipVisible: true} })
beforeEach(async () => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot(appReducers, storeModuleConfig)
]
});
});
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({}); store = TestBed.inject(Store);
service = TestBed.inject(ContextHelpService); service = new ContextHelpService(store);
testScheduler = new TestScheduler((actual, expected) => expect(actual).toEqual(expected));
}); });
it('should be created', () => { it('should be created', () => {
expect(service).toBeTruthy(); expect(service).toBeTruthy();
}); });
it('toggleIcons calls should be observable in shouldShowIcons$', () => {
testScheduler.run(({cold, expectObservable}) => {
const toggles = cold('-xxxxx');
toggles.subscribe((_) => service.toggleIcons());
expectObservable(service.shouldShowIcons$()).toBe('ftftft', booleans);
});
});
it('add and remove calls should be observable in getContextHelp$', () => {
testScheduler.run(({cold, expectObservable}) => {
const modifications = cold('-abAcCB', {
a: () => service.add({id: 'a'}), b: () => service.add({id: 'b'}), c: () => service.add({id: 'c'}),
A: () => service.remove('a'), B: () => service.remove('b'), C: () => service.remove('c'),
})
modifications.subscribe(mod => mod());
const match = (id) => ({ 0: undefined, 1: {id} });
expectObservable(service.getContextHelp$('a')).toBe('01-0---', match('a'));
expectObservable(service.getContextHelp$('b')).toBe('0-1---0', match('b'));
expectObservable(service.getContextHelp$('c')).toBe('0---10-', match('c'));
});
});
it('toggleTooltip calls should be observable in getContextHelp$', () => {
service.add({id: 'a', isTooltipVisible: false});
service.add({id: 'b', isTooltipVisible: false});
testScheduler.run(({cold, expectObservable}) => {
const toggles = cold('-aaababbabba');
toggles.subscribe(id => service.toggleTooltip(id));
expectObservable(service.getContextHelp$('a')).toBe('0101-0--1--0', mkContextHelp('a'));
expectObservable(service.getContextHelp$('b')).toBe('0---1-01-01-', mkContextHelp('b'));
});
});
it('hideTooltip and showTooltip calls should be observable in getContextHelp$', () => {
service.add({id: 'a', isTooltipVisible: false});
testScheduler.run(({cold, expectObservable}) => {
const hideShowCalls = cold('-shssshhs', {
s: () => service.showTooltip('a'), h: () => service.hideTooltip('a')
});
hideShowCalls.subscribe(fn => fn());
expectObservable(service.getContextHelp$('a')).toBe('010111001', mkContextHelp('a'));
});
});
}); });