|
| 1 | +import { TestBed } from "@angular/core/testing"; |
| 2 | +import { AppConfig, AppConfigService } from "./app-config.service"; |
| 3 | +import { provideHttpClient } from "@angular/common/http"; |
| 4 | +import { |
| 5 | + HttpTestingController, |
| 6 | + provideHttpClientTesting, |
| 7 | +} from "@angular/common/http/testing"; |
| 8 | + |
| 9 | +describe("AppConfigService", () => { |
| 10 | + let service: AppConfigService; |
| 11 | + let httpTesting: HttpTestingController; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + TestBed.configureTestingModule({ |
| 15 | + providers: [ |
| 16 | + AppConfigService, |
| 17 | + provideHttpClient(), |
| 18 | + provideHttpClientTesting(), |
| 19 | + ], |
| 20 | + }); |
| 21 | + httpTesting = TestBed.inject(HttpTestingController); |
| 22 | + service = TestBed.inject(AppConfigService); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should be created", () => { |
| 26 | + expect(httpTesting).toBeTruthy(); |
| 27 | + expect(service).toBeTruthy(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should load app config from backend", async () => { |
| 31 | + const mockConfig = { |
| 32 | + production: false, |
| 33 | + logoWidth: "500", |
| 34 | + facility: "test-facility", |
| 35 | + oaiProviderRoute: "test-oai", |
| 36 | + doiBaseUrl: "test-doi", |
| 37 | + directMongoAccess: false, |
| 38 | + accessDataHref: "test-access", |
| 39 | + accessInstructions: "test-instructions", |
| 40 | + scicatBaseUrl: "test-scicat", |
| 41 | + showLogoBanner: true, |
| 42 | + logoBanner: "test-logo", |
| 43 | + lbBaseUrl: "test-lb", |
| 44 | + statusMessage: "test-status", |
| 45 | + statusCode: "INFO", |
| 46 | + contactEmail: "test-contact", |
| 47 | + }; |
| 48 | + const configPromise = service.loadAppConfig(); |
| 49 | + const req = httpTesting.expectOne("/config"); |
| 50 | + expect(req.request.method).toBe("GET"); |
| 51 | + req.flush(mockConfig); |
| 52 | + await configPromise; |
| 53 | + console.log("Config loaded:", service.getConfig()); |
| 54 | + expect(service.getConfig()).toEqual(mockConfig as AppConfig); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should set defaults for status banner and help if not provided", async () => { |
| 58 | + const mockConfig = { |
| 59 | + production: false, |
| 60 | + logoWidth: "500", |
| 61 | + facility: "test-facility", |
| 62 | + oaiProviderRoute: "test-oai", |
| 63 | + doiBaseUrl: "test-doi", |
| 64 | + directMongoAccess: false, |
| 65 | + accessDataHref: "test-access", |
| 66 | + accessInstructions: "test-instructions", |
| 67 | + scicatBaseUrl: "test-scicat", |
| 68 | + showLogoBanner: true, |
| 69 | + logoBanner: "test-logo", |
| 70 | + lbBaseUrl: "test-lb", |
| 71 | + }; |
| 72 | + const configPromise = service.loadAppConfig(); |
| 73 | + const req = httpTesting.expectOne("/config"); |
| 74 | + expect(req.request.method).toBe("GET"); |
| 75 | + req.flush(mockConfig); |
| 76 | + await configPromise; |
| 77 | + const config = service.getConfig(); |
| 78 | + expect(config.statusMessage).toBe(""); |
| 79 | + expect(config.statusCode).toBe("NONE"); |
| 80 | + expect(config.contactEmail).toBe(""); |
| 81 | + }); |
| 82 | +}); |
0 commit comments