Skip to content

Commit 5e1f1df

Browse files
Copilotrenemadsen
andcommitted
Convert service tests from Jasmine/Karma to Jest and update workflows
Co-authored-by: renemadsen <[email protected]>
1 parent 28517fd commit 5e1f1df

File tree

3 files changed

+33
-64
lines changed

3 files changed

+33
-64
lines changed

.github/workflows/dotnet-core-master.yml

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,18 @@ jobs:
7171
- name: Run Angular unit tests
7272
run: |
7373
cd eform-angular-frontend/eform-client
74-
# Check if Karma is installed
75-
if [ ! -d "node_modules/karma" ]; then
76-
echo "⚠️ Karma is not installed in the frontend repository."
77-
echo "Unit tests require Karma to be installed. Skipping unit tests."
74+
# Check if Jest is configured
75+
if [ ! -f "jest.config.js" ] && [ ! -f "jest.config.ts" ]; then
76+
echo "⚠️ Jest is not configured in the frontend repository."
77+
echo "Unit tests require Jest to be configured. Skipping unit tests."
7878
echo ""
79-
echo "To enable unit tests, add these dependencies to the frontend's package.json:"
80-
echo " \"karma\": \"~6.4.0\","
81-
echo " \"karma-chrome-launcher\": \"~3.1.0\","
82-
echo " \"karma-coverage\": \"~2.2.0\","
83-
echo " \"karma-jasmine\": \"~5.1.0\","
84-
echo " \"karma-jasmine-html-reporter\": \"~2.0.0\""
85-
echo ""
86-
echo "And add this test script:"
87-
echo " \"test:ci\": \"ng test --no-watch --no-progress --browsers=ChromeHeadless --code-coverage\""
79+
echo "To enable unit tests, ensure Jest is configured in the frontend repository."
8880
exit 0
8981
fi
9082
91-
# Try different test commands based on what's available in package.json
92-
if grep -q '"test:ci"' package.json 2>/dev/null; then
93-
echo "Running with test:ci script..."
94-
npm run test:ci -- --include='**/time-planning-pn/**/*.spec.ts'
95-
elif grep -q '"test"' package.json 2>/dev/null; then
96-
echo "Running with test script..."
97-
npm run test -- --watch=false --browsers=ChromeHeadless --include='**/time-planning-pn/**/*.spec.ts'
98-
else
99-
echo "No test script found in package.json, skipping unit tests"
100-
fi
83+
# Run Jest tests for time-planning-pn plugin
84+
echo "Running Jest tests for time-planning-pn plugin..."
85+
npm test -- --testPathPattern=time-planning-pn --coverage --collectCoverageFrom='src/app/plugins/modules/time-planning-pn/**/*.ts' --coveragePathIgnorePatterns='\.spec\.ts$'
10186
pn-test:
10287
needs: build
10388
runs-on: ubuntu-latest

.github/workflows/dotnet-core-pr.yml

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,18 @@ jobs:
6565
- name: Run Angular unit tests
6666
run: |
6767
cd eform-angular-frontend/eform-client
68-
# Check if Karma is installed
69-
if [ ! -d "node_modules/karma" ]; then
70-
echo "⚠️ Karma is not installed in the frontend repository."
71-
echo "Unit tests require Karma to be installed. Skipping unit tests."
68+
# Check if Jest is configured
69+
if [ ! -f "jest.config.js" ] && [ ! -f "jest.config.ts" ]; then
70+
echo "⚠️ Jest is not configured in the frontend repository."
71+
echo "Unit tests require Jest to be configured. Skipping unit tests."
7272
echo ""
73-
echo "To enable unit tests, add these dependencies to the frontend's package.json:"
74-
echo " \"karma\": \"~6.4.0\","
75-
echo " \"karma-chrome-launcher\": \"~3.1.0\","
76-
echo " \"karma-coverage\": \"~2.2.0\","
77-
echo " \"karma-jasmine\": \"~5.1.0\","
78-
echo " \"karma-jasmine-html-reporter\": \"~2.0.0\""
79-
echo ""
80-
echo "And add this test script:"
81-
echo " \"test:ci\": \"ng test --no-watch --no-progress --browsers=ChromeHeadless --code-coverage\""
73+
echo "To enable unit tests, ensure Jest is configured in the frontend repository."
8274
exit 0
8375
fi
8476
85-
# Try different test commands based on what's available in package.json
86-
if grep -q '"test:ci"' package.json 2>/dev/null; then
87-
echo "Running with test:ci script..."
88-
npm run test:ci -- --include='**/time-planning-pn/**/*.spec.ts'
89-
elif grep -q '"test"' package.json 2>/dev/null; then
90-
echo "Running with test script..."
91-
npm run test -- --watch=false --browsers=ChromeHeadless --include='**/time-planning-pn/**/*.spec.ts'
92-
else
93-
echo "No test script found in package.json, skipping unit tests"
94-
fi
77+
# Run Jest tests for time-planning-pn plugin
78+
echo "Running Jest tests for time-planning-pn plugin..."
79+
npm test -- --testPathPattern=time-planning-pn --coverage --collectCoverageFrom='src/app/plugins/modules/time-planning-pn/**/*.ts' --coveragePathIgnorePatterns='\.spec\.ts$'
9580
pn-test:
9681
needs: build
9782
runs-on: ubuntu-22.04

eform-client/src/app/plugins/modules/time-planning-pn/services/time-planning-pn-plannings.service.spec.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
import { TestBed } from '@angular/core/testing';
21
import { TimePlanningPnPlanningsService } from './time-planning-pn-plannings.service';
32
import { ApiBaseService } from 'src/app/common/services';
43
import { of } from 'rxjs';
54

65
describe('TimePlanningPnPlanningsService', () => {
76
let service: TimePlanningPnPlanningsService;
8-
let mockApiBaseService: jasmine.SpyObj<ApiBaseService>;
7+
let mockApiBaseService: jest.Mocked<ApiBaseService>;
98

109
beforeEach(() => {
11-
mockApiBaseService = jasmine.createSpyObj('ApiBaseService', ['post', 'put', 'get']);
10+
mockApiBaseService = {
11+
post: jest.fn(),
12+
put: jest.fn(),
13+
get: jest.fn(),
14+
} as any;
1215

13-
TestBed.configureTestingModule({
14-
providers: [
15-
TimePlanningPnPlanningsService,
16-
{ provide: ApiBaseService, useValue: mockApiBaseService }
17-
]
18-
});
19-
20-
service = TestBed.inject(TimePlanningPnPlanningsService);
16+
service = new TimePlanningPnPlanningsService(mockApiBaseService);
2117
});
2218

2319
it('should be created', () => {
2420
expect(service).toBeTruthy();
2521
});
2622

2723
describe('getPlannings', () => {
28-
it('should call apiBaseService.post with correct parameters', () => {
24+
it('should call apiBaseService.post with correct parameters', (done) => {
2925
const mockRequest = {
3026
dateFrom: '2024-01-01',
3127
dateTo: '2024-01-07',
@@ -35,10 +31,11 @@ describe('TimePlanningPnPlanningsService', () => {
3531
showResignedSites: false
3632
};
3733
const mockResponse = { success: true, model: [] };
38-
mockApiBaseService.post.and.returnValue(of(mockResponse as any));
34+
mockApiBaseService.post.mockReturnValue(of(mockResponse as any));
3935

4036
service.getPlannings(mockRequest).subscribe(result => {
4137
expect(result).toEqual(mockResponse as any);
38+
done();
4239
});
4340

4441
expect(mockApiBaseService.post).toHaveBeenCalledWith(
@@ -47,7 +44,7 @@ describe('TimePlanningPnPlanningsService', () => {
4744
);
4845
});
4946

50-
it('should handle empty response', () => {
47+
it('should handle empty response', (done) => {
5148
const mockRequest = {
5249
dateFrom: '2024-01-01',
5350
dateTo: '2024-01-07',
@@ -57,27 +54,29 @@ describe('TimePlanningPnPlanningsService', () => {
5754
showResignedSites: false
5855
};
5956
const mockResponse = { success: true, model: [] };
60-
mockApiBaseService.post.and.returnValue(of(mockResponse as any));
57+
mockApiBaseService.post.mockReturnValue(of(mockResponse as any));
6158

6259
service.getPlannings(mockRequest).subscribe(result => {
6360
expect(result.model).toEqual([]);
61+
done();
6462
});
6563
});
6664
});
6765

6866
describe('updatePlanning', () => {
69-
it('should call apiBaseService.put with correct parameters', () => {
67+
it('should call apiBaseService.put with correct parameters', (done) => {
7068
const mockPlanningModel = {
7169
id: 123,
7270
planHours: 8,
7371
message: 1,
7472
planText: 'Test planning'
7573
} as any;
7674
const mockResponse = { success: true };
77-
mockApiBaseService.put.and.returnValue(of(mockResponse as any));
75+
mockApiBaseService.put.mockReturnValue(of(mockResponse as any));
7876

7977
service.updatePlanning(mockPlanningModel, 123).subscribe(result => {
8078
expect(result).toEqual(mockResponse as any);
79+
done();
8180
});
8281

8382
expect(mockApiBaseService.put).toHaveBeenCalledWith(
@@ -89,7 +88,7 @@ describe('TimePlanningPnPlanningsService', () => {
8988
it('should construct correct URL with id parameter', () => {
9089
const mockPlanningModel = { id: 456 } as any;
9190
const mockResponse = { success: true };
92-
mockApiBaseService.put.and.returnValue(of(mockResponse as any));
91+
mockApiBaseService.put.mockReturnValue(of(mockResponse as any));
9392

9493
service.updatePlanning(mockPlanningModel, 456).subscribe();
9594

0 commit comments

Comments
 (0)