From 47de0222fcee84dda6f6b425a8a9ecd060a49894 Mon Sep 17 00:00:00 2001 From: BART! Date: Thu, 13 Jun 2024 15:55:33 +0200 Subject: [PATCH] test: fix all tests --- .../src/lib/ngx-scrolltop.component.spec.ts | 17 ++++++++-------- .../lib/ngx-scrolltop.core.service.spec.ts | 20 ++++++++----------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/projects/ngx-scrolltop/src/lib/ngx-scrolltop.component.spec.ts b/projects/ngx-scrolltop/src/lib/ngx-scrolltop.component.spec.ts index 6deb0f0..e39284e 100644 --- a/projects/ngx-scrolltop/src/lib/ngx-scrolltop.component.spec.ts +++ b/projects/ngx-scrolltop/src/lib/ngx-scrolltop.component.spec.ts @@ -1,10 +1,11 @@ -import { ChangeDetectorRef } from '@angular/core'; +import { ChangeDetectorRef, ComponentRef } from '@angular/core'; import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { NgxScrollTopComponent } from './ngx-scrolltop.component'; import { NgxScrollTopCoreService } from './ngx-scrolltop.core.service'; describe('NgxScrollTopComponent', () => { let component: NgxScrollTopComponent; + let componentRef: ComponentRef; let fixture: ComponentFixture; let element: HTMLButtonElement; let cdRef: ChangeDetectorRef; @@ -19,6 +20,7 @@ describe('NgxScrollTopComponent', () => { beforeEach(() => { fixture = TestBed.createComponent(NgxScrollTopComponent); component = fixture.componentInstance; + componentRef = fixture.componentRef; component.show.set(true); fixture.detectChanges(); element = fixture.nativeElement.querySelector('.scrolltop-button'); @@ -29,23 +31,22 @@ describe('NgxScrollTopComponent', () => { expect(component).toBeTruthy(); }); - it('should set backgroundColor', () => { - component.backgroundColor = '#1b5e20'; + componentRef.setInput('backgroundColor', '#1b5e20'); cdRef.detectChanges(); expect(element.style.backgroundColor).toEqual('rgb(27, 94, 32)'); }); it('should set backgroundColor with alpha', () => { - component.backgroundColor = '#212121cc'; + componentRef.setInput('backgroundColor', '#212121cc'); cdRef.detectChanges(); expect(element.style.backgroundColor).toEqual('rgba(33, 33, 33, 0.8)'); }); it('should set symbolColor', () => { - component.symbolColor = '#FF6F00'; + componentRef.setInput('symbolColor', '#FF6F00'); cdRef.detectChanges(); expect( @@ -54,7 +55,7 @@ describe('NgxScrollTopComponent', () => { }); it('should set size', () => { - component.size = 55; + componentRef.setInput('size', 55); cdRef.detectChanges(); expect(element.style.width).toEqual('55px'); @@ -63,14 +64,14 @@ describe('NgxScrollTopComponent', () => { }); it('should set position', () => { - component.position = 'left'; + componentRef.setInput('position', 'left'); cdRef.detectChanges(); expect(element.style.left).toEqual('20px'); }); it('should set theme', () => { - component.theme = 'deeppurple'; + componentRef.setInput('theme', 'deeppurple'); cdRef.detectChanges(); const computedStyle = window.getComputedStyle(element); diff --git a/projects/ngx-scrolltop/src/lib/ngx-scrolltop.core.service.spec.ts b/projects/ngx-scrolltop/src/lib/ngx-scrolltop.core.service.spec.ts index 5afbd88..8ec39da 100644 --- a/projects/ngx-scrolltop/src/lib/ngx-scrolltop.core.service.spec.ts +++ b/projects/ngx-scrolltop/src/lib/ngx-scrolltop.core.service.spec.ts @@ -1,29 +1,23 @@ import { DOCUMENT } from '@angular/common'; -import { getTestBed, TestBed } from '@angular/core/testing'; +import { TestBed } from '@angular/core/testing'; import { NgxScrollTopCoreService } from './ngx-scrolltop.core.service'; describe('NgxScrollTopService', () => { - let injector: TestBed; let srvc: NgxScrollTopCoreService; const mockDocument = { documentElement: { scrollTop: window.innerHeight * 2 + 1 }, }; beforeEach(() => { - srvc = new NgxScrollTopCoreService('classic'); TestBed.configureTestingModule({ providers: [{ provide: DOCUMENT, useValue: mockDocument }, NgxScrollTopCoreService], }); - injector = getTestBed(); - srvc = injector.get(NgxScrollTopCoreService); + srvc = TestBed.inject(NgxScrollTopCoreService); }); it('should be created', () => { - const service: NgxScrollTopCoreService = TestBed.get( - NgxScrollTopCoreService - ); - expect(service).toBeTruthy(); + expect(srvc).toBeTruthy(); }); it('Classic mode: Show button on scroll', () => { @@ -35,8 +29,10 @@ describe('NgxScrollTopService', () => { it('Smart mode: Show button on scroll', () => { const mode = 'smart'; // Set private properties as any - (srvc as any).scrolledFromTop = true; - (srvc as any).scrollOffset = window.innerHeight * 2 + 2; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (srvc as any).scrolledFromTop.set(true); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (srvc as any).scrollOffset.set(window.innerHeight * 2 + 2); expect(srvc.onWindowScroll(mode)).toBe(true); }); @@ -45,4 +41,4 @@ describe('NgxScrollTopService', () => { srvc.scrollToTop(); expect(document.documentElement.scrollTop).toBe(0); }); -}); +}); \ No newline at end of file