Skip to content

Commit

Permalink
Add tests for app.module
Browse files Browse the repository at this point in the history
  • Loading branch information
sggerard committed Nov 26, 2024
1 parent 456accf commit 0c22617
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion frontend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
"@pipes(.*)": "<rootDir>/src/app/pipes/$1",
"@projects(.*)": "<rootDir>/src/app/projects/$1",
"@services(.*)": "<rootDir>/src/app/services/$1",
"@shared(.*)": "<rootDir>/src/app/shared/$1"
"@shared(.*)": "<rootDir>/src/app/shared/$1",
'^environments/(.*)$': '<rootDir>/src/environments/$1',
}
};
21 changes: 21 additions & 0 deletions frontend/src/app/app.module.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
18 changes: 14 additions & 4 deletions frontend/src/app/services/config.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 }
],
Expand All @@ -31,6 +36,7 @@ describe('ConfigService', () => {
});

configService = TestBed.inject(ConfigService);
httpClient = TestBed.inject(HttpClient);
router = TestBed.inject(Router);
contentService = TestBed.inject(ContentService);
});
Expand All @@ -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();
});
});
Expand Down

0 comments on commit 0c22617

Please sign in to comment.