Skip to content

Commit

Permalink
test: fix all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bartholomej committed Jun 13, 2024
1 parent f20f0f2 commit 47de022
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
17 changes: 9 additions & 8 deletions projects/ngx-scrolltop/src/lib/ngx-scrolltop.component.spec.ts
Original file line number Diff line number Diff line change
@@ -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<NgxScrollTopComponent>;
let fixture: ComponentFixture<NgxScrollTopComponent>;
let element: HTMLButtonElement;
let cdRef: ChangeDetectorRef;
Expand All @@ -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');
Expand All @@ -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(
Expand All @@ -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');
Expand All @@ -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);
Expand Down
20 changes: 8 additions & 12 deletions projects/ngx-scrolltop/src/lib/ngx-scrolltop.core.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
});
Expand All @@ -45,4 +41,4 @@ describe('NgxScrollTopService', () => {
srvc.scrollToTop();
expect(document.documentElement.scrollTop).toBe(0);
});
});
});

0 comments on commit 47de022

Please sign in to comment.