Skip to content

Commit

Permalink
Merge ecaef7b into 3bc7162
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescoBorzi committed Apr 8, 2020
2 parents 3bc7162 + ecaef7b commit a173e35
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 49 deletions.
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Expand Up @@ -2,6 +2,7 @@ import 'reflect-metadata';
import '../polyfills';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';

import { AppRoutingModule } from './config/app-routing.module';
import { AppComponent } from './main/app.component';
Expand All @@ -21,6 +22,7 @@ import { SqlEditorModule } from './features/sql-editor/sql-editor.module';
declarations: [ AppComponent ],
imports: [
BrowserModule,
HttpClientModule,
/* Misc */
AppRoutingModule,
ConnectionWindowModule,
Expand Down
5 changes: 5 additions & 0 deletions src/app/main/app.component.html
Expand Up @@ -2,3 +2,8 @@
<keira-main-window *ngIf="mysqlService.connectionEstablished"></keira-main-window>

<div id="sqlite-e2e-test" [attr.e2e]="sqliteResult?.name" style="display: none"></div>

<div class="newer-version-alert text-center" *ngIf="showNewerVersionAlert">
<span>Psst...There is a newer version of Keira!</span>
<a class="btn btn-secondary btn-sm ml-2" [href]="KEIRA3_REPO_URL + '/releases'" target="_blank">Download</a>
</div>
19 changes: 19 additions & 0 deletions src/app/main/app.component.scss
@@ -0,0 +1,19 @@
.newer-version-alert {
position: fixed;
bottom: 3px;
left: 50%;
transform: translateX(-50%);
background-color: rgba(0, 0, 0, 0.8);
box-shadow: 0 0 46px 0 white;
color: white;
padding: 14px 30px;
border-radius: 10px;

span {
font-style: italic;
font-weight: bold;
}
.btn {
font-weight: bold;
}
}
96 changes: 64 additions & 32 deletions src/app/main/app.component.spec.ts
@@ -1,27 +1,27 @@
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { PerfectScrollbarModule } from 'ngx-perfect-scrollbar';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { PerfectScrollbarModule } from 'ngx-perfect-scrollbar';
import { ToastrModule, ToastrService } from 'ngx-toastr';
import { Subject } from 'rxjs';
import { instance, reset } from 'ts-mockito';

import { AppComponent } from './app.component';
import { SidebarComponent } from './main-window/sidebar/sidebar.component';
import { MainWindowComponent } from './main-window/main-window.component';
import { ElectronService } from '../shared/services/electron.service';
import { instance, reset } from 'ts-mockito';
import { MockedElectronService, MockedMysqlService } from '@keira-testing/mocks';
import { MysqlService } from '../shared/services/mysql.service';
import { ConnectionWindowComponent } from './connection-window/connection-window.component';
import { QueryErrorComponent } from '../shared/modules/query-output/query-error/query-error.component';
import { ModalConfirmModule } from '../shared/modules/modal-confirm/modal-confirm.module';
import { LogoutBtnComponent } from './main-window/sidebar/logout-btn/logout-btn.component';
import { ToastrModule, ToastrService } from 'ngx-toastr';
import { Subject } from 'rxjs';
import { LATEST_RELEASE_API_URL } from '@keira-constants/general';
import { version } from '../../../package.json';

describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let subject: Subject<boolean>;

beforeEach(async(() => {
TestBed.configureTestingModule({
Expand All @@ -39,6 +39,7 @@ describe('AppComponent', () => {
RouterTestingModule,
BrowserAnimationsModule,
PerfectScrollbarModule,
HttpClientTestingModule,
ModalConfirmModule,
ToastrModule.forRoot(),
],
Expand All @@ -49,43 +50,74 @@ describe('AppComponent', () => {
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
const setup = () => {
const fixture = TestBed.createComponent(AppComponent);
const component = fixture.componentInstance;
const toastrService: ToastrService = TestBed.inject(ToastrService);
const httpTestingController = TestBed.inject(HttpTestingController);

subject = new Subject<boolean>();
const connectionLostSubject = new Subject<boolean>();
// @ts-ignore
TestBed.inject(MysqlService)['connectionLost$'] = subject.asObservable();
TestBed.inject(MysqlService)['connectionLost$'] = connectionLostSubject.asObservable();

fixture.detectChanges();
});
return { fixture, component, connectionLostSubject, toastrService, httpTestingController };
};

it('should correctly react on connectionLost$ [connection lost]', () => {
const toastrService: ToastrService = TestBed.inject(ToastrService);
spyOnAllFunctions(toastrService);
describe('handleConnectionLostAlerts', () => {
it('should correctly react on connectionLost$ [connection lost]', () => {
const { fixture, toastrService, connectionLostSubject } = setup();
fixture.detectChanges();
spyOnAllFunctions(toastrService);

connectionLostSubject.next(false);
connectionLostSubject.next(false);
connectionLostSubject.next(false);

expect(toastrService.success).toHaveBeenCalledTimes(0);
expect(toastrService.error).toHaveBeenCalledTimes(1);
expect(toastrService.error).toHaveBeenCalledWith('Database connection lost');
});

subject.next(false);
subject.next(false);
subject.next(false);
it('should correctly react on connectionLost$ [reconnected]', () => {
const { fixture, connectionLostSubject, toastrService } = setup();
fixture.detectChanges();
spyOnAllFunctions(toastrService);

expect(toastrService.success).toHaveBeenCalledTimes(0);
expect(toastrService.error).toHaveBeenCalledTimes(1);
expect(toastrService.error).toHaveBeenCalledWith('Database connection lost');
connectionLostSubject.next(true);

expect(toastrService.error).toHaveBeenCalledTimes(0);
expect(toastrService.success).toHaveBeenCalledTimes(1);
expect(toastrService.success).toHaveBeenCalledWith('Database reconnected');
});
});

it('should correctly react on connectionLost$ [reconnected]', () => {
const toastrService: ToastrService = TestBed.inject(ToastrService);
spyOnAllFunctions(toastrService);
describe('handleNewerVersionAlert', () => {
it('should correctly query and show the alert when the latest version is different than the current one', () => {
const { fixture, httpTestingController } = setup();

fixture.detectChanges();

const req = httpTestingController.expectOne(LATEST_RELEASE_API_URL);
expect(req.request.method).toEqual('GET');
req.flush({ tag_name: 'some newer version' });

httpTestingController.verify();
});

it('should correctly query and NOT show the alert when the latest version the same as the current one', () => {
const { fixture, httpTestingController } = setup();

fixture.detectChanges();

subject.next(true);
const req = httpTestingController.expectOne(LATEST_RELEASE_API_URL);
expect(req.request.method).toEqual('GET');
req.flush({ tag_name: `v${version}` });

expect(toastrService.error).toHaveBeenCalledTimes(0);
expect(toastrService.success).toHaveBeenCalledTimes(1);
expect(toastrService.success).toHaveBeenCalledWith('Database reconnected');
httpTestingController.verify();
});
});

afterEach(() => {
fixture.debugElement.nativeElement.remove();
reset(MockedElectronService);
reset(MockedMysqlService);
});
Expand Down
65 changes: 48 additions & 17 deletions src/app/main/app.component.ts
@@ -1,45 +1,76 @@
import { Component, OnInit } from '@angular/core';
import { ToastrService } from 'ngx-toastr';
import { distinctUntilChanged } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';

import { MysqlService } from '../shared/services/mysql.service';
import { SqliteQueryService } from '@keira-shared/services/sqlite-query.service';
import { ElectronService } from '@keira-shared/services/electron.service';
import { KEIRA3_REPO_URL, LATEST_RELEASE_API_URL } from '@keira-constants/general';
import { version } from '../../../package.json';
import { SubscriptionHandler } from '@keira-shared/utils/subscription-handler/subscription-handler';

@Component({
selector: 'keira-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

export class AppComponent extends SubscriptionHandler implements OnInit {
public readonly KEIRA3_REPO_URL = KEIRA3_REPO_URL;
showNewerVersionAlert = false;
sqliteResult: { id: number; name: string };

constructor(
public mysqlService: MysqlService,
public toastrService: ToastrService,
private sqliteQueryService: SqliteQueryService,
private electronService: ElectronService,
) {}
private http: HttpClient,
) {
super();
}

ngOnInit(): void {
this.mysqlService.connectionLost$
.pipe(distinctUntilChanged())
.subscribe((status) => {
if (!status) {
this.toastrService.error('Database connection lost');
} else {
this.toastrService.success('Database reconnected');
}
});
this.handleConnectionLostAlerts();
this.handleSqliteTest();
this.handleNewerVersionAlert();
}

private handleSqliteTest() {
/* istanbul ignore next */
if (this.electronService.isElectron()) {
this.sqliteQueryService.query<{ id: number, name: string}>(
'SELECT * FROM achievements WHERE id = 970', true
).subscribe((result) => {
this.sqliteResult = result ? result[0] : null;
});
this.subscriptions.push(
this.sqliteQueryService.query<{ id: number, name: string }>(
'SELECT * FROM achievements WHERE id = 970', true
).subscribe((result) => {
this.sqliteResult = result ? result[0] : null;
}),
);
}
}

private handleConnectionLostAlerts() {
this.subscriptions.push(
this.mysqlService.connectionLost$
.pipe(distinctUntilChanged())
.subscribe((status) => {
if (!status) {
this.toastrService.error('Database connection lost');
} else {
this.toastrService.success('Database reconnected');
}
}),
);
}

private handleNewerVersionAlert() {
this.subscriptions.push(
this.http.get<{ tag_name: string }>(LATEST_RELEASE_API_URL).subscribe(release => {
const currentTag = `v${version}`;
if (currentTag !== release.tag_name) {
this.showNewerVersionAlert = true;
}
}),
);
}
}
1 change: 1 addition & 0 deletions src/app/shared/constants/general.ts
Expand Up @@ -3,3 +3,4 @@ export const KEIRA3_REPO_URL = 'https://github.com/azerothcore/Keira3';
export const AC_FORUM_URL = 'https://github.com/azerothcore/forum/issues';
export const AC_DISCORD_URL = 'https://discordapp.com/channels/217589275766685707/536630256048799744';
export const PAYPAL_DONATE_URL = 'https://www.paypal.me/francesco92dev';
export const LATEST_RELEASE_API_URL = 'https://api.github.com/repos/azerothcore/Keira3/releases/latest';

0 comments on commit a173e35

Please sign in to comment.