Skip to content

Commit

Permalink
Transport provider crud prepare rebased (#109)
Browse files Browse the repository at this point in the history
* cherry pick new commits

* service tests on top of reverted work
  • Loading branch information
esteban-gs authored Apr 5, 2022
1 parent eaea96c commit cc14d5a
Show file tree
Hide file tree
Showing 19 changed files with 299 additions and 95 deletions.
6 changes: 4 additions & 2 deletions src/app/configs/configs-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { AuthGuardService } from "../shared";
import { ConfigsComponent } from "./configs.component";
import { TransportProviderComponent } from "./transport-provider/transport-provider.component";

const routes: Routes = [
{
Expand All @@ -12,7 +11,10 @@ const routes: Routes = [
children: [
{
path: "transport-providers",
component: TransportProviderComponent,
loadChildren: () =>
import(`./transport-providers/transport-providers.module`).then(
(m) => m.TransportProvidersModule
),
canActivate: [AuthGuardService],
},
],
Expand Down
3 changes: 0 additions & 3 deletions src/app/configs/configs.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
<p>
configs works!
</p>
<router-outlet></router-outlet>
7 changes: 3 additions & 4 deletions src/app/configs/configs.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { ConfigsComponent } from "./configs.component";
import { ConfigsRoutingModule } from "./configs-routing.module";
import { TransportProviderComponent } from "./transport-provider/transport-provider.component";
import { RecordsTableModule } from "../shared/components/records-table/records-table.module";
import { TransportProvidersModule } from "./transport-providers/transport-providers.module";

@NgModule({
imports: [CommonModule, ConfigsRoutingModule, RecordsTableModule],
declarations: [ConfigsComponent, TransportProviderComponent],
imports: [CommonModule, ConfigsRoutingModule, TransportProvidersModule],
declarations: [ConfigsComponent],
})
export class ConfigsModule {
constructor() {
Expand Down
29 changes: 0 additions & 29 deletions src/app/configs/transport-provider/transport-provider.component.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
import { HttpClientModule } from "@angular/common/http";
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { RouterTestingModule } from "@angular/router/testing";
import { TransportProvidersStoreService } from "src/app/shared/services/transport-providers-store.service";
import { TransportProvidersStoreServiceSpy } from "src/app/shared/testing/services.spy";
import { TransportProvidersServiceSpy } from "src/app/shared/testing/services.spy";

import { TransportProviderComponent } from "./transport-provider.component";
import { TransportProviderListComponent } from "./transport-provider-list.component";
import { TransportProvidersService } from "src/app/online-orders/transport-providers.service";

describe("c", () => {
let component: TransportProviderComponent;
let fixture: ComponentFixture<TransportProviderComponent>;
let component: TransportProviderListComponent;
let fixture: ComponentFixture<TransportProviderListComponent>;
// const httpClientSpy: any = jasmine.createSpyObj("HttpClient", ["get"]);

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [TransportProviderComponent],
declarations: [TransportProviderListComponent],
imports: [HttpClientModule, RouterTestingModule],
providers: [
{
provide: TransportProvidersStoreService,
useClass: TransportProvidersStoreServiceSpy,
provide: TransportProvidersService,
useClass: TransportProvidersServiceSpy,
},
],
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(TransportProviderComponent);
fixture = TestBed.createComponent(TransportProviderListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Component, OnInit } from "@angular/core";
import { Observable } from "rxjs";
import { filter, first } from "rxjs/operators";
import { TransportProvider } from "src/app/online-orders/shared";
import { TransportProvidersService } from "src/app/online-orders/transport-providers.service";

@Component({
selector: "app-transport-list-provider",
templateUrl: "./transport-provider-list.component.html",
styleUrls: ["./transport-provider-list.component.css"],
})
export class TransportProviderListComponent implements OnInit {
public transportProviders$: Observable<TransportProvider[]>;
public excludeCols: string[] = ["availabilityRules"];

constructor(private service: TransportProvidersService) {}

handleRowSelect(selectedRecord: TransportProvider): void {
// navigate to single record
console.log(selectedRecord);
}

ngOnInit(): void {
this.transportProviders$ = this.service.getTransportProviders();

// filter((x) => x.length !== 0 || !x)
// first()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>transport-providers-edit works!</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ComponentFixture, TestBed } from "@angular/core/testing";

import { TransportProvidersEditComponent } from "./transport-providers-edit.component";

describe("TransportProvidersEditComponent", () => {
let component: TransportProvidersEditComponent;
let fixture: ComponentFixture<TransportProvidersEditComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [TransportProvidersEditComponent],
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(TransportProvidersEditComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it("should create", () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Component, OnInit } from "@angular/core";

@Component({
selector: "app-transport-providers-edit",
templateUrl: "./transport-providers-edit.component.html",
styleUrls: ["./transport-providers-edit.component.css"],
})
export class TransportProvidersEditComponent {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { AuthGuardService } from "src/app/shared/services/auth-guard.service";
import { TransportProviderListComponent } from "./tranport-providers-list/transport-provider-list.component";
import { TransportProvidersEditComponent } from "./transport-providers-edit/transport-providers-edit.component";

const routes: Routes = [
{
path: "",
redirectTo: "list",
},
{
path: "list",
component: TransportProviderListComponent,
canActivate: [AuthGuardService],
},
{
path: "edit:id",
component: TransportProvidersEditComponent,
canActivate: [AuthGuardService],
},
];

@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class TransportProvidersRoutingModule {}
15 changes: 15 additions & 0 deletions src/app/configs/transport-providers/transport-providers.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { TransportProviderListComponent } from "./tranport-providers-list/transport-provider-list.component";
import { RecordsTableModule } from "src/app/shared/components/records-table/records-table.module";
import { TransportProvidersRoutingModule } from "./transport-providers-routing.module";
import { TransportProvidersEditComponent } from "./transport-providers-edit/transport-providers-edit.component";

@NgModule({
declarations: [
TransportProviderListComponent,
TransportProvidersEditComponent,
],
imports: [CommonModule, RecordsTableModule, TransportProvidersRoutingModule],
})
export class TransportProvidersModule {}
9 changes: 9 additions & 0 deletions src/app/menu/load-menu-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ export function loadMenuRules(authList: string[]): Array<MenuRule> {
icon: "airport_shuttle",
routerLink: ["configuration/transport-providers"],
authorizedRoles: [LRole.ADMIN],
items: [
new MenuRule({
id: 14,
label: "List",
icon: "list",
routerLink: ["configuration/transport-providers/list"],
authorizedRoles: [LRole.ADMIN],
}),
],
}),
],
}),
Expand Down
128 changes: 115 additions & 13 deletions src/app/online-orders/transport-providers.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,129 @@
import { TestBed, inject } from "@angular/core/testing";
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { TestBed } from "@angular/core/testing";

import { TransportProvidersService } from "./transport-providers.service";
import { HttpClientModule } from "@angular/common/http";
import { HttpClient, HttpClientModule } from "@angular/common/http";
import {
HttpClientTestingModule,
HttpTestingController,
} from "@angular/common/http/testing";
import { MessagesServiceSpy } from "../shared/testing/services.spy";
import { MessagesService } from "../shared/components/messages/messages.service";
import { TransportProvider } from "./shared";
import { of } from "rxjs";

describe("TransportProviderService", () => {
let service: TransportProvidersService;
let clientSpy: jasmine.SpyObj<HttpClient>;
let messageSpy: jasmine.SpyObj<MessagesService>;
let httpClientSpy: jasmine.SpyObj<HttpClient>;

beforeEach(() => {
httpClientSpy = jasmine.createSpyObj("HttpClient", ["get"]);

TestBed.configureTestingModule({
providers: [
TransportProvidersService,
{
provide: HttpClient,
useValue: httpClientSpy,
},
{
provide: MessagesService,
useClass: MessagesServiceSpy,
},
],
imports: [],
});
service = TestBed.inject(TransportProvidersService);
httpClientSpy = TestBed.inject(HttpClient) as jasmine.SpyObj<HttpClient>;
messageSpy = TestBed.inject(
MessagesService
) as jasmine.SpyObj<MessagesService>;
});

it("should be created", () => {
expect(service).toBeTruthy();
});

describe("chaching", () => {
beforeEach(() => {
spyOn(HttpClient.prototype, "get").calls.reset();
httpClientSpy.get.and.returnValue(
of({
data: [new TransportProvider()],
})
);
});

it("when api called, session var is set", (done: DoneFn) => {
service.getTransportProviders().subscribe(() => {
expect(
(
JSON.parse(
sessionStorage.getItem("tranportProviders")
) as TransportProvider[]
).length
).toBe(1);
done();
});
});

it("when none, should callApi", (done: DoneFn) => {
service.getTransportProviders().subscribe(() => {
expect(httpClientSpy.get.calls.count()).toBe(1);
done();
});
});

it("when NOT stale, should not callApi", (done: DoneFn) => {
service.providersAge = Date.now() - 300 * 999; // age logic is in service

service.getTransportProviders().subscribe(() => {
expect(httpClientSpy.get.calls.count()).toBe(0);
done();
});
});
});
});

describe("TransportProviderServiceHttp", () => {
let service: TransportProvidersService;
let httpMock: HttpTestingController;
let messageSpy: jasmine.SpyObj<MessagesService>;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [TransportProvidersService],
providers: [
{
provide: MessagesService,
useClass: MessagesServiceSpy,
},
],
imports: [HttpClientModule, HttpClientTestingModule],
})
.compileComponents()
.catch((e) => console.error(e));
TestBed.inject(HttpTestingController);
});
service = TestBed.inject(TransportProvidersService);
httpMock = TestBed.inject(HttpTestingController);
messageSpy = TestBed.inject(
MessagesService
) as jasmine.SpyObj<MessagesService>;
});

it("should be created", inject(
[TransportProvidersService],
(service: TransportProvidersService) => {
expect(service).toBeTruthy();
}
));
describe("when http error", () => {
it("should call showErrors toast", (done: DoneFn) => {
service.getTransportProviders().subscribe({
next: () => done.fail("should have failed with the 500 error"),
error: () => {
expect(messageSpy.showErrors.calls.count()).toBe(1);
done();
},
});

const testReq = httpMock.expectOne(
"http://localhost:9876/api/transportproviders"
);
expect(testReq.request.method).toEqual("GET");
testReq.flush("", { status: 500, statusText: "SErver Error" });
});
});
});
Loading

0 comments on commit cc14d5a

Please sign in to comment.