From 0c22617ed0ea22de966d5c4c6befa20c5e1d83bc Mon Sep 17 00:00:00 2001 From: sggerard Date: Tue, 26 Nov 2024 15:49:32 -0800 Subject: [PATCH] Add tests for app.module --- frontend/jest.config.js | 3 ++- frontend/src/app/app.module.spec.ts | 21 +++++++++++++++++++ .../src/app/services/config.service.spec.ts | 18 ++++++++++++---- 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 frontend/src/app/app.module.spec.ts diff --git a/frontend/jest.config.js b/frontend/jest.config.js index cadbb4d..ddbaadd 100644 --- a/frontend/jest.config.js +++ b/frontend/jest.config.js @@ -14,6 +14,7 @@ module.exports = { "@pipes(.*)": "/src/app/pipes/$1", "@projects(.*)": "/src/app/projects/$1", "@services(.*)": "/src/app/services/$1", - "@shared(.*)": "/src/app/shared/$1" + "@shared(.*)": "/src/app/shared/$1", + '^environments/(.*)$': '/src/environments/$1', } }; \ No newline at end of file diff --git a/frontend/src/app/app.module.spec.ts b/frontend/src/app/app.module.spec.ts new file mode 100644 index 0000000..d0cbf35 --- /dev/null +++ b/frontend/src/app/app.module.spec.ts @@ -0,0 +1,21 @@ +import { TestBed } from '@angular/core/testing'; +import { AppModule } from './app.module'; + +jest.mock('environments/environment', () => ({ + environment: { + production: false, + graphURI: 'http://localhost:1337', + }, + })); + +describe('AppModule', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [AppModule], + }).compileComponents(); + }); + + it('should create the app module', () => { + expect(AppModule).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/services/config.service.spec.ts b/frontend/src/app/services/config.service.spec.ts index 4800316..4af51e4 100644 --- a/frontend/src/app/services/config.service.spec.ts +++ b/frontend/src/app/services/config.service.spec.ts @@ -1,17 +1,23 @@ import { TestBed } from '@angular/core/testing'; import { ConfigService } from '@services/config.service'; import { ContentService } from './content-service'; -import { HttpClient, HttpHandler } from '@angular/common/http'; +import { HttpClient } from '@angular/common/http'; import { Router } from '@angular/router'; +import { of } from 'rxjs'; // Mocking ContentService const mockContentService = { getRoutes: jest.fn(), getGlobalContent: jest.fn() } +const mockConfigResponse = {debugMode: true }; +const mockHttpClient = { + get: jest.fn( () => of(mockConfigResponse)) +} describe('ConfigService', () => { let configService: ConfigService; + let httpClient: HttpClient; let router: Router; let contentService: ContentService; @@ -21,8 +27,7 @@ describe('ConfigService', () => { TestBed.configureTestingModule({ providers: [ ConfigService, - HttpClient, - HttpHandler, + { provide: HttpClient, useValue: mockHttpClient }, { provide: Router, useValue: { config: [], resetConfig: jest.fn() } }, { provide: ContentService, useValue: mockContentService } ], @@ -31,6 +36,7 @@ describe('ConfigService', () => { }); configService = TestBed.inject(ConfigService); + httpClient = TestBed.inject(HttpClient); router = TestBed.inject(Router); contentService = TestBed.inject(ContentService); }); @@ -39,17 +45,21 @@ describe('ConfigService', () => { jest.clearAllMocks(); }); + describe('init', () => { it('should initialize routes and configuration successfully', async () => { - // Mock ContentService methods + // Mock ContentService & httpClient methods const mockRoutes = [{ attributes: { route: 'test-route' } }]; mockContentService.getRoutes.mockResolvedValue(mockRoutes); mockContentService.getGlobalContent.mockResolvedValue({}); await configService.init(); + // Expectation: ConfigService methods and properties have been called or set correctly expect(contentService.getRoutes).toHaveBeenCalled(); expect(contentService.getGlobalContent).toHaveBeenCalled(); + expect(httpClient.get).toHaveBeenCalledWith('api/config/BCMI'); + expect(configService.config).toEqual(mockConfigResponse); expect(router.resetConfig).toHaveBeenCalled(); }); });