Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NRPT-262: Associating mine records. Fixing unit tests, code cleanup. #544

Merged
merged 4 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions angular/projects/admin-nrpti/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TestBed, async } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { AppComponent } from './app.component';
Expand All @@ -11,23 +11,23 @@ import { HttpClientTestingModule } from '@angular/common/http/testing';
import { SidebarComponent } from './sidebar/sidebar.component';
import { ToggleButtonComponent } from './toggle-button/toggle-button.component';
import {
BreadcrumbComponent,
StoreService,
GlobalModule,
LoadingScreenService
} from 'nrpti-angular-components';
import { of } from 'rxjs';
import { EventEmitter } from 'events';

describe('AppComponent', () => {
beforeEach(async(() => {
beforeEach((() => {
const mockKeycloakService = {
isValidForSite: () => {
return true;
}
};

const mockStoreService = {
stateChange: of(),
toggleSideNave: () => { }
getItem: () => { },
stateChange: new EventEmitter()
};

const mockLoadingScreenService = {
Expand All @@ -40,10 +40,9 @@ describe('AppComponent', () => {
HeaderComponent,
FooterComponent,
SidebarComponent,
ToggleButtonComponent,
BreadcrumbComponent
ToggleButtonComponent
],
imports: [RouterTestingModule, HttpClientTestingModule, BrowserAnimationsModule],
imports: [GlobalModule, RouterTestingModule, HttpClientTestingModule, BrowserAnimationsModule],
providers: [
ApiService,
{ provide: LoadingScreenService, useValue: mockLoadingScreenService },
Expand All @@ -53,7 +52,7 @@ describe('AppComponent', () => {
}).compileComponents();
}));

it('should create the app', async(() => {
it('should create the app', (() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
Expand Down
107 changes: 105 additions & 2 deletions angular/projects/admin-nrpti/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { Component, OnInit, HostListener, ChangeDetectorRef } from '@angular/core';
import { Component, OnInit, HostListener, ChangeDetectorRef, OnDestroy } from '@angular/core';
import { Router } from '@angular/router';
import { IBreadcrumb, StoreService, LoadingScreenService } from 'nrpti-angular-components';
import { FactoryService } from './services/factory.service';
import { Subscription } from 'rxjs/internal/Subscription';
import { interval } from 'rxjs/internal/observable/interval';
import { takeWhile, switchMap } from 'rxjs/operators';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
export class AppComponent implements OnInit, OnDestroy {
private alive = true;
public currentWindowWidth = window.innerWidth;
public showSideContent = this.currentWindowWidth > 768 ? true : false;
public userClosedSideContent = false;

mineSubscription: Subscription;
epicProjectSubscription: Subscription;

public breadcrumbs: IBreadcrumb[];
public activeBreadcrumb: IBreadcrumb;

Expand All @@ -21,13 +29,22 @@ export class AppComponent implements OnInit {
constructor(
private router: Router,
private loadingScreenService: LoadingScreenService,
private factoryService: FactoryService,
private storeService: StoreService,
private _changeDetectionRef: ChangeDetectorRef
) {
this.breadcrumbs = [];
}

ngOnInit() {
if (this.mineSubscription) {
this.mineSubscription.unsubscribe();
}

if (this.epicProjectSubscription) {
this.epicProjectSubscription.unsubscribe();
}

this.storeService.stateChange.subscribe((state: object) => {
if (state && state.hasOwnProperty('userClosedSideContent')) {
this.userClosedSideContent = state['userClosedSideContent'];
Expand All @@ -52,6 +69,88 @@ export class AppComponent implements OnInit {
}
this._changeDetectionRef.detectChanges();
});

// Subscribe to updates on specific models
this.updateMines();
this.updateEpicProjects();
}

private updateMines() {
// Fetch initially
const minesSub = this.factoryService.searchService.getSearchResults(
this.factoryService.apiService.pathAPI,
'',
['MineBCMI'],
[],
1,
100000,
'+name'
).subscribe((mineResults: any[]) => {
this.setStoreServiceItem('mines', mineResults[0].data.searchResults);
minesSub.unsubscribe();
});

// Update every 4 hours
this.mineSubscription = interval(1000 * 60 * 60 * 4)
.pipe(
takeWhile(() => this.alive),
switchMap(() => this.factoryService.searchService.getSearchResults(
this.factoryService.apiService.pathAPI,
'',
['MineBCMI'],
[],
1,
100000,
'+name'
)),
)
.subscribe((mineResults: any[]) => {
this.setStoreServiceItem('mines', mineResults[0].data.searchResults);
});
}

private updateEpicProjects() {
// Fetch initially
const epicSub = this.factoryService.searchService.getSearchResults(
this.factoryService.apiService.pathAPI,
'',
['EPICProject'],
[],
1,
100000,
'+name'
).subscribe((epicProjectResults: any[]) => {
this.setStoreServiceItem('epicProjects', epicProjectResults[0].data.searchResults);
epicSub.unsubscribe();
});

// Update every 4 hours
this.epicProjectSubscription = interval(1000 * 60 * 60 * 4)
.pipe(
takeWhile(() => this.alive),
switchMap(() => this.factoryService.searchService.getSearchResults(
this.factoryService.apiService.pathAPI,
'',
['EPICProject'],
[],
1,
100000,
'+name'
)),
)
.subscribe((epicProjectResults: any[]) => {
this.setStoreServiceItem('epicProjects', epicProjectResults[0].data.searchResults);
});
}

// Sets a store service list item, but unshifts a null-based element first.
private setStoreServiceItem(key, list) {
// First unshift an item into the list.
list.unshift({_id: null, name: 'None'});

// Set the object in the store service
const newObject = { [key]: list };
this.storeService.setItem(newObject);
}

public navigateBreadcrumb(breadcrumbData) {
Expand Down Expand Up @@ -82,4 +181,8 @@ export class AppComponent implements OnInit {

this.currentWindowWidth = window.innerWidth;
}

ngOnDestroy() {
this.alive = false;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { ConfirmComponent } from './confirm.component';
import { DialogService } from 'ng2-bootstrap-modal';
Expand All @@ -7,7 +7,7 @@ describe('ConfirmComponent', () => {
let component: ConfirmComponent;
let fixture: ComponentFixture<ConfirmComponent>;

beforeEach(async(() => {
beforeEach((() => {
TestBed.configureTestingModule({
declarations: [ConfirmComponent],
providers: [DialogService]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { FooterComponent } from './footer.component';

describe('FooterComponent', () => {
let component: FooterComponent;
let fixture: ComponentFixture<FooterComponent>;

beforeEach(async(() => {
beforeEach((() => {
TestBed.configureTestingModule({
declarations: [FooterComponent]
}).compileComponents();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { async, TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing';

import { HeaderComponent } from './header.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
Expand All @@ -16,7 +16,7 @@ describe('HeaderComponent', () => {

const mockRouter = jasmine.createSpyObj('Router', ['navigate', 'events']);

beforeEach(async(() => {
beforeEach((() => {
TestBed.configureTestingModule({
imports: [BrowserAnimationsModule],
declarations: [HeaderComponent],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { HomeComponent } from './home.component';
import { RouterTestingModule } from '@angular/router/testing';
Expand All @@ -18,7 +18,7 @@ describe('HomeComponent', () => {
}
};

beforeEach(async(() => {
beforeEach((() => {
TestBed.configureTestingModule({
declarations: [HomeComponent],
imports: [RouterTestingModule],
Expand Down
Loading