Skip to content

Commit

Permalink
Update ngx-webstorage in test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocelyn Cabildo authored and Jocelyn Cabildo committed Sep 10, 2024
1 parent 1487b59 commit 6d6d190
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DataService } from "src/app/services/data.service";
import { Router } from "@angular/router";
import { FoiValidComponent } from "src/app/utils-components/foi-valid/foi-valid.component";
import { FoiRequest } from "src/app/models/FoiRequest";
import { NgxWebstorageModule } from "ngx-webstorage";
import { provideNgxWebstorage } from "ngx-webstorage";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { OwlNativeDateTimeModule, OwlDateTimeModule } from "@danielmoncada/angular-datetime-picker";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
Expand All @@ -29,12 +29,11 @@ describe("AdoptiveParentsComponent", () => {
imports: [
HttpClientTestingModule,
ReactiveFormsModule,
NgxWebstorageModule.forRoot(),
OwlDateTimeModule,
OwlNativeDateTimeModule,
FontAwesomeModule,
],
providers: [DataService, { provide: Router, useClass: MockRouter }],
providers: [DataService, { provide: Router, useClass: MockRouter }, provideNgxWebstorage()],
}).compileComponents();
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { MockRouter } from "../../MockClasses";
import { FoiValidComponent } from "src/app/utils-components/foi-valid/foi-valid.component";
import { FoiFileinputComponent } from "src/app/utils-components/foi-fileinput/foi-fileinput.component";
import { FoiRequest } from "src/app/models/FoiRequest";
import { NgxWebstorageModule } from "ngx-webstorage";
import { provideNgxWebstorage } from "ngx-webstorage";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
import { OwlNativeDateTimeModule, OwlDateTimeModule } from "@danielmoncada/angular-datetime-picker";
Expand All @@ -24,12 +24,11 @@ describe("AnotherInformationComponent", () => {
imports: [
HttpClientTestingModule,
ReactiveFormsModule,
NgxWebstorageModule.forRoot(),
OwlDateTimeModule,
OwlNativeDateTimeModule,
FontAwesomeModule,
],
providers: [DataService, { provide: Router, useClass: MockRouter }],
providers: [DataService, { provide: Router, useClass: MockRouter }, provideNgxWebstorage()],
}).compileComponents();
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Router } from "@angular/router";
import { FoiValidComponent } from "src/app/utils-components/foi-valid/foi-valid.component";
import { FoiFileinputComponent } from "src/app/utils-components/foi-fileinput/foi-fileinput.component";
import { FoiRequest } from "src/app/models/FoiRequest";
import { NgxWebstorageModule } from "ngx-webstorage";
import { provideNgxWebstorage } from "ngx-webstorage";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
import { OwlNativeDateTimeModule, OwlDateTimeModule } from "@danielmoncada/angular-datetime-picker";
Expand All @@ -24,12 +24,11 @@ describe("ChildInformationComponent", () => {
imports: [
HttpClientTestingModule,
ReactiveFormsModule,
NgxWebstorageModule.forRoot(),
OwlDateTimeModule,
OwlNativeDateTimeModule,
FontAwesomeModule,
],
providers: [DataService, { provide: Router, useClass: MockRouter }],
providers: [DataService, { provide: Router, useClass: MockRouter }, provideNgxWebstorage()],
}).compileComponents();
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MockRouter } from "../../MockClasses";
import { Router } from "@angular/router";
import { FoiRequest } from "src/app/models/FoiRequest";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { NgxWebstorageModule } from "ngx-webstorage";
import { provideNgxWebstorage } from "ngx-webstorage";
import { UtilsComponentsModule } from "src/app/utils-components/utils-components.module";

describe("ContactInfoOptionsComponent", () => {
Expand All @@ -17,8 +17,8 @@ describe("ContactInfoOptionsComponent", () => {
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [ContactInfoOptionsComponent],
imports: [HttpClientTestingModule, ReactiveFormsModule, NgxWebstorageModule.forRoot(), UtilsComponentsModule],
providers: [DataService, { provide: Router, useClass: MockRouter }],
imports: [HttpClientTestingModule, ReactiveFormsModule, UtilsComponentsModule],
providers: [DataService, { provide: Router, useClass: MockRouter }, provideNgxWebstorage()],
}).compileComponents();
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DataService } from "src/app/services/data.service";
import { MockRouter } from "../../MockClasses";
import { Router } from "@angular/router";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { NgxWebstorageModule } from "ngx-webstorage";
import { provideNgxWebstorage } from "ngx-webstorage";
import { FoiRequest } from "src/app/models/FoiRequest";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
import { OwlNativeDateTimeModule, OwlDateTimeModule } from "@danielmoncada/angular-datetime-picker";
Expand All @@ -23,12 +23,11 @@ describe("ContactInfoComponent", () => {
imports: [
HttpClientTestingModule,
ReactiveFormsModule,
NgxWebstorageModule.forRoot(),
OwlDateTimeModule,
OwlNativeDateTimeModule,
FontAwesomeModule,
],
providers: [DataService, { provide: Router, useClass: MockRouter }],
providers: [DataService, { provide: Router, useClass: MockRouter }, provideNgxWebstorage()],
}).compileComponents();
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MockRouter } from "../../MockClasses";
import { Router } from "@angular/router";
import { FoiRequest } from "src/app/models/FoiRequest";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { NgxWebstorageModule } from "ngx-webstorage";
import { provideNgxWebstorage } from "ngx-webstorage";
import { UtilsComponentsModule } from "src/app/utils-components/utils-components.module";
import { OwlNativeDateTimeModule, OwlDateTimeModule } from "@danielmoncada/angular-datetime-picker";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
Expand Down Expand Up @@ -41,13 +41,12 @@ describe("DescriptionTimeframeComponent", () => {
imports: [
HttpClientTestingModule,
ReactiveFormsModule,
NgxWebstorageModule.forRoot(),
UtilsComponentsModule,
OwlDateTimeModule,
OwlNativeDateTimeModule,
FontAwesomeModule,
],
providers: [DataService, { provide: Router, useClass: MockRouter }],
providers: [DataService, { provide: Router, useClass: MockRouter }, provideNgxWebstorage()],
}).compileComponents();
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DataService } from "src/app/services/data.service";
import { Router } from "@angular/router";
import { MockRouter } from "../../MockClasses";
import { UtilsComponentsModule } from "src/app/utils-components/utils-components.module";
import { NgxWebstorageModule } from "ngx-webstorage";
import { provideNgxWebstorage } from "ngx-webstorage";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { FoiRequest } from "src/app/models/FoiRequest";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
Expand All @@ -21,13 +21,12 @@ describe("VerifyYourIdentityComponent", () => {
imports: [
HttpClientTestingModule,
ReactiveFormsModule,
NgxWebstorageModule.forRoot(),
UtilsComponentsModule,
OwlDateTimeModule,
OwlNativeDateTimeModule,
FontAwesomeModule,
],
providers: [DataService, { provide: Router, useClass: MockRouter }],
providers: [DataService, { provide: Router, useClass: MockRouter }, provideNgxWebstorage()],
}).compileComponents();
}));

Expand Down
74 changes: 38 additions & 36 deletions web/src/app/services/data.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import { TestBed } from "@angular/core/testing";
import { DataService } from "./data.service";
import { TransomApiClientService } from "../transom-api-client.service";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { NgxWebstorageModule } from "ngx-webstorage";
import { provideNgxWebstorage } from "ngx-webstorage";
import { FoiRoute } from "../models/FoiRoute";
import { FoiRequest } from "../models/FoiRequest";
import { of, Observable } from 'rxjs';
import { of, Observable } from "rxjs";

class MockApiClient {
setHeader(key:string, value:string){

}
setHeader(key: string, value: string) {}
postFoiRequest(foiRequest: FoiRequest): Observable<any> {
return of(true);
}
Expand All @@ -22,11 +20,12 @@ describe("DataService", () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, NgxWebstorageModule.forRoot()],
imports: [HttpClientTestingModule],
providers: [
{ provide: TransomApiClientService, useClass: MockApiClient }
{ provide: TransomApiClientService, useClass: MockApiClient },
provideNgxWebstorage(),
//{ provide: LocalStorageService, useClass: MockLocalStorage }
]
],
});
service = TestBed.get(DataService);
sessionStorage.removeItem("foi-request");
Expand All @@ -46,8 +45,8 @@ describe("DataService", () => {
it("should set and get the current request state", () => {
const foi: FoiRequest = {
requestData: {
personalInfo: { foo: "bar" }
}
personalInfo: { foo: "bar" },
},
};
service.setCurrentState(foi);
let returnedState = service.getCurrentState("personalInfo", "anotherInfo");
Expand All @@ -61,25 +60,25 @@ describe("DataService", () => {
it("should set current state from a FormGroup", () => {
const foi: FoiRequest = {
requestData: {
personalInfo: { foo: "bar" }
}
personalInfo: { foo: "bar" },
},
};
service.setCurrentState(foi);
let req = service.getCurrentState();
const testForm: any = {
value: { firstName: "Paul" }
value: { firstName: "Paul" },
};
service.setCurrentState(req, "personalInfo", testForm);

let storedFoiRequest = sessionStorage.getItem("foi-request");
expect(storedFoiRequest).toEqual('{"requestData":{"personalInfo":{"firstName":"Paul"}}}');
});

it("should record a child file attachment in session storage base64 encoded", done => {
it("should record a child file attachment in session storage base64 encoded", (done) => {
const f = new File(["foo"], "foo.txt", {
type: "text/plain"
type: "text/plain",
});
service.setChildFileAttachment(f).subscribe(value => {
service.setChildFileAttachment(f).subscribe((value) => {
if (value) {
let storedData = sessionStorage.getItem(service.childFileKey);
expect(storedData).toEqual("data:text/plain;base64,Zm9v");
Expand All @@ -92,11 +91,11 @@ describe("DataService", () => {
});
});

it("should record a personal file attachment in session storage base64 encoded", done => {
it("should record a personal file attachment in session storage base64 encoded", (done) => {
const f = new File(["foo"], "foo.txt", {
type: "text/plain"
type: "text/plain",
});
service.setPersonFileAttachment(f).subscribe(value => {
service.setPersonFileAttachment(f).subscribe((value) => {
if (value) {
let storedData = sessionStorage.getItem(service.personFileKey);
expect(storedData).toEqual("data:text/plain;base64,Zm9v");
Expand All @@ -109,11 +108,11 @@ describe("DataService", () => {
});
});

it("should return a file object from session storage", done => {
it("should return a file object from session storage", (done) => {
const f = new File(["foo"], "foo.txt", {
type: "text/plain"
type: "text/plain",
});
service.setPersonFileAttachment(f).subscribe(value => {
service.setPersonFileAttachment(f).subscribe((value) => {
if (value) {
const fReturned = service.getBlobFrom(service.personFileKey);
const fr: FileReader = new FileReader();
Expand All @@ -126,36 +125,39 @@ describe("DataService", () => {
});
});

it("should return ministries from data.json", done => {
service.getMinistries().subscribe(mins => {
it("should return ministries from data.json", (done) => {
service.getMinistries().subscribe((mins) => {
expect(mins).toBeTruthy();
expect(mins.length).toBeGreaterThan(1);
done();
});
});

it("should return topics by request subject data.json", () => {
const topics = service.getTopicsObj({child: true, yourself: true});
const topics = service.getTopicsObj({ child: true, yourself: true });
expect(topics).toBeTruthy();
expect(topics.length).toBeGreaterThan(1);
});

it('should submit the request through the api client', (done) => {
it("should submit the request through the api client", (done) => {
const apiClient: TransomApiClientService = TestBed.get(TransomApiClientService);
spyOn(apiClient, 'setHeader').and.callThrough();
spyOn(apiClient, 'postFoiRequest').and.callThrough();
const obs = service.submitRequest('token1', 'nonce1', {requestData:{personalInfo:{foo:"bar"}}});
spyOn(apiClient, "setHeader").and.callThrough();
spyOn(apiClient, "postFoiRequest").and.callThrough();

const obs = service.submitRequest("token1", "nonce1", { requestData: { personalInfo: { foo: "bar" } } });
expect(obs).toBeTruthy();
expect(apiClient.setHeader).toHaveBeenCalledTimes(2);
expect(apiClient.setHeader).toHaveBeenCalledWith( 'Authorization', 'Bearer token1' );
expect(apiClient.setHeader).toHaveBeenCalledWith( 'captcha-nonce', 'nonce1' );
expect(apiClient.setHeader).toHaveBeenCalledWith("Authorization", "Bearer token1");
expect(apiClient.setHeader).toHaveBeenCalledWith("captcha-nonce", "nonce1");

expect(apiClient.postFoiRequest).toHaveBeenCalledWith({requestData:{personalInfo:{foo:"bar"}}, attachments: []});
expect(apiClient.postFoiRequest).toHaveBeenCalledWith({
requestData: { personalInfo: { foo: "bar" } },
attachments: [],
});

obs.subscribe(retval => {
obs.subscribe((retval) => {
expect(retval).toBeTruthy();
done();
})
} );
});
});
});
Loading

0 comments on commit 6d6d190

Please sign in to comment.