From be4534b677f1d7ac2f4abd46d43b3214ff1411a8 Mon Sep 17 00:00:00 2001 From: Mehmet Erim Date: Mon, 20 Sep 2021 17:13:04 +0300 Subject: [PATCH] fix testing errors --- .../chart.js/src}/chart.component.spec.ts | 56 +++++-------------- .../packages/components/src/test-setup.ts | 2 + .../lib/tests/append-content.token.spec.ts | 14 +---- .../packages/theme-shared/src/test-setup.ts | 2 +- 4 files changed, 18 insertions(+), 56 deletions(-) rename npm/ng-packs/packages/{theme-shared/src/lib/tests => components/chart.js/src}/chart.component.spec.ts (60%) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/tests/chart.component.spec.ts b/npm/ng-packs/packages/components/chart.js/src/chart.component.spec.ts similarity index 60% rename from npm/ng-packs/packages/theme-shared/src/lib/tests/chart.component.spec.ts rename to npm/ng-packs/packages/components/chart.js/src/chart.component.spec.ts index 083d810043..d7d4a1b42e 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/tests/chart.component.spec.ts +++ b/npm/ng-packs/packages/components/chart.js/src/chart.component.spec.ts @@ -1,10 +1,10 @@ import { createHostFactory, SpectatorHost } from '@ngneat/spectator/jest'; +import Chart from 'chart.js/auto'; import { ReplaySubject } from 'rxjs'; -import { ChartComponent } from '../components'; -import * as widgetUtils from '../utils/widget-utils'; -import { chartJsLoaded$ } from '../utils/widget-utils'; -// import 'chart.js'; -declare const Chart; +import { ChartComponent } from './chart.component'; +import * as widgetUtils from './widget-utils'; + +Chart Object.defineProperty(window, 'getComputedStyle', { value: () => ({ @@ -34,19 +34,18 @@ describe('ChartComponent', () => { }, }, }); - }); - test('should throw error when chart.js is not loaded', () => { - try { - spectator.component.testChartJs(); - } catch (error) { - expect(error.message).toContain('Chart is not found'); - } + window.ResizeObserver = + window.ResizeObserver || + jest.fn().mockImplementation(() => ({ + disconnect: jest.fn(), + observe: jest.fn(), + unobserve: jest.fn(), + })); }); test('should have a success class by default', done => { import('chart.js').then(() => { - chartJsLoaded$.next(); setTimeout(() => { expect(spectator.component.chart).toBeTruthy(); done(); @@ -56,7 +55,6 @@ describe('ChartComponent', () => { describe('#reinit', () => { it('should call the destroy method', done => { - chartJsLoaded$.next(); const spy = jest.spyOn(spectator.component.chart, 'destroy'); spectator.setHostInput({ data: { @@ -79,7 +77,6 @@ describe('ChartComponent', () => { describe('#refresh', () => { it('should call the update method', done => { - chartJsLoaded$.next(); const spy = jest.spyOn(spectator.component.chart, 'update'); spectator.component.refresh(); setTimeout(() => { @@ -89,38 +86,11 @@ describe('ChartComponent', () => { }); }); - describe('#generateLegend', () => { - it('should call the generateLegend method', done => { - chartJsLoaded$.next(); - const spy = jest.spyOn(spectator.component.chart, 'generateLegend'); - spectator.component.generateLegend(); - setTimeout(() => { - expect(spy).toHaveBeenCalled(); - done(); - }, 0); - }); - }); - - describe('#onCanvasClick', () => { - it('should emit the onDataSelect', done => { - spectator.component.onDataSelect.subscribe(() => { - done(); - }); - - chartJsLoaded$.next(); - jest - .spyOn(spectator.component.chart, 'getElementAtEvent') - .mockReturnValue([document.createElement('div')]); - spectator.click('canvas'); - }); - }); - describe('#base64Image', () => { it('should return the base64 image', done => { - chartJsLoaded$.next(); setTimeout(() => { - expect(spectator.component.base64Image).toContain('base64'); + expect(spectator.component.getBase64Image()).toContain('base64'); done(); }, 0); }); diff --git a/npm/ng-packs/packages/components/src/test-setup.ts b/npm/ng-packs/packages/components/src/test-setup.ts index 1100b3e8a6..b2f7487191 100644 --- a/npm/ng-packs/packages/components/src/test-setup.ts +++ b/npm/ng-packs/packages/components/src/test-setup.ts @@ -1 +1,3 @@ +import 'jest-canvas-mock'; import 'jest-preset-angular/setup-jest'; + diff --git a/npm/ng-packs/packages/theme-shared/src/lib/tests/append-content.token.spec.ts b/npm/ng-packs/packages/theme-shared/src/lib/tests/append-content.token.spec.ts index fe3b0ac37c..286218efa0 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/tests/append-content.token.spec.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/tests/append-content.token.spec.ts @@ -1,9 +1,8 @@ +import { DomInsertionService } from '@abp/ng.core'; import { Component } from '@angular/core'; import { createComponentFactory, Spectator } from '@ngneat/spectator'; -import { THEME_SHARED_APPEND_CONTENT } from '../tokens/append-content.token'; -import { DomInsertionService } from '@abp/ng.core'; -import { chartJsLoaded$ } from '../utils'; import styles from '../constants/styles'; +import { THEME_SHARED_APPEND_CONTENT } from '../tokens/append-content.token'; @Component({ selector: 'abp-dummy', template: '' }) class DummyComponent {} @@ -18,13 +17,4 @@ describe('AppendContentToken', () => { spectator.inject(THEME_SHARED_APPEND_CONTENT); expect(spectator.inject(DomInsertionService).has(styles)).toBe(true); }); - - it('should be loaded the chart.js', done => { - chartJsLoaded$.subscribe(loaded => { - expect(loaded).toBe(true); - done(); - }); - - spectator.inject(THEME_SHARED_APPEND_CONTENT); - }); }); diff --git a/npm/ng-packs/packages/theme-shared/src/test-setup.ts b/npm/ng-packs/packages/theme-shared/src/test-setup.ts index 713f967b86..ab68e1eb87 100644 --- a/npm/ng-packs/packages/theme-shared/src/test-setup.ts +++ b/npm/ng-packs/packages/theme-shared/src/test-setup.ts @@ -1,2 +1,2 @@ -import 'jest-canvas-mock'; import 'jest-preset-angular/setup-jest'; +