import 'reflect-metadata'; import { ChangeDetectorRef, ElementRef, } from '@angular/core'; import { BackgroundImageCyclerComponent, } from './background-image-cycler.component'; const initBackgroundImageCyclerDirective = ( elementRef?: ElementRef, changeDetectorRef?: ChangeDetectorRef, ) => { return new BackgroundImageCyclerComponent( elementRef, changeDetectorRef, ); }; describe('ngOnInit', () => { test('Calls nextImage', () => { const backgroundImageCyclerDirective = initBackgroundImageCyclerDirective(); backgroundImageCyclerDirective.nextImage = jest.fn(); backgroundImageCyclerDirective.ngOnInit(); expect(backgroundImageCyclerDirective.nextImage).toHaveBeenCalled(); }); }); describe('nextImage', () => { // tslint:disable-next-line test('Sets the currentImageUrl to the first image in the array if currentImageUrl is undefined', () => { const changeDetectorRef = {} as ChangeDetectorRef; changeDetectorRef.markForCheck = jest.fn(); const backgroundImageCyclerDirective = initBackgroundImageCyclerDirective( undefined, changeDetectorRef, ); backgroundImageCyclerDirective.imageUrls = ['1', '2', '3']; backgroundImageCyclerDirective.currentImageUrl = undefined; backgroundImageCyclerDirective.nextImage(); expect( backgroundImageCyclerDirective.currentImageUrl, ).toBe('1'); }); // tslint:disable-next-line test('Sets the currentImageUrl to the next image in the array', () => { const changeDetectorRef = {} as ChangeDetectorRef; changeDetectorRef.markForCheck = jest.fn(); const backgroundImageCyclerDirective = initBackgroundImageCyclerDirective( undefined, changeDetectorRef, ); backgroundImageCyclerDirective.imageUrls = ['1', '2', '3']; backgroundImageCyclerDirective.currentImageUrl = '2'; backgroundImageCyclerDirective.nextImage(); expect( backgroundImageCyclerDirective.currentImageUrl, ).toBe('3'); }); // tslint:disable-next-line test('Sets the currentImageUrl to the first image in the array if currentImageUrl is the last image url', () => { const changeDetectorRef = {} as ChangeDetectorRef; changeDetectorRef.markForCheck = jest.fn(); const backgroundImageCyclerDirective = initBackgroundImageCyclerDirective( undefined, changeDetectorRef, ); backgroundImageCyclerDirective.imageUrls = ['1', '2', '3']; backgroundImageCyclerDirective.currentImageUrl = '3'; backgroundImageCyclerDirective.nextImage(); expect( backgroundImageCyclerDirective.currentImageUrl, ).toBe('1'); }); // tslint:disable-next-line test('Calls nextImage again after the timeout has completed', () => { jest.useFakeTimers(); const changeDetectorRef = {} as ChangeDetectorRef; changeDetectorRef.markForCheck = jest.fn(); const backgroundImageCyclerDirective = initBackgroundImageCyclerDirective( undefined, changeDetectorRef, ); backgroundImageCyclerDirective.imageUrls = ['1', '2', '3']; backgroundImageCyclerDirective.currentImageUrl = '3'; backgroundImageCyclerDirective.nextImage(); backgroundImageCyclerDirective.nextImage = jest.fn(); jest.runAllTimers(); expect( backgroundImageCyclerDirective.nextImage, ).toHaveBeenCalledTimes(1); }); test('Marks the compoent for change detection', () => { const changeDetectorRef = {} as ChangeDetectorRef; changeDetectorRef.markForCheck = jest.fn(); const backgroundImageCyclerDirective = initBackgroundImageCyclerDirective( undefined, changeDetectorRef, ); backgroundImageCyclerDirective.imageUrls = ['1', '2', '3']; backgroundImageCyclerDirective.currentImageUrl = '3'; backgroundImageCyclerDirective.nextImage(); expect( changeDetectorRef.markForCheck, ).toHaveBeenCalled(); }); }); describe('isSelected', () => { test('True if argument is current image', () => { const backgroundImageCyclerDirective = initBackgroundImageCyclerDirective(); backgroundImageCyclerDirective.currentImageUrl = 'test'; const result = backgroundImageCyclerDirective.isSelected('test'); expect(result).toBe(true); }); test('False if argument is not current image', () => { const backgroundImageCyclerDirective = initBackgroundImageCyclerDirective(); backgroundImageCyclerDirective.currentImageUrl = 'test'; const result = backgroundImageCyclerDirective.isSelected('otherImage'); expect(result).toBe(false); }); });