Skip to content

Commit 0060907

Browse files
Copilotrenemadsen
andcommitted
Add unit tests for TimePlanningsContainer and TimePlanningsTable components
Co-authored-by: renemadsen <[email protected]>
1 parent 57df597 commit 0060907

File tree

5 files changed

+550
-0
lines changed

5 files changed

+550
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,34 @@ jobs:
4242
with:
4343
name: time-planning-container
4444
path: time-planning-container.tar
45+
angular-unit-test:
46+
runs-on: ubuntu-latest
47+
steps:
48+
- uses: actions/checkout@v3
49+
with:
50+
path: eform-angular-timeplanning-plugin
51+
- name: Extract branch name
52+
id: extract_branch
53+
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
54+
- name: 'Preparing Frontend checkout'
55+
uses: actions/checkout@v3
56+
with:
57+
fetch-depth: 0
58+
repository: microting/eform-angular-frontend
59+
ref: ${{ steps.extract_branch.outputs.branch }}
60+
path: eform-angular-frontend
61+
- name: Use Node.js
62+
uses: actions/setup-node@v3
63+
with:
64+
node-version: 20
65+
- name: Copy dependencies
66+
run: |
67+
cp -av eform-angular-timeplanning-plugin/eform-client/src/app/plugins/modules/time-planning-pn eform-angular-frontend/eform-client/src/app/plugins/modules/time-planning-pn
68+
cd eform-angular-frontend/eform-client && ../../eform-angular-timeplanning-plugin/testinginstallpn.sh
69+
- name: yarn install
70+
run: cd eform-angular-frontend/eform-client && yarn install
71+
- name: Run Angular unit tests
72+
run: cd eform-angular-frontend/eform-client && npm run test:ci -- --include='**/time-planning-pn/**/*.spec.ts'
4573
pn-test:
4674
needs: build
4775
runs-on: ubuntu-latest

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,31 @@ jobs:
3939
with:
4040
name: time-planning-container
4141
path: time-planning-container.tar
42+
angular-unit-test:
43+
runs-on: ubuntu-latest
44+
steps:
45+
- uses: actions/checkout@v3
46+
with:
47+
path: eform-angular-timeplanning-plugin
48+
- name: 'Preparing Frontend checkout'
49+
uses: actions/checkout@v3
50+
with:
51+
fetch-depth: 0
52+
repository: microting/eform-angular-frontend
53+
ref: stable
54+
path: eform-angular-frontend
55+
- name: Use Node.js
56+
uses: actions/setup-node@v3
57+
with:
58+
node-version: 20
59+
- name: Copy dependencies
60+
run: |
61+
cp -av eform-angular-timeplanning-plugin/eform-client/src/app/plugins/modules/time-planning-pn eform-angular-frontend/eform-client/src/app/plugins/modules/time-planning-pn
62+
cd eform-angular-frontend/eform-client && ../../eform-angular-timeplanning-plugin/testinginstallpn.sh
63+
- name: yarn install
64+
run: cd eform-angular-frontend/eform-client && yarn install
65+
- name: Run Angular unit tests
66+
run: cd eform-angular-frontend/eform-client && npm run test:ci -- --include='**/time-planning-pn/**/*.spec.ts'
4267
pn-test:
4368
needs: build
4469
runs-on: ubuntu-22.04
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Angular Unit Tests for Time Planning Plugin
2+
3+
This directory contains unit tests for the Time Planning plugin components.
4+
5+
## Test Files
6+
7+
- `time-plannings-container.component.spec.ts` - Unit tests for the container component
8+
- `time-plannings-table.component.spec.ts` - Unit tests for the table component
9+
10+
## Running Tests
11+
12+
These tests are designed to run when the plugin is integrated into the main eform-angular-frontend repository.
13+
14+
To run the tests locally:
15+
16+
1. Copy the plugin files to the main frontend repository (use devinstall.sh)
17+
2. Navigate to the frontend directory
18+
3. Run: `npm test` or `ng test`
19+
20+
## Test Coverage
21+
22+
### TimePlanningsContainerComponent
23+
- Date navigation (forward/backward)
24+
- Date formatting
25+
- Event handlers
26+
- Dialog interactions
27+
- Site filtering (resigned sites toggle)
28+
29+
### TimePlanningsTableComponent
30+
- Time conversion utilities (minutes to time, hours to time)
31+
- Cell styling logic based on work status
32+
- Date validation
33+
- Stop time display formatting
34+
35+
## Running in CI/CD
36+
37+
The GitHub Actions workflow will automatically run these tests when changes are pushed to the repository.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { TimePlanningsContainerComponent } from './time-plannings-container.component';
3+
import { TimePlanningPnPlanningsService } from '../../../services/time-planning-pn-plannings.service';
4+
import { TimePlanningPnSettingsService } from '../../../services/time-planning-pn-settings.service';
5+
import { MatDialog } from '@angular/material/dialog';
6+
import { Store } from '@ngrx/store';
7+
import { of } from 'rxjs';
8+
import { format } from 'date-fns';
9+
10+
describe('TimePlanningsContainerComponent', () => {
11+
let component: TimePlanningsContainerComponent;
12+
let fixture: ComponentFixture<TimePlanningsContainerComponent>;
13+
let mockPlanningsService: jasmine.SpyObj<TimePlanningPnPlanningsService>;
14+
let mockSettingsService: jasmine.SpyObj<TimePlanningPnSettingsService>;
15+
let mockDialog: jasmine.SpyObj<MatDialog>;
16+
let mockStore: jasmine.SpyObj<Store>;
17+
18+
beforeEach(async () => {
19+
mockPlanningsService = jasmine.createSpyObj('TimePlanningPnPlanningsService', ['getPlannings', 'updatePlanning']);
20+
mockSettingsService = jasmine.createSpyObj('TimePlanningPnSettingsService', ['getAvailableSites', 'getResignedSites', 'getAssignedSite', 'updateAssignedSite']);
21+
mockDialog = jasmine.createSpyObj('MatDialog', ['open']);
22+
mockStore = jasmine.createSpyObj('Store', ['select']);
23+
24+
mockStore.select.and.returnValue(of('en-US'));
25+
mockSettingsService.getAvailableSites.and.returnValue(of({ success: true, model: [] }));
26+
mockPlanningsService.getPlannings.and.returnValue(of({ success: true, model: [] }));
27+
28+
await TestBed.configureTestingModule({
29+
declarations: [TimePlanningsContainerComponent],
30+
providers: [
31+
{ provide: TimePlanningPnPlanningsService, useValue: mockPlanningsService },
32+
{ provide: TimePlanningPnSettingsService, useValue: mockSettingsService },
33+
{ provide: MatDialog, useValue: mockDialog },
34+
{ provide: Store, useValue: mockStore }
35+
]
36+
}).compileComponents();
37+
38+
fixture = TestBed.createComponent(TimePlanningsContainerComponent);
39+
component = fixture.componentInstance;
40+
});
41+
42+
it('should create', () => {
43+
expect(component).toBeTruthy();
44+
});
45+
46+
describe('Date Navigation', () => {
47+
beforeEach(() => {
48+
component.dateFrom = new Date(2024, 0, 15); // Jan 15, 2024
49+
component.dateTo = new Date(2024, 0, 21); // Jan 21, 2024
50+
});
51+
52+
it('should move dates backward by 7 days when goBackward is called', () => {
53+
const expectedDateFrom = new Date(2024, 0, 8); // Jan 8, 2024
54+
const expectedDateTo = new Date(2024, 0, 14); // Jan 14, 2024
55+
56+
component.goBackward();
57+
58+
expect(component.dateFrom.getDate()).toBe(expectedDateFrom.getDate());
59+
expect(component.dateTo.getDate()).toBe(expectedDateTo.getDate());
60+
expect(mockPlanningsService.getPlannings).toHaveBeenCalled();
61+
});
62+
63+
it('should move dates forward by 7 days when goForward is called', () => {
64+
const expectedDateFrom = new Date(2024, 0, 22); // Jan 22, 2024
65+
const expectedDateTo = new Date(2024, 0, 28); // Jan 28, 2024
66+
67+
component.goForward();
68+
69+
expect(component.dateFrom.getDate()).toBe(expectedDateFrom.getDate());
70+
expect(component.dateTo.getDate()).toBe(expectedDateTo.getDate());
71+
expect(mockPlanningsService.getPlannings).toHaveBeenCalled();
72+
});
73+
});
74+
75+
describe('Date Formatting', () => {
76+
it('should format date range correctly', () => {
77+
component.dateFrom = new Date(2024, 0, 15); // Jan 15, 2024
78+
component.dateTo = new Date(2024, 0, 21); // Jan 21, 2024
79+
80+
const result = component.formatDateRange();
81+
82+
expect(result).toBe('15.01.2024 - 21.01.2024');
83+
});
84+
85+
it('should handle single digit days and months correctly', () => {
86+
component.dateFrom = new Date(2024, 0, 1); // Jan 1, 2024
87+
component.dateTo = new Date(2024, 0, 7); // Jan 7, 2024
88+
89+
const result = component.formatDateRange();
90+
91+
expect(result).toBe('01.01.2024 - 07.01.2024');
92+
});
93+
});
94+
95+
describe('Event Handlers', () => {
96+
it('should call getPlannings when onTimePlanningChanged is triggered', () => {
97+
spyOn(component, 'getPlannings');
98+
99+
component.onTimePlanningChanged({});
100+
101+
expect(component.getPlannings).toHaveBeenCalled();
102+
});
103+
104+
it('should call getPlannings when onAssignedSiteChanged is triggered', () => {
105+
spyOn(component, 'getPlannings');
106+
107+
component.onAssignedSiteChanged({});
108+
109+
expect(component.getPlannings).toHaveBeenCalled();
110+
});
111+
112+
it('should update siteId and call getPlannings when onSiteChanged is triggered', () => {
113+
spyOn(component, 'getPlannings');
114+
const testSiteId = 123;
115+
116+
component.onSiteChanged(testSiteId);
117+
118+
expect(component.siteId).toBe(testSiteId);
119+
expect(component.getPlannings).toHaveBeenCalled();
120+
});
121+
});
122+
123+
describe('Dialog', () => {
124+
it('should open download excel dialog with available sites', () => {
125+
component.availableSites = [{ id: 1, name: 'Test Site' } as any];
126+
const mockDialogRef = { afterClosed: () => of(null) };
127+
mockDialog.open.and.returnValue(mockDialogRef as any);
128+
129+
component.openDownloadExcelDialog();
130+
131+
expect(mockDialog.open).toHaveBeenCalled();
132+
});
133+
});
134+
135+
describe('Show Resigned Sites', () => {
136+
it('should load resigned sites when showResignedSites is true', () => {
137+
mockSettingsService.getResignedSites.and.returnValue(of({ success: true, model: [{ id: 1, name: 'Resigned Site' }] } as any));
138+
139+
component.onShowResignedSitesChanged({ checked: true });
140+
141+
expect(mockSettingsService.getResignedSites).toHaveBeenCalled();
142+
expect(component.showResignedSites).toBe(true);
143+
});
144+
145+
it('should load available sites when showResignedSites is false', () => {
146+
component.showResignedSites = true;
147+
mockSettingsService.getAvailableSites.and.returnValue(of({ success: true, model: [{ id: 1, name: 'Available Site' }] } as any));
148+
149+
component.onShowResignedSitesChanged({ checked: false });
150+
151+
expect(mockSettingsService.getAvailableSites).toHaveBeenCalled();
152+
expect(component.showResignedSites).toBe(false);
153+
});
154+
});
155+
});

0 commit comments

Comments
 (0)