From 262dd0f198501b3218a9cae3fae7c8b126058d02 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 15 Feb 2021 12:44:29 -0500 Subject: [PATCH] Trying to get the tests to pass, but still more work to do. --- src/app/app.module.ts | 4 +-- src/app/custom-date-adapter.ts | 6 ++-- src/app/graphs/graphs.component.spec.ts | 38 ++++++++++++++++++++----- src/app/services/graph.service.spec.ts | 32 ++++++++++++++++++++- 4 files changed, 68 insertions(+), 12 deletions(-) diff --git a/src/app/app.module.ts b/src/app/app.module.ts index b1df4f4..1c99e05 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -49,7 +49,7 @@ import { MatDatepickerModule } from '@angular/material/datepicker'; import { MatNativeDateModule, DateAdapter } from '@angular/material/core'; import {MatPaginatorModule} from '@angular/material/paginator'; import { CustomDateAdapter } from './custom-date-adapter'; -import { MatTableModule } from '@angular/material/table' +import { MatTableModule } from '@angular/material/table'; import {MatGridListModule} from '@angular/material/grid-list'; import {MatDividerModule} from '@angular/material/divider'; import { SidebarComponent } from './sidebar/sidebar.component'; @@ -130,7 +130,7 @@ export function getBaseHref(platformLocation: PlatformLocation): string { ApiService, GraphService, CacheService, - SettingsService, + SettingsService, MatDatepickerModule, {provide: DateAdapter, useClass: CustomDateAdapter }, {provide: 'APP_ENVIRONMENT', useClass: ThisEnvironment}, diff --git a/src/app/custom-date-adapter.ts b/src/app/custom-date-adapter.ts index b0a59f6..b60914b 100644 --- a/src/app/custom-date-adapter.ts +++ b/src/app/custom-date-adapter.ts @@ -1,11 +1,13 @@ import { NativeDateAdapter } from '@angular/material/core'; +import {Injectable} from '@angular/core'; /** Adapts the native JS Date for use with cdk-based components that work with dates. */ +@Injectable() export class CustomDateAdapter extends NativeDateAdapter { - + getFirstDayOfWeek(): number { return 1; } -} \ No newline at end of file +} diff --git a/src/app/graphs/graphs.component.spec.ts b/src/app/graphs/graphs.component.spec.ts index 18ddbaa..82663fb 100644 --- a/src/app/graphs/graphs.component.spec.ts +++ b/src/app/graphs/graphs.component.spec.ts @@ -1,19 +1,43 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { GraphsComponent } from './graphs.component'; +import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; +import {MatBottomSheetModule} from '@angular/material/bottom-sheet'; +import {RouterTestingModule} from '@angular/router/testing'; +import {ApiService} from '../services/api.service'; +import {APP_BASE_HREF} from '@angular/common'; +import {Router} from '@angular/router'; +import {MockEnvironment} from '../testing/environment.mock'; +import {DateAdapter} from '@angular/material/core'; +import {CustomDateAdapter} from '../custom-date-adapter'; describe('GraphsComponent', () => { let component: GraphsComponent; let fixture: ComponentFixture; - - beforeEach(async () => { - await TestBed.configureTestingModule({ - declarations: [ GraphsComponent ] - }) - .compileComponents(); - }); + let httpMock: HttpTestingController; + const mockEnvironment = new MockEnvironment(); + const mockRouter = { + createUrlTree: jasmine.createSpy('createUrlTree'), + navigate: jasmine.createSpy('navigate') + }; beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + HttpClientTestingModule, + MatBottomSheetModule, + RouterTestingModule.withRoutes([]), + ], + providers: [ + ApiService, + {provide: 'APP_ENVIRONMENT', useValue: mockEnvironment}, + {provide: APP_BASE_HREF, useValue: '/'}, + {provide: Router, useValue: mockRouter}, + {provide: Location, useValue: location}, + {provide: DateAdapter, useClass: CustomDateAdapter } + ] + }); + httpMock = TestBed.inject(HttpTestingController); fixture = TestBed.createComponent(GraphsComponent); component = fixture.componentInstance; fixture.detectChanges(); diff --git a/src/app/services/graph.service.spec.ts b/src/app/services/graph.service.spec.ts index 3aebd00..14c3422 100644 --- a/src/app/services/graph.service.spec.ts +++ b/src/app/services/graph.service.spec.ts @@ -1,13 +1,43 @@ import { TestBed } from '@angular/core/testing'; import { GraphService } from './graph.service'; +import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; +import {MatBottomSheetModule} from '@angular/material/bottom-sheet'; +import {RouterTestingModule} from '@angular/router/testing'; +import {ApiService} from './api.service'; +import {APP_BASE_HREF} from '@angular/common'; +import {Router} from '@angular/router'; +import {MockEnvironment} from '../testing/environment.mock'; describe('GraphService', () => { + let httpMock: HttpTestingController; + let location: Location; let service: GraphService; + const mockEnvironment = new MockEnvironment(); + const mockRouter = { + createUrlTree: jasmine.createSpy('createUrlTree'), + navigate: jasmine.createSpy('navigate') + }; beforeEach(() => { - TestBed.configureTestingModule({}); + TestBed.configureTestingModule({ + imports: [ + HttpClientTestingModule, + MatBottomSheetModule, + RouterTestingModule.withRoutes([]), + ], + providers: [ + ApiService, + {provide: 'APP_ENVIRONMENT', useValue: mockEnvironment}, + {provide: APP_BASE_HREF, useValue: '/'}, + {provide: Router, useValue: mockRouter}, + {provide: Location, useValue: location}, + ] + }); + + httpMock = TestBed.inject(HttpTestingController); service = TestBed.inject(GraphService); + location = TestBed.inject(Location); }); it('should be created', () => {